Skip to content

Instantly share code, notes, and snippets.

@configureappio
Created June 6, 2018 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save configureappio/30e690ecb7b227d67580d5c9d9b6240e to your computer and use it in GitHub Desktop.
Save configureappio/30e690ecb7b227d67580d5c9d9b6240e to your computer and use it in GitHub Desktop.
Example of delegating service registration to a method in another assembly
public static class ServiceConfiguration
{
public static void ConfigureServices(IServiceCollection services, IMvcBuilder mvcBuilder)
{
services.AddTransient(provider => new ValidatorActionFilter());
mvcBuilder.AddMvcOptions(o => o.Filters.AddService<ValidatorActionFilter>());
mvcBuilder.AddFluentValidation(fvc => fvc.RegisterValidatorsFromAssemblyContaining<PersonValidation>());
services.AddSingleton<IEnglishHello, EnglishHello>();
services.AddSingleton<IFrenchHello, FrenchHello>();
services.AddSingleton<IEnglishInformalHello, EnglishInformalHello>();
services.AddSingleton<ISayHelloLanguage>(provider => provider.GetService<IEnglishHello>());
services.AddSingleton<ISayHelloLanguage>(provider => provider.GetService<IFrenchHello>());
services.AddSingleton<ISayHello>(provider => provider.GetService<IEnglishHello>());
services.AddSingleton<ISayHello>(provider => provider.GetService<IFrenchHello>());
services.AddSingleton<ISayHello>(provider => provider.GetService<IEnglishInformalHello>());
services.AddSingleton<ISayHelloFactory>(provider =>
new SayHelloFactory(provider.GetService<IEnumerable<ISayHello>>()));
}
public static IServiceProvider BuildAlternativeServiceProvider(this IServiceCollection services)
{
var builder = new ContainerBuilder();
builder.Populate(services);
return new AutofacServiceProvider(builder.Build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment