ASP.NET Core MVC 2.依赖注入 IoC(控制反转)容器

依赖注入,IoC 容器

依赖注入的生命周期
• Transient:每次被请求都会创建新的实例 (生命周期短)
• Scoped:每次 Web 请求会创建一个实例 (web请求完毕就销毁)
• Singleton:一旦被创建实例,就会一直使用这个实例 (直到应用停止)

Startup 类的 ConfigureServices 方法里面进行依赖注入配置。

public void ConfigureServices(IServiceCollection services)
{
    // 每当有其他类请求 ICinemaService 接口时,容器都会返回 CinemaMemoryService 的实例
    services.AddSingleton<ICinemaService, CinemaMemoryService>();
    services.AddSingleton<IMovieService, MovieMemoryService>();
}
依赖注入的好处

1.不用去管生命周期(容器统一管理,省去复杂性)
2.互相请求的都是借口,类型之间没有依赖。有利于单元测试



管道与中间件

Startup 类的 Configure 方法里面进行管道和中间件的配置



管道示意图






完整的Web请求的流程


PS: IIS(反向代理服务器)去寻找.net core(CLR) => CLR到 WEB应用里去找main方法 => 启动内置服务器(Kestrel) => kestrel 将请求发送到管道里 => 运行中间件后原路返回
中间件不运行第二个解决方法
            //这样就可以运行那个下一个了
            app.Use(async (context,next) =>
            {
                await context.Response.WriteAsync("Hello World!");
                await next();
            });

            app.Use(async (context,next) =>
            {
                await context.Response.WriteAsync("Another Hello");
                await next();
            });
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Another Hello");
            });

使用控制台查看运行信息(configure 添加ILogger logger)

 public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILogger<Startup> logger)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //这样就可以运行那个下一个了
            app.Use(async (context,next) =>
            {
                logger.LogInformation("T1");
                await context.Response.WriteAsync("Hello World!");
                await next();
                logger.LogInformation("T1End");
            });

            app.Use(async (context,next) =>
            {
                logger.LogInformation("T2");
                await context.Response.WriteAsync("Another Hello");
                await next();
                logger.LogInformation("T2End");
            });
            app.Run(async (context) =>
            {
                logger.LogInformation("T3");
                await context.Response.WriteAsync("Another Hello");
                logger.LogInformation("T3End");
            });
        }

修改启动配置文件 launchSettings.json 只留下控制台启动方式

{
    "WebApplication7": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

需要手动注入Mvc服务

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();//使用该方法后才可以使用app.UseMvc()方法
            services.AddSingleton<Ifactory, Factory>();
            //关联起来
            services.Configure<ConnectionStringOptions>(_configuration.GetSection("ConnectionStrings"));
        }

两种获取依赖注入实例的方法

  1. 通过 Controller 的构造函数(推荐)
  2. [FromServices](服务仅被单一接口使用时推荐该方法)
// Controller
public WeatherForecastController(ILogger<WeatherForecastController> logger, IOrderService orderService, IGenericService<IOrderService> genericService)
{
    _orderService = orderService;
    _logger = logger;
}
// [FromServices]
[HttpGet]
public int GetService([FromServices]IMySingletonService singleton1,
                      [FromServices]IMySingletonService singleton2,
                      [FromServices]IMyTransientService transient1,
                      [FromServices]IMyTransientService transient2,
                      [FromServices]IMyScopedService scoped1,
                      [FromServices]IMyScopedService scoped2)
{
    Console.WriteLine($"singleton1:{singleton1.GetHashCode()}");
    Console.WriteLine($"singleton2:{singleton2.GetHashCode()}");
    Console.WriteLine($"transient1:{transient1.GetHashCode()}");
    Console.WriteLine($"transient2:{transient2.GetHashCode()}");
    Console.WriteLine($"scoped1:{scoped1.GetHashCode()}");
    Console.WriteLine($"scoped2:{scoped2.GetHashCode()}");
    Console.WriteLine($"========请求结束=======");
    return 1;
}
posted @ 2020-10-23 20:47  李花花小番茄  阅读(123)  评论(0编辑  收藏  举报