Skip to content

Commit

Permalink
Added overload for DaprClient DI registration (dapr#1289)
Browse files Browse the repository at this point in the history
* Added overload for DaprClient DI registration allowing the consumer to easily use values from injected services (e.g. IConfiguration).

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

* Added supporting unit test

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

---------

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
Co-authored-by: Phillip Hoff <phillip@orst.edu>
  • Loading branch information
2 people authored and m3nax committed Jun 25, 2024
1 parent 5e79b9e commit aa1c125
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,32 @@ public static class DaprServiceCollectionExtensions
/// <param name="configure"></param>
public static void AddDaprClient(this IServiceCollection services, Action<DaprClientBuilder> configure = null)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
ArgumentNullException.ThrowIfNull(services, nameof(services));

services.TryAddSingleton(_ =>
{
var builder = new DaprClientBuilder();
if (configure != null)
{
configure.Invoke(builder);
}
configure?.Invoke(builder);

return builder.Build();
});
}

/// <summary>
/// Adds Dapr client services to the provided <see cref="IServiceCollection"/>. This does not include integration
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure"></param>
public static void AddDaprClient(this IServiceCollection services,
Action<IServiceProvider, DaprClientBuilder> configure)
{
ArgumentNullException.ThrowIfNull(services, nameof(services));

services.TryAddSingleton(serviceProvider =>
{
var builder = new DaprClientBuilder();
configure?.Invoke(serviceProvider, builder);

return builder.Build();
});
Expand Down
30 changes: 30 additions & 0 deletions test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ public void AddDaprClient_RegistersDaprClientOnlyOnce()
Assert.True(daprClient.JsonSerializerOptions.PropertyNameCaseInsensitive);
}

[Fact]
public void AddDaprClient_RegistersUsingDependencyFromIServiceProvider()
{

var services = new ServiceCollection();
services.AddSingleton<TestConfigurationProvider>();
services.AddDaprClient((provider, builder) =>
{
var configProvider = provider.GetRequiredService<TestConfigurationProvider>();
var caseSensitivity = configProvider.GetCaseSensitivity();

builder.UseJsonSerializationOptions(new JsonSerializerOptions
{
PropertyNameCaseInsensitive = caseSensitivity
});
});

var serviceProvider = services.BuildServiceProvider();

DaprClientGrpc client = serviceProvider.GetRequiredService<DaprClient>() as DaprClientGrpc;

//Registers with case-insensitive as true by default, but we set as false above
Assert.False(client.JsonSerializerOptions.PropertyNameCaseInsensitive);
}

#if NET8_0_OR_GREATER
[Fact]
public void AddDaprClient_WithKeyedServices()
Expand All @@ -65,5 +90,10 @@ public void AddDaprClient_WithKeyedServices()
Assert.NotNull(daprClient);
}
#endif

private class TestConfigurationProvider
{
public bool GetCaseSensitivity() => false;
}
}
}

0 comments on commit aa1c125

Please sign in to comment.