Skip to content

Commit b0cf257

Browse files
committed
[Blazor][Wasm] Move HttpClient from default services to extension method (dotnet#16929)
1 parent c935e9a commit b0cf257

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,6 @@ private void InitializeDefaultServices()
9898
Services.AddSingleton<INavigationInterception>(WebAssemblyNavigationInterception.Instance);
9999
Services.AddSingleton<ILoggerFactory, WebAssemblyLoggerFactory>();
100100
Services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(WebAssemblyConsoleLogger<>)));
101-
Services.AddSingleton<HttpClient>(s =>
102-
{
103-
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
104-
var navigationManager = s.GetRequiredService<NavigationManager>();
105-
return new HttpClient
106-
{
107-
BaseAddress = new Uri(navigationManager.BaseUri)
108-
};
109-
});
110101
}
111102
}
112103
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Net.Http;
3+
using Microsoft.AspNetCore.Components;
4+
5+
namespace Microsoft.Extensions.DependencyInjection
6+
{
7+
public static class HttpClientServiceCollectionExtensions
8+
{
9+
/// <summary>
10+
/// Adds a <see cref="HttpClient" /> instance to the <paramref name="serviceCollection" /> that is
11+
/// configured to use the application's base address (<seealso cref="NavigationManager.BaseUri" />).
12+
/// </summary>
13+
/// <param name="serviceCollection">The <see cref="IServiceCollection" />.</param>
14+
/// <returns>The configured <see cref="IServiceCollection" />.</returns>
15+
public static IServiceCollection AddBaseAddressHttpClient(this IServiceCollection serviceCollection)
16+
{
17+
return serviceCollection.AddSingleton(s =>
18+
{
19+
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
20+
var navigationManager = s.GetRequiredService<NavigationManager>();
21+
return new HttpClient
22+
{
23+
BaseAddress = new Uri(navigationManager.BaseUri)
24+
};
25+
});
26+
}
27+
}
28+
}

src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostBuilderTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ private static IReadOnlyList<Type> DefaultServiceTypes
8080
typeof(NavigationManager),
8181
typeof(INavigationInterception),
8282
typeof(ILoggerFactory),
83-
typeof(HttpClient),
8483
typeof(ILogger<>),
8584
};
8685
}

src/Components/WebAssembly/testassets/StandaloneApp/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
6+
using Microsoft.Extensions.DependencyInjection;
67

78
namespace StandaloneApp
89
{
@@ -12,6 +13,7 @@ public static async Task Main(string[] args)
1213
{
1314
var builder = WebAssemblyHostBuilder.CreateDefault(args);
1415
builder.RootComponents.Add<App>("app");
16+
builder.Services.AddBaseAddressHttpClient();
1517

1618
await builder.Build().RunAsync();
1719
}

src/Components/test/testassets/BasicTestApp/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static async Task Main(string[] args)
3434

3535
builder.RootComponents.Add<Index>("root");
3636

37+
builder.Services.AddBaseAddressHttpClient();
3738
builder.Services.AddSingleton<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
3839
builder.Services.AddAuthorizationCore(options =>
3940
{

src/ProjectTemplates/ComponentsWebAssembly.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Client/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using System.Text;
@@ -19,6 +19,7 @@ public static async Task Main(string[] args)
1919
builder.RootComponents.Add<App>("app");
2020

2121
// use builder.Services to configure application services.
22+
builder.Services.AddBaseAddressHttpClient();
2223
#if (IndividualLocalAuth)
2324
#if (Hosted)
2425
builder.Services.AddApiAuthorization();

0 commit comments

Comments
 (0)