Skip to content

Commit

Permalink
Use TryAddSingleton() for registering services. (#1238)
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Hoff <[email protected]>
  • Loading branch information
philliphoff authored Feb 16, 2024
1 parent 83858d7 commit e244e88
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
21 changes: 7 additions & 14 deletions src/Dapr.AspNetCore/DaprMvcBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Microsoft.Extensions.DependencyInjection
using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.Extensions.DependencyInjection.Extensions;

/// <summary>
/// Provides extension methods for <see cref="IMvcBuilder" />.
Expand All @@ -40,27 +41,19 @@ public static IMvcBuilder AddDapr(this IMvcBuilder builder, Action<DaprClientBui
throw new ArgumentNullException(nameof(builder));
}

// This pattern prevents registering services multiple times in the case AddDapr is called
// by non-user-code.
if (builder.Services.Any(s => s.ImplementationType == typeof(DaprMvcMarkerService)))
{
return builder;
}

builder.Services.AddDaprClient(configureClient);

builder.Services.AddSingleton<DaprMvcMarkerService>();
builder.Services.AddSingleton<IApplicationModelProvider, StateEntryApplicationModelProvider>();
builder.Services.TryAddSingleton<IApplicationModelProvider, StateEntryApplicationModelProvider>();

builder.Services.Configure<MvcOptions>(options =>
{
options.ModelBinderProviders.Insert(0, new StateEntryModelBinderProvider());
if (!options.ModelBinderProviders.Any(p => p is StateEntryModelBinderProvider))
{
options.ModelBinderProviders.Insert(0, new StateEntryModelBinderProvider());
}
});

return builder;
}

private class DaprMvcMarkerService
{
}
}
}
13 changes: 0 additions & 13 deletions src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ public static void AddDaprClient(this IServiceCollection services, Action<DaprCl
throw new ArgumentNullException(nameof(services));
}

// This pattern prevents registering services multiple times in the case AddDaprClient is called
// by non-user-code.
if (services.Any(s => s.ImplementationType == typeof(DaprClientMarkerService)))
{
return;
}

services.AddSingleton<DaprClientMarkerService>();

services.TryAddSingleton(_ =>
{
var builder = new DaprClientBuilder();
Expand All @@ -56,9 +47,5 @@ public static void AddDaprClient(this IServiceCollection services, Action<DaprCl
return builder.Build();
});
}

private class DaprClientMarkerService
{
}
}
}
18 changes: 18 additions & 0 deletions test/Dapr.AspNetCore.Test/DaprMvcBuilderExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,23 @@ public void AddDapr_RegistersDaprOnlyOnce()

Assert.False(daprClient.JsonSerializerOptions.PropertyNameCaseInsensitive);
}

#if NET8_0_OR_GREATER
[Fact]
public void AddDapr_WithKeyedServices()
{
var services = new ServiceCollection();

services.AddKeyedSingleton("key1", new Object());

services.AddControllers().AddDapr();

var serviceProvider = services.BuildServiceProvider();

var daprClient = serviceProvider.GetService<DaprClient>();

Assert.NotNull(daprClient);
}
#endif
}
}
18 changes: 18 additions & 0 deletions test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,23 @@ public void AddDaprClient_RegistersDaprClientOnlyOnce()

Assert.True(daprClient.JsonSerializerOptions.PropertyNameCaseInsensitive);
}

#if NET8_0_OR_GREATER
[Fact]
public void AddDaprClient_WithKeyedServices()
{
var services = new ServiceCollection();

services.AddKeyedSingleton("key1", new Object());

services.AddDaprClient();

var serviceProvider = services.BuildServiceProvider();

var daprClient = serviceProvider.GetService<DaprClient>();

Assert.NotNull(daprClient);
}
#endif
}
}

0 comments on commit e244e88

Please sign in to comment.