File tree 6 files changed +36
-12
lines changed
test/testassets/BasicTestApp
ProjectTemplates/ComponentsWebAssembly.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Client
6 files changed +36
-12
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
+ // 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
+ }
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 ;
@@ -18,7 +18,7 @@ public static async Task Main(string[] args)
18
18
var builder = WebAssemblyHostBuilder . CreateDefault ( args ) ;
19
19
builder . RootComponents . Add < App > ( "app" ) ;
20
20
21
- // use builder.Services to configure application services.
21
+ builder . Services . AddBaseAddressHttpClient ( ) ;
22
22
#if ( IndividualLocalAuth )
23
23
#if ( Hosted )
24
24
builder . Services . AddApiAuthorization ( ) ;
You can’t perform that action at this time.
0 commit comments