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()
  • 相关阅读:
    [2010山东ACM省赛] Balloons(搜索)
    [2010山东ACM省赛] Greatest Number(数的组合+二分搜索)
    [ACM] hdu 1213 How Many Tables(并查集)
    C Shuffle Cards
    快速读入输出模板
    J Distance to Work
    E Sort String
    H Diff-prime Pairs
    A PACM Team
    区间 (interval)
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/5872109.html
Copyright © 2011-2022 走看看