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()
  • 相关阅读:
    innodb count优化测试
    基于HTML5 Canvas生成粒子效果的人物头像
    基于HTML5 SVG炫酷文字爆炸特效
    一款基于jQuery轮播切换焦点图,可播放多张图片
    基于Bootstrap的jQuery开关按钮组合
    基于jQuery上下切换的焦点图—带缩略图悬浮
    基于HTML5 Canvas实现的图片马赛克模糊特效
    基于jQuery的宽屏可左右切换的焦点图插件
    基于HTML5的捕鱼达人游戏网页版
    基于HTML5实现的中国象棋游戏
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/5872109.html
Copyright © 2011-2022 走看看