
csharp-netcore.libraries.generichost.IServiceCollectionExtensions.mustache Maven / Gradle / Ivy
{{>partial_header}}
{{#nrt}}
#nullable enable
{{/nrt}}
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using {{packageName}}.{{clientPackage}};
using {{packageName}}.{{apiPackage}};
namespace {{packageName}}.Extensions
{
///
/// Extension methods for IServiceCollection
///
{{>visibility}} static class IServiceCollectionExtensions
{
{{^hasAuthMethods}}
///
/// Add the api to your host builder.
///
///
///
public static void Add{{apiName}}<{{#apiInfo}}{{#apis}}T{{classname}}{{^-last}}, {{/-last}}{{/apis}}{{/apiInfo}}>(this IServiceCollection services)
{{#apiInfo}}
{{#apis}}
where T{{classname}} : class, {{interfacePrefix}}{{apiPackage}}.{{interfacePrefix}}{{classname}}
{{/apis}}
{{/apiInfo}}
{
HostConfiguration<{{#apiInfo}}{{#apis}}T{{classname}}{{^-last}}, {{/-last}}{{/apis}}{{/apiInfo}}> config = new HostConfiguration<{{#apiInfo}}{{#apis}}T{{classname}}{{^-last}}, {{/-last}}{{/apis}}{{/apiInfo}}>(services);
Add{{apiName}}(services, config);
}
///
/// Add the api to your host builder.
///
///
///
public static void Add{{apiName}}(this IServiceCollection services)
{
HostConfiguration<{{>DefaultApis}}> config = new HostConfiguration<{{>DefaultApis}}>(services);
Add{{apiName}}(services, config);
}
{{/hasAuthMethods}}
///
/// Add the api to your host builder.
///
///
///
public static void Add{{apiName}}<{{#apiInfo}}{{#apis}}T{{classname}}{{^-last}}, {{/-last}}{{/apis}}{{/apiInfo}}>(this IServiceCollection services, Action> options)
{{#apiInfo}}
{{#apis}}
where T{{classname}} : class, {{interfacePrefix}}{{apiPackage}}.{{interfacePrefix}}{{classname}}
{{/apis}}
{{/apiInfo}}
{
HostConfiguration<{{#apiInfo}}{{#apis}}T{{classname}}{{^-last}}, {{/-last}}{{/apis}}{{/apiInfo}}> config = new HostConfiguration<{{#apiInfo}}{{#apis}}T{{classname}}{{^-last}}, {{/-last}}{{/apis}}{{/apiInfo}}>(services);
options(config);
Add{{apiName}}(services, config);
}
///
/// Add the api to your host builder.
///
///
///
public static void Add{{apiName}}(this IServiceCollection services, ActionDefaultApis}}>> options)
{
HostConfiguration<{{>DefaultApis}}> config = new HostConfiguration<{{>DefaultApis}}>(services);
options(config);
Add{{apiName}}(services, config);
}
internal static void Add{{apiName}}<{{#apiInfo}}{{#apis}}T{{classname}}{{^-last}}, {{/-last}}{{/apis}}{{/apiInfo}}>(IServiceCollection services, HostConfiguration<{{#apiInfo}}{{#apis}}T{{classname}}{{^-last}}, {{/-last}}{{/apis}}{{/apiInfo}}> host)
{{#apiInfo}}
{{#apis}}
where T{{classname}} : class, {{interfacePrefix}}{{apiPackage}}.{{interfacePrefix}}{{classname}}
{{/apis}}
{{/apiInfo}}
{
if (!host.HttpClientsAdded)
host.Add{{apiName}}HttpClients();
services.AddSingleton();
// ensure that a token provider was provided for this token type
// if not, default to RateLimitProvider
var containerServices = services.Where(s => s.ServiceType.IsGenericType &&
s.ServiceType.GetGenericTypeDefinition().IsAssignableFrom(typeof(TokenContainer<>))).ToArray();
foreach(var containerService in containerServices)
{
var tokenType = containerService.ServiceType.GenericTypeArguments[0];
var provider = services.FirstOrDefault(s => s.ServiceType.IsAssignableFrom(typeof(TokenProvider<>).MakeGenericType(tokenType)));
if (provider == null)
{
services.AddSingleton(typeof(RateLimitProvider<>).MakeGenericType(tokenType));
services.AddSingleton(typeof(TokenProvider<>).MakeGenericType(tokenType),
s => s.GetRequiredService(typeof(RateLimitProvider<>).MakeGenericType(tokenType)));
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy