Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added overload for DaprClient DI registration #1289

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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;
}
}
}
Loading