1
+ // Copyright (c) Duende Software. All rights reserved.
2
+ // See LICENSE in the project root for license information.
3
+
1
4
using Microsoft . AspNetCore . Components . Authorization ;
2
5
using Microsoft . AspNetCore . Components . WebAssembly . Hosting ;
3
6
using Microsoft . Extensions . DependencyInjection ;
@@ -7,9 +10,10 @@ namespace Duende.Bff.Blazor.Client;
7
10
8
11
public static class ServiceCollectionExtensions
9
12
{
10
- public static IServiceCollection AddBff ( this IServiceCollection services , Action < BffBlazorOptions > ? configureAction = null )
13
+ public static IServiceCollection AddBff ( this IServiceCollection services ,
14
+ Action < BffBlazorOptions > ? configureAction = null )
11
15
{
12
- if ( configureAction != null )
16
+ if ( configureAction != null )
13
17
{
14
18
services . Configure ( configureAction ) ;
15
19
}
@@ -24,14 +28,14 @@ public static IServiceCollection AddBff(this IServiceCollection services, Actio
24
28
var baseAddress = GetBaseAddress ( sp ) ;
25
29
client . BaseAddress = new Uri ( baseAddress ) ;
26
30
} ) . AddHttpMessageHandler < AntiforgeryHandler > ( ) ;
27
-
31
+
28
32
return services ;
29
33
}
30
34
31
35
private static string GetBaseAddress ( IServiceProvider sp )
32
36
{
33
37
var opt = sp . GetRequiredService < IOptions < BffBlazorOptions > > ( ) ;
34
- if ( opt . Value . RemoteApiBaseAddress != null )
38
+ if ( opt . Value . RemoteApiBaseAddress != null )
35
39
{
36
40
return opt . Value . RemoteApiBaseAddress ;
37
41
}
@@ -48,7 +52,8 @@ private static string GetRemoteApiPath(IServiceProvider sp)
48
52
return opt . Value . RemoteApiPath ;
49
53
}
50
54
51
- private static Action < IServiceProvider , HttpClient > SetBaseAddressInConfigureClient ( Action < IServiceProvider , HttpClient > ? configureClient )
55
+ private static Action < IServiceProvider , HttpClient > SetBaseAddressInConfigureClient (
56
+ Action < IServiceProvider , HttpClient > ? configureClient )
52
57
{
53
58
return ( sp , client ) =>
54
59
{
@@ -57,7 +62,8 @@ private static Action<IServiceProvider, HttpClient> SetBaseAddressInConfigureCli
57
62
} ;
58
63
}
59
64
60
- private static Action < IServiceProvider , HttpClient > SetBaseAddressInConfigureClient ( Action < HttpClient > ? configureClient )
65
+ private static Action < IServiceProvider , HttpClient > SetBaseAddressInConfigureClient (
66
+ Action < HttpClient > ? configureClient )
61
67
{
62
68
return ( sp , client ) =>
63
69
{
@@ -81,37 +87,43 @@ private static void SetBaseAddress(IServiceProvider sp, HttpClient client)
81
87
{
82
88
remoteApiPath = remoteApiPath . Substring ( 1 ) ;
83
89
}
90
+
84
91
if ( ! remoteApiPath . EndsWith ( "/" ) )
85
92
{
86
93
remoteApiPath += "/" ;
87
94
}
88
95
}
96
+
89
97
client . BaseAddress = new Uri ( new Uri ( baseAddress ) , remoteApiPath ) ;
90
98
}
91
99
92
- public static IHttpClientBuilder AddRemoteApiHttpClient ( this IServiceCollection services , string clientName , Action < HttpClient > configureClient )
100
+ public static IHttpClientBuilder AddRemoteApiHttpClient ( this IServiceCollection services , string clientName ,
101
+ Action < HttpClient > configureClient )
93
102
{
94
- return services . AddHttpClient ( clientName , SetBaseAddressInConfigureClient ( configureClient ) )
103
+ return services . AddHttpClient ( clientName , SetBaseAddressInConfigureClient ( configureClient ) )
95
104
. AddHttpMessageHandler < AntiforgeryHandler > ( ) ;
96
105
}
97
106
98
- public static IHttpClientBuilder AddRemoteApiHttpClient ( this IServiceCollection services , string clientName , Action < IServiceProvider , HttpClient > ? configureClient = null )
107
+ public static IHttpClientBuilder AddRemoteApiHttpClient ( this IServiceCollection services , string clientName ,
108
+ Action < IServiceProvider , HttpClient > ? configureClient = null )
99
109
{
100
110
return services . AddHttpClient ( clientName , SetBaseAddressInConfigureClient ( configureClient ) )
101
- . AddHttpMessageHandler < AntiforgeryHandler > ( ) ;
111
+ . AddHttpMessageHandler < AntiforgeryHandler > ( ) ;
102
112
}
103
-
104
- public static IHttpClientBuilder AddRemoteApiHttpClient < T > ( this IServiceCollection services , Action < HttpClient > configureClient )
113
+
114
+ public static IHttpClientBuilder AddRemoteApiHttpClient < T > ( this IServiceCollection services ,
115
+ Action < HttpClient > configureClient )
105
116
where T : class
106
117
{
107
118
return services . AddHttpClient < T > ( SetBaseAddressInConfigureClient ( configureClient ) )
108
119
. AddHttpMessageHandler < AntiforgeryHandler > ( ) ;
109
120
}
110
121
111
- public static IHttpClientBuilder AddRemoteApiHttpClient < T > ( this IServiceCollection services , Action < IServiceProvider , HttpClient > ? configureClient = null )
122
+ public static IHttpClientBuilder AddRemoteApiHttpClient < T > ( this IServiceCollection services ,
123
+ Action < IServiceProvider , HttpClient > ? configureClient = null )
112
124
where T : class
113
125
{
114
126
return services . AddHttpClient < T > ( SetBaseAddressInConfigureClient ( configureClient ) )
115
- . AddHttpMessageHandler < AntiforgeryHandler > ( ) ;
127
+ . AddHttpMessageHandler < AntiforgeryHandler > ( ) ;
116
128
}
117
- }
129
+ }
0 commit comments