A framework for splitting out the registration of services in IServiceCollection into profiles. No more large Startup.cs classes.
- When building an IHost, add ConfigureServicesWithProfiles() method to the IHostBuilder to load Service Profiles in the executing assembly.
IHostBuilder builder = Host.CreateDefaultBuilder(args).ConfigureServicesWithProfiles();
-
Instantiate a HostServiceProfileLoader() class.
-
Execute one of the Load() methods and pass in the IServiceCollection, IConfiguration and IHostEnvironment.
IHostServiceProfileLoader loader = new HostServiceProfileLoader();
loader.Load(services, Configuration, Environment);
loader.LoadFromAssembly(services, Configuration, Environment, typeof(Program).Assembly);
loader.LoadFromAssemblies(services, Configuration, Environment, new Assembly[] {typeof(Program).Assembly});
- Create profile classes that derive from HostServiceProfile and implement the Configure() method.
public class TestProfile : HostServiceProfile
{
public override void Configure(IHostServiceProfileContext context)
{
context.Services.AddBeer();
}
}
See the following sample https://github.com/TheDanielDoyle/ServiceProfiles/blob/develop/Samples/ServiceProfiles.Samples.Console/Program.cs