zoukankan      html  css  js  c++  java
  • DotNETCore 学习笔记 依赖注入和多环境

    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()
  • 相关阅读:
    王歆瑶20191128-1 总结
    王歆瑶 20191121-1 每周例行报告
    王歆瑶20191114-1 每周例行报告
    王歆瑶20191107-1 每周例行报告
    王歆瑶20191031-1 每周例行报告
    王歆瑶20191024-1 每周例行报告
    王歆瑶20191017-1 每周例行报告
    王歆瑶20191010-2 每周例行报告
    王歆瑶20190919-4 单元测试,结对
    LeetCode 11 盛水最多的容器
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/5872109.html
Copyright © 2011-2022 走看看