Dependency Injection
------------------------------------------------------------------------
ASP.NET services can be configured with the following lifetimes:
Transient
Transient lifetime services are created each time they are requested. This lifetime works best for
lightweight, stateless services.
Scoped
Scoped lifetime services are created once per request.
Singleton
Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run
if you specify an instance there) and then every subsequent request will use the same instance. If your
application requires singleton behavior, allowing the services container to manage the service’s lifetime
is recommended instead of implementing the singleton design pattern and managing your object’s lifetime in
the class yourself.
**********************************************************************************
"dependencies" : {
"Autofac": "4.0.0",
"Autofac.Extensions.DependencyInjection": "4.0.0"
},
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// add other framework services
// Add Autofac
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterModule<DefaultModule>();
containerBuilder.Populate(services);
var container = containerBuilder.Build();
return new AutofacServiceProvider(container);
}
public class DefaultModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CharacterRepository>().As<ICharacterRepository>();
}
}
**********************************************************************************
Working with Multiple Environments
ASPNETCORE_ENVIRONMENT:Development, Staging, and Production
env.IsEnvironment("environmentname")
or
env.EnvironmentName == "Development"
Startup{EnvironmentName} (for example StartupDevelopment)
Configure{EnvironmentName}() ConfigureDevelopment()
Configure{EnvironmentName}Services(). ConfigureDevelopmentServices()