Skip to content

Commit

Permalink
Review lifecycle of services registered in the DI container
Browse files Browse the repository at this point in the history
  • Loading branch information
joegoldman2 committed Nov 10, 2023
1 parent ebfd75d commit cda980b
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions Src/Fido2.AspNet/Fido2NetLibBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IFido2NetLibBuilder AddFido2(this IServiceCollection services, ICo

private static void AddServices(this IServiceCollection services)
{
services.AddTransient<IFido2, Fido2>();
services.AddScoped<IFido2, Fido2>();
services.AddSingleton<IMetadataService, NullMetadataService>(); //Default implementation if we choose not to enable MDS
services.TryAddSingleton<ISystemClock, SystemClock>();
}
Expand All @@ -45,14 +45,14 @@ public static IFido2NetLibBuilder AddFido2(this IServiceCollection services, Act

public static void AddCachedMetadataService(this IFido2NetLibBuilder builder, Action<IFido2MetadataServiceBuilder> configAction)
{
builder.AddMetadataService<DistributedCacheMetadataService>();
builder.Services.AddScoped<IMetadataService, DistributedCacheMetadataService>();

configAction(new Fido2NetLibBuilder(builder.Services));
}

public static IFido2MetadataServiceBuilder AddFileSystemMetadataRepository(this IFido2MetadataServiceBuilder builder, string directoryPath)
{
builder.Services.AddTransient<IMetadataRepository, FileSystemMetadataRepository>(r =>
builder.Services.AddScoped<IMetadataRepository, FileSystemMetadataRepository>(provider =>
{
return new FileSystemMetadataRepository(directoryPath);
});
Expand All @@ -65,7 +65,7 @@ public static IFido2MetadataServiceBuilder AddConformanceMetadataRepository(
HttpClient client = null,
string origin = "")
{
builder.Services.AddTransient<IMetadataRepository>(provider =>
builder.Services.AddScoped<IMetadataRepository>(provider =>
{
return new ConformanceMetadataRepository(client, origin);
});
Expand All @@ -83,15 +83,10 @@ public static IFido2MetadataServiceBuilder AddFidoMetadataRepository(this IFido2
if (clientBuilder != null)
clientBuilder(httpClientBuilder);

builder.Services.AddTransient<IMetadataRepository, Fido2MetadataServiceRepository>();
builder.Services.AddScoped<IMetadataRepository, Fido2MetadataServiceRepository>();

return builder;
}

private static void AddMetadataService<TService>(this IFido2NetLibBuilder builder) where TService : class, IMetadataService
{
builder.Services.AddScoped<IMetadataService, TService>();
}
}

public interface IFido2NetLibBuilder
Expand Down

0 comments on commit cda980b

Please sign in to comment.