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()
  • 相关阅读:
    解决Win10图片打开方式没有“Windows照片查看器”问题
    20199305 2019-2020-2 《网络攻防实践》第四周作业
    20199305 2019-2020-2 《网络攻防实践》第三周作业
    20199305 2019-2020-2 《网络攻防实践》第二周作业
    20199305 2019-2020-2 《网络攻防实践》第一周作业
    20199305 《网络攻防实践》假期作业
    补交作业:第六周 SumN
    外设驱动程序设计-2
    外设驱动程序设计-1
    2019-2020-1 20199305《Linux内核原理与分析》第十二周作业
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/5872109.html
Copyright © 2011-2022 走看看