[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:
Marlon Regenhardt 2020-02-20 19:47:49 +01:00 committed by GitHub
parent e9ccd7fe08
commit 6a85855fbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 12 deletions

View File

@ -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)
};
});
}
}
}

View File

@ -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)
};
});
}
}
}

View File

@ -80,7 +80,6 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting
typeof(NavigationManager),
typeof(INavigationInterception),
typeof(ILoggerFactory),
typeof(HttpClient),
typeof(ILogger<>),
};
}

View File

@ -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();
}

View File

@ -34,6 +34,7 @@ namespace BasicTestApp
builder.RootComponents.Add<Index>("root");
builder.Services.AddBaseAddressHttpClient();
builder.Services.AddSingleton<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
builder.Services.AddAuthorizationCore(options =>
{

View File

@ -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();