File tree Expand file tree Collapse file tree
test/Dapr.AspNetCore.Test Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -31,18 +31,32 @@ public static class DaprServiceCollectionExtensions
3131 /// <param name="configure"></param>
3232 public static void AddDaprClient ( this IServiceCollection services , Action < DaprClientBuilder > configure = null )
3333 {
34- if ( services is null )
35- {
36- throw new ArgumentNullException ( nameof ( services ) ) ;
37- }
34+ ArgumentNullException . ThrowIfNull ( services , nameof ( services ) ) ;
3835
3936 services . TryAddSingleton ( _ =>
4037 {
4138 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 ) ;
4660
4761 return builder . Build ( ) ;
4862 } ) ;
Original file line number Diff line number Diff line change @@ -48,6 +48,31 @@ public void AddDaprClient_RegistersDaprClientOnlyOnce()
4848 Assert . True ( daprClient . JsonSerializerOptions . PropertyNameCaseInsensitive ) ;
4949 }
5050
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+
5176#if NET8_0_OR_GREATER
5277 [ Fact ]
5378 public void AddDaprClient_WithKeyedServices ( )
@@ -65,5 +90,10 @@ public void AddDaprClient_WithKeyedServices()
6590 Assert . NotNull ( daprClient ) ;
6691 }
6792#endif
93+
94+ private class TestConfigurationProvider
95+ {
96+ public bool GetCaseSensitivity ( ) => false ;
97+ }
6898 }
6999}
You can’t perform that action at this time.
0 commit comments