[Blazor][Wasm] Move HttpClient from default services to Program.Main (#19119)
* [Blazor][Wasm] Move HttpClient from default services to extension method (#16929) * Apply suggestions from code review Co-authored-by: Steve Sanderson <SteveSandersonMS@users.noreply.github.com>
This commit is contained in:
parent
e9ccd7fe08
commit
6a85855fbb
|
|
@ -98,15 +98,6 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting
|
|||
Services.AddSingleton<INavigationInterception>(WebAssemblyNavigationInterception.Instance);
|
||||
Services.AddSingleton<ILoggerFactory, WebAssemblyLoggerFactory>();
|
||||
Services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(WebAssemblyConsoleLogger<>)));
|
||||
Services.AddSingleton<HttpClient>(s =>
|
||||
{
|
||||
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
|
||||
var navigationManager = s.GetRequiredService<NavigationManager>();
|
||||
return new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(navigationManager.BaseUri)
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public static class HttpClientServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a <see cref="HttpClient" /> instance to the <paramref name="serviceCollection" /> that is
|
||||
/// configured to use the application's base address (<seealso cref="NavigationManager.BaseUri" />).
|
||||
/// </summary>
|
||||
/// <param name="serviceCollection">The <see cref="IServiceCollection" />.</param>
|
||||
/// <returns>The configured <see cref="IServiceCollection" />.</returns>
|
||||
public static IServiceCollection AddBaseAddressHttpClient(this IServiceCollection serviceCollection)
|
||||
{
|
||||
return serviceCollection.AddSingleton(s =>
|
||||
{
|
||||
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
|
||||
var navigationManager = s.GetRequiredService<NavigationManager>();
|
||||
return new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(navigationManager.BaseUri)
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -80,7 +80,6 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting
|
|||
typeof(NavigationManager),
|
||||
typeof(INavigationInterception),
|
||||
typeof(ILoggerFactory),
|
||||
typeof(HttpClient),
|
||||
typeof(ILogger<>),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace StandaloneApp
|
||||
{
|
||||
|
|
@ -12,6 +13,7 @@ namespace StandaloneApp
|
|||
{
|
||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
builder.RootComponents.Add<App>("app");
|
||||
builder.Services.AddBaseAddressHttpClient();
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ namespace BasicTestApp
|
|||
|
||||
builder.RootComponents.Add<Index>("root");
|
||||
|
||||
builder.Services.AddBaseAddressHttpClient();
|
||||
builder.Services.AddSingleton<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
|
||||
builder.Services.AddAuthorizationCore(options =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text;
|
||||
|
|
@ -18,7 +18,7 @@ namespace ComponentsWebAssembly_CSharp
|
|||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
builder.RootComponents.Add<App>("app");
|
||||
|
||||
// use builder.Services to configure application services.
|
||||
builder.Services.AddBaseAddressHttpClient();
|
||||
#if (IndividualLocalAuth)
|
||||
#if (Hosted)
|
||||
builder.Services.AddApiAuthorization();
|
||||
|
|
|
|||
Loading…
Reference in New Issue