Skip to content

Commit 6a85855

Browse files
[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 <[email protected]>
1 parent e9ccd7f commit 6a85855

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Net.Http;
6+
using Microsoft.AspNetCore.Components;
7+
8+
namespace Microsoft.Extensions.DependencyInjection
9+
{
10+
public static class HttpClientServiceCollectionExtensions
11+
{
12+
/// <summary>
13+
/// Adds a <see cref="HttpClient" /> instance to the <paramref name="serviceCollection" /> that is
14+
/// configured to use the application's base address (<seealso cref="NavigationManager.BaseUri" />).
15+
/// </summary>
16+
/// <param name="serviceCollection">The <see cref="IServiceCollection" />.</param>
17+
/// <returns>The configured <see cref="IServiceCollection" />.</returns>
18+
public static IServiceCollection AddBaseAddressHttpClient(this IServiceCollection serviceCollection)
19+
{
20+
return serviceCollection.AddSingleton(s =>
21+
{
22+
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
23+
var navigationManager = s.GetRequiredService<NavigationManager>();
24+
return new HttpClient
25+
{
26+
BaseAddress = new Uri(navigationManager.BaseUri)
27+
};
28+
});
29+
}
30+
}
31+
}

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 & 2 deletions
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;
@@ -18,7 +18,7 @@ public static async Task Main(string[] args)
1818
var builder = WebAssemblyHostBuilder.CreateDefault(args);
1919
builder.RootComponents.Add<App>("app");
2020

21-
// use builder.Services to configure application services.
21+
builder.Services.AddBaseAddressHttpClient();
2222
#if (IndividualLocalAuth)
2323
#if (Hosted)
2424
builder.Services.AddApiAuthorization();

0 commit comments

Comments
 (0)