Skip to content

Commit

Permalink
Review lifecycle of services registered in the DI container (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegoldman2 authored Jan 26, 2024
1 parent 2b60596 commit 0a9886d
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 @@ -23,7 +23,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 @@ -42,14 +42,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 @@ -62,7 +62,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 @@ -80,15 +80,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 0a9886d

Please sign in to comment.