Skip to content

Commit

Permalink
Use TryAddSingleton for adding services (microsoft#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kahbazi authored Apr 24, 2020
1 parent 4536b27 commit bee3eca
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.ReverseProxy.Common.Abstractions.Telemetry;
using Microsoft.ReverseProxy.Common.Abstractions.Time;
using Microsoft.ReverseProxy.Common.Telemetry;
Expand All @@ -22,65 +23,65 @@ internal static class Core
public static IReverseProxyBuilder AddTelemetryShims(this IReverseProxyBuilder builder)
{
// NOTE: Consumers of ReverseProxy are expected to replace these with their own classes
builder.Services.AddSingleton<IMetricCreator, NullMetricCreator>();
builder.Services.AddSingleton<IOperationLogger, TextOperationLogger>();
builder.Services.TryAddSingleton<IMetricCreator, NullMetricCreator>();
builder.Services.TryAddSingleton<IOperationLogger, TextOperationLogger>();
return builder;
}

public static IReverseProxyBuilder AddMetrics(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<ProxyMetrics>();
builder.Services.TryAddSingleton<ProxyMetrics>();
return builder;
}

public static IReverseProxyBuilder AddInMemoryRepos(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<IBackendsRepo, InMemoryBackendsRepo>();
builder.Services.AddSingleton<IRoutesRepo, InMemoryRoutesRepo>();
builder.Services.TryAddSingleton<IBackendsRepo, InMemoryBackendsRepo>();
builder.Services.TryAddSingleton<IRoutesRepo, InMemoryRoutesRepo>();

return builder;
}

public static IReverseProxyBuilder AddConfigBuilder(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<IDynamicConfigBuilder, DynamicConfigBuilder>();
builder.Services.AddSingleton<IRouteValidator, RouteValidator>();
builder.Services.AddSingleton<IRuntimeRouteBuilder, RuntimeRouteBuilder>();
builder.Services.TryAddSingleton<IDynamicConfigBuilder, DynamicConfigBuilder>();
builder.Services.TryAddSingleton<IRouteValidator, RouteValidator>();
builder.Services.TryAddSingleton<IRuntimeRouteBuilder, RuntimeRouteBuilder>();
return builder;
}

public static IReverseProxyBuilder AddRuntimeStateManagers(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<IEndpointManagerFactory, EndpointManagerFactory>();
builder.Services.AddSingleton<IBackendManager, BackendManager>();
builder.Services.AddSingleton<IRouteManager, RouteManager>();
builder.Services.TryAddSingleton<IEndpointManagerFactory, EndpointManagerFactory>();
builder.Services.TryAddSingleton<IBackendManager, BackendManager>();
builder.Services.TryAddSingleton<IRouteManager, RouteManager>();
return builder;
}

public static IReverseProxyBuilder AddConfigManager(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<IReverseProxyConfigManager, ReverseProxyConfigManager>();
builder.Services.TryAddSingleton<IReverseProxyConfigManager, ReverseProxyConfigManager>();
return builder;
}

public static IReverseProxyBuilder AddDynamicEndpointDataSource(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<IProxyDynamicEndpointDataSource, ProxyDynamicEndpointDataSource>();
builder.Services.TryAddSingleton<IProxyDynamicEndpointDataSource, ProxyDynamicEndpointDataSource>();
return builder;
}

public static IReverseProxyBuilder AddProxy(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<IProxyHttpClientFactoryFactory, ProxyHttpClientFactoryFactory>();
builder.Services.AddSingleton<ILoadBalancer, LoadBalancer>();
builder.Services.AddSingleton<IRandomFactory, RandomFactory>();
builder.Services.AddSingleton<IHttpProxy, HttpProxy>();
builder.Services.TryAddSingleton<IProxyHttpClientFactoryFactory, ProxyHttpClientFactoryFactory>();
builder.Services.TryAddSingleton<ILoadBalancer, LoadBalancer>();
builder.Services.TryAddSingleton<IRandomFactory, RandomFactory>();
builder.Services.TryAddSingleton<IHttpProxy, HttpProxy>();
return builder;
}

public static IReverseProxyBuilder AddBackgroundWorkers(this IReverseProxyBuilder builder)
{
builder.Services.AddSingleton<IMonotonicTimer, MonotonicTimer>();
builder.Services.TryAddSingleton<IMonotonicTimer, MonotonicTimer>();

return builder;
}
Expand Down

0 comments on commit bee3eca

Please sign in to comment.