File tree 6 files changed +33
-11
lines changed
test/testassets/BasicTestApp
ProjectTemplates/ComponentsWebAssembly.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Client
6 files changed +33
-11
lines changed Original file line number Diff line number Diff line change @@ -98,15 +98,6 @@ private void InitializeDefaultServices()
98
98
Services . AddSingleton < INavigationInterception > ( WebAssemblyNavigationInterception . Instance ) ;
99
99
Services . AddSingleton < ILoggerFactory , WebAssemblyLoggerFactory > ( ) ;
100
100
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
- } ) ;
110
101
}
111
102
}
112
103
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -80,7 +80,6 @@ private static IReadOnlyList<Type> DefaultServiceTypes
80
80
typeof ( NavigationManager ) ,
81
81
typeof ( INavigationInterception ) ,
82
82
typeof ( ILoggerFactory ) ,
83
- typeof ( HttpClient ) ,
84
83
typeof ( ILogger < > ) ,
85
84
} ;
86
85
}
Original file line number Diff line number Diff line change 3
3
4
4
using System . Threading . Tasks ;
5
5
using Microsoft . AspNetCore . Components . WebAssembly . Hosting ;
6
+ using Microsoft . Extensions . DependencyInjection ;
6
7
7
8
namespace StandaloneApp
8
9
{
@@ -12,6 +13,7 @@ public static async Task Main(string[] args)
12
13
{
13
14
var builder = WebAssemblyHostBuilder . CreateDefault ( args ) ;
14
15
builder . RootComponents . Add < App > ( "app" ) ;
16
+ builder . Services . AddBaseAddressHttpClient ( ) ;
15
17
16
18
await builder . Build ( ) . RunAsync ( ) ;
17
19
}
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ public static async Task Main(string[] args)
34
34
35
35
builder . RootComponents . Add < Index > ( "root" ) ;
36
36
37
+ builder . Services . AddBaseAddressHttpClient ( ) ;
37
38
builder . Services . AddSingleton < AuthenticationStateProvider , ServerAuthenticationStateProvider > ( ) ;
38
39
builder . Services . AddAuthorizationCore ( options =>
39
40
{
Original file line number Diff line number Diff line change 1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Threading . Tasks ;
4
4
using System . Text ;
@@ -19,6 +19,7 @@ public static async Task Main(string[] args)
19
19
builder . RootComponents . Add < App > ( "app" ) ;
20
20
21
21
// use builder.Services to configure application services.
22
+ builder . Services . AddBaseAddressHttpClient ( ) ;
22
23
#if ( IndividualLocalAuth )
23
24
#if ( Hosted )
24
25
builder . Services . AddApiAuthorization ( ) ;
You can’t perform that action at this time.
0 commit comments