File tree Expand file tree Collapse file tree 2 files changed +52
-8
lines changed
test/Dapr.AspNetCore.Test Expand file tree Collapse file tree 2 files changed +52
-8
lines changed Original file line number Diff line number Diff line change @@ -31,18 +31,32 @@ public static class DaprServiceCollectionExtensions
31
31
/// <param name="configure"></param>
32
32
public static void AddDaprClient ( this IServiceCollection services , Action < DaprClientBuilder > configure = null )
33
33
{
34
- if ( services is null )
35
- {
36
- throw new ArgumentNullException ( nameof ( services ) ) ;
37
- }
34
+ ArgumentNullException . ThrowIfNull ( services , nameof ( services ) ) ;
38
35
39
36
services . TryAddSingleton ( _ =>
40
37
{
41
38
var builder = new DaprClientBuilder ( ) ;
42
- if ( configure != null )
43
- {
44
- configure . Invoke ( builder ) ;
45
- }
39
+ configure ? . Invoke ( builder ) ;
40
+
41
+ return builder . Build ( ) ;
42
+ } ) ;
43
+ }
44
+
45
+ /// <summary>
46
+ /// Adds Dapr client services to the provided <see cref="IServiceCollection"/>. This does not include integration
47
+ /// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
48
+ /// </summary>
49
+ /// <param name="services">The <see cref="IServiceCollection"/>.</param>
50
+ /// <param name="configure"></param>
51
+ public static void AddDaprClient ( this IServiceCollection services ,
52
+ Action < IServiceProvider , DaprClientBuilder > configure )
53
+ {
54
+ ArgumentNullException . ThrowIfNull ( services , nameof ( services ) ) ;
55
+
56
+ services . TryAddSingleton ( serviceProvider =>
57
+ {
58
+ var builder = new DaprClientBuilder ( ) ;
59
+ configure ? . Invoke ( serviceProvider , builder ) ;
46
60
47
61
return builder . Build ( ) ;
48
62
} ) ;
Original file line number Diff line number Diff line change @@ -48,6 +48,31 @@ public void AddDaprClient_RegistersDaprClientOnlyOnce()
48
48
Assert . True ( daprClient . JsonSerializerOptions . PropertyNameCaseInsensitive ) ;
49
49
}
50
50
51
+ [ Fact ]
52
+ public void AddDaprClient_RegistersUsingDependencyFromIServiceProvider ( )
53
+ {
54
+
55
+ var services = new ServiceCollection ( ) ;
56
+ services . AddSingleton < TestConfigurationProvider > ( ) ;
57
+ services . AddDaprClient ( ( provider , builder ) =>
58
+ {
59
+ var configProvider = provider . GetRequiredService < TestConfigurationProvider > ( ) ;
60
+ var caseSensitivity = configProvider . GetCaseSensitivity ( ) ;
61
+
62
+ builder . UseJsonSerializationOptions ( new JsonSerializerOptions
63
+ {
64
+ PropertyNameCaseInsensitive = caseSensitivity
65
+ } ) ;
66
+ } ) ;
67
+
68
+ var serviceProvider = services . BuildServiceProvider ( ) ;
69
+
70
+ DaprClientGrpc client = serviceProvider . GetRequiredService < DaprClient > ( ) as DaprClientGrpc ;
71
+
72
+ //Registers with case-insensitive as true by default, but we set as false above
73
+ Assert . False ( client . JsonSerializerOptions . PropertyNameCaseInsensitive ) ;
74
+ }
75
+
51
76
#if NET8_0_OR_GREATER
52
77
[ Fact ]
53
78
public void AddDaprClient_WithKeyedServices ( )
@@ -65,5 +90,10 @@ public void AddDaprClient_WithKeyedServices()
65
90
Assert . NotNull ( daprClient ) ;
66
91
}
67
92
#endif
93
+
94
+ private class TestConfigurationProvider
95
+ {
96
+ public bool GetCaseSensitivity ( ) => false ;
97
+ }
68
98
}
69
99
}
You can’t perform that action at this time.
0 commit comments