Microsoft.Extensions.DependencyInjection入门

转载:https://www.cnblogs.com/zcqiand/p/14257661.html

Microsoft.Extensions.DependencyInjection入门

1 前置阅读

在阅读本文章之前,你可以先阅读:

什么是依赖注入

2 简介

Microsoft.Extensions.DependencyInjection是.NET Core内置依赖注入模块。

3 使用

首先,在Startup.ConfigureServices方法中,将Knife,Actor注册到服务容器中。
public void ConfigureServices(IServiceCollection services)
{
	services.AddTransient<Actor>();
	services.AddTransient<Knife>();
	
	services.AddControllers();
	services.AddSwaggerGen(c=>
	{
		c.SwaggerDoc("v1",new OpenApiInfo{Title="Example.DependencyInjection.WebApi",Version="v1"});
	});
}

然后,增加HomeController,执行actor.Kill。

using Microsoft.AspNetCore.Mvc;
using System;

namespace Autofac.WebApi.Controllers
{
	[Route("[controller]")]
	[ApiController]
	public class HomeController:Controller
	{
		private readonly Actor actor;
		public HomeController(Actor actor);
		{
			this.actor = actor??throw new ArgumnetNullException(nameof(actor));
		}
		
		[HttpGet]
		public string Get()
		{
			return actor.Kill();
		}
	}
}

启动调试,让我们来看看输出结果:
小明用刀杀怪

4 生命周期

  • 单例 Singleton:依赖项注入容器对服务实现的每个后续请求都使用相同的实例。
  • 使用域 Scopped:对于Web应用程序,作用域范围内的生存期表示每个客户端请求(连接)都会
    创建一次服务。
  • 瞬时(暂时)Transient:每次从服务容器中请求时,都会创建瞬态生存期服务。

首先,分别建三个代表生命周期的类:MySingletonService、MyScopedService、MyTransientService。

namespace Example.DependencyInjection.WebApi
{
	pubklic class MySingletonService
	{
	}
	public class MyScopedService
	{
	}
	public class MyTransientService 
	{
	}
}

然后,在Startup.ConfigureServices方法中,将MySingletonService、MyScopedService、MyTransientService注册
到服务容器中。

serivce.AddSingleton<MySingletonService,MySingletonService>();
serivce.AddScoped<MyScopedService,MyScopedService>();
service.AddTransient<MyTransientService,MyTransientService>();

最后HomeController增加GetServiceLifetime方法。

[Routed("ServiceLifetime")]
[HttpGet]
public List<string> GetServiceLifetime([FromServices] MySingletonService singleton1,
[FromServices] MySingletonService singleton2,
[FromServices] MyScopedService scoped1,[FromServices] MyScopedService scoped2,
[FromServices] MyTransientService transient1,[FromServices] MyTransientService transient2)
{
	var s =new List<string>();
    s.Add($"singleton1:{singleton1.GetHashCode()}");
    s.Add($"singleton2:{singleton2.GetHashCode()}");
    s.Add($"scoped1:{scoped1.GetHashCode()}");
    s.Add($"scoped2:{scoped2.GetHashCode()}");
    s.Add($"transient1:{transient1.GetHashCode()}");
    s.Add($"transient2:{transient2.GetHashCode()}");
    return s;
}

启动调试,执行两遍,让我们来看看输出结果:
第一遍:

[
  "singleton1:65122748",
  "singleton2:65122748",
  "scoped1:52786977",
  "scoped2:52786977",
  "transient1:16782441",
  "transient2:16991442"
]

第二遍:

[
  "singleton1:65122748",
  "singleton2:65122748",
  "scoped1:56140151",
  "scoped2:56140151",
  "transient1:1997173",
  "transient2:54718731"
]

从结果我们发现:

  • 单例:Singleton 两次的HashCode没有变化
  • 作用域:Scoped 每个请求内的HashCode是相同的,不同请求的HashCode是不同的
  • 瞬时(暂时):Transient每次的HashCode都不相同
    注意例子中,我们使用通过[FromServices]注入的,另外我们也可以选择通过Controller构造函数注入,这是在Controller中有两种依赖注入的实例的获取方式。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值