重新整理.net core 计1400篇[九] (.net core 中的依赖注入的服务注入)

前言

在该系列六中介绍了一个简单的依赖注入,该节介绍.net core 中的依赖注入的服务注入。

ServiceDescriptor

ServiceDescriptor 是服务描述的意思,这个是做什么的呢?

我们知道当我们要实例化一个服务的时候,我们通过serviceType 属性去查找,是否我们注册了。

那么通过serviceType 查找到的是什么呢?就是这个ServiceDescriptor,好了那么分析一下这个serviceDescriptor 到底是什么吧。

来看一下又什么属性吧!

public ServiceLifetime Lifetime
{
	[CompilerGenerated]
	get;
}

/// <inheritdoc />
public Type ServiceType
{
	[CompilerGenerated]
	get;
}

/// <inheritdoc />
public Type ImplementationType
{
	[CompilerGenerated]
	get;
}

/// <inheritdoc />
public object ImplementationInstance
{
	[CompilerGenerated]
	get;
}

/// <inheritdoc />
public Func<IServiceProvider, object> ImplementationFactory
{
	[CompilerGenerated]
	get;
}

下面来介绍一下属性。

Lifetime 这个我想不用介绍了吧,生命周期。

public enum ServiceLifetime
{
	/// <summary>
	/// Specifies that a single instance of the service will be created.
	/// </summary>
	Singleton,
	/// <summary>
	/// Specifies that a new instance of the service will be created for each scope.
	/// </summary>
	/// <remarks>
	/// In ASP.NET Core applications a scope is created around each server request.
	/// </remarks>
	Scoped,
	/// <summary>
	/// Specifies that a new instance of the service will be created every time it is requested.
	/// </summary>
	Transient
}

ServiceType,我想我也不用介绍的,就是当前注入类型,比如说IFoo

然后呢,下面三种属性体现了,提供服务实例的三种方式:

/// <inheritdoc />
public Type ImplementationType
{
	[CompilerGenerated]
	get;
}

/// <inheritdoc />
public object ImplementationInstance
{
	[CompilerGenerated]
	get;
}

/// <inheritdoc />
public Func<IServiceProvider, object> ImplementationFactory
{
	[CompilerGenerated]
	get;
}

这个需要我们来看下这个的时候ServiceDescriptor的构造函数。

/// <summary>
/// Initializes a new instance of <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceDescriptor" /> with the specified <paramref name="implementationType" />.
/// </summary>
/// <param name="serviceType">The <see cref="T:System.Type" /> of the service.</param>
/// <param name="implementationType">The <see cref="T:System.Type" /> implementing the service.</param>
/// <param name="lifetime">The <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceLifetime" /> of the service.</param>
public ServiceDescriptor(Type serviceType, Type implementationType, ServiceLifetime lifetime)
	: this(serviceType, lifetime)
{
	//IL_0016: Unknown result type (might be due to invalid IL or missing references)
	//IL_002a: Unknown result type (might be due to invalid IL or missing references)
	if (serviceType == (Type)null)
	{
		throw new ArgumentNullException("serviceType");
	}
	if (implementationType == (Type)null)
	{
		throw new ArgumentNullException("implementationType");
	}
	ImplementationType = implementationType;
}

/// <summary>
/// Initializes a new instance of <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceDescriptor" /> with the specified <paramref name="instance" />
/// as a <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton" />.
/// </summary>
/// <param name="serviceType">The <see cref="T:System.Type" /> of the service.</param>
/// <param name="instance">The instance implementing the service.</param>
public ServiceDescriptor(Type serviceType, object instance)
	: this(serviceType, ServiceLifetime.Singleton)
{
	//IL_0016: Unknown result type (might be due to invalid IL or missing references)
	//IL_0024: Unknown result type (might be due to invalid IL or missing references)
	if (serviceType == (Type)null)
	{
		throw new ArgumentNullException("serviceType");
	}
	if (instance == null)
	{
		throw new ArgumentNullException("instance");
	}
	ImplementationInstance = instance;
}

/// <summary>
/// Initializes a new instance of <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceDescriptor" /> with the specified <paramref name="factory" />.
/// </summary>
/// <param name="serviceType">The <see cref="T:System.Type" /> of the service.</param>
/// <param name="factory">A factory used for creating service instances.</param>
/// <param name="lifetime">The <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceLifetime" /> of the service.</param>
public ServiceDescriptor(Type serviceType, Func<IServiceProvider, object> factory, ServiceLifetime lifetime)
	: this(serviceType, lifetime)
{
	//IL_0016: Unknown result type (might be due to invalid IL or missing references)
	//IL_0024: Unknown result type (might be due to invalid IL or missing references)
	if (serviceType == (Type)null)
	{
		throw new ArgumentNullException("serviceType");
	}
	if (factory == null)
	{
		throw new ArgumentNullException("factory");
	}
	ImplementationFactory = factory;
}

我们看到了这三个构造方法,那么我们基本明白了,到时候创建服务实例的时候就是这几种方法了。

这里面有一个值得注意的地方,就是:

public ServiceDescriptor(Type serviceType, object instance)
	: this(serviceType, ServiceLifetime.Singleton)
{
	//IL_0016: Unknown result type (might be due to invalid IL or missing references)
	//IL_0024: Unknown result type (might be due to invalid IL or missing references)
	if (serviceType == (Type)null)
	{
		throw new ArgumentNullException("serviceType");
	}
	if (instance == null)
	{
		throw new ArgumentNullException("instance");
	}
	ImplementationInstance = instance;
}

也就是说我们可以直接让接口对应实例,比如说:new ServiceDescriptor(IFoo,new Foo());默认的生命周期模式就是:ServiceLifetime.Singleton

IServiceCollection

依赖存储框架将服务注册存储在一个通过IServiceCollection 接口表示的集合中。

public interface IServiceCollection : IList<ServiceDescriptor>, ICollection<ServiceDescriptor>, IEnumerable<ServiceDescriptor>, IEnumerable```
{
}

在 IServiceCollection 中,本质上其实就是一个ServiceDescriptor集合。

那么来看下IServiceCollection的实例对象ServiceCollection的扩展方法ServiceCollectionServiceExtensions。

private static IServiceCollection Add(IServiceCollection collection, Type serviceType, Type implementationType, ServiceLifetime lifetime)
{
	ServiceDescriptor serviceDescriptor = new ServiceDescriptor(serviceType, implementationType, lifetime);
	((ICollection<ServiceDescriptor>)collection).Add(serviceDescriptor);
	return collection;
}

private static IServiceCollection Add(IServiceCollection collection, Type serviceType, Func<IServiceProvider, object> implementationFactory, ServiceLifetime lifetime)
{
	ServiceDescriptor serviceDescriptor = new ServiceDescriptor(serviceType, implementationFactory, lifetime);
	((ICollection<ServiceDescriptor>)collection).Add(serviceDescriptor);
	return collection;
}

可以帮助我们直接创建ServiceDescriptor,并且加入到当前的ServiceCollection 中。

然后ServiceCollectionServiceExtensions 在3.0在还更新了,tryadd,因为我们一个ServiceType 可以对应多个,使用tryadd 的时候,就会尝试加入,如果有的话就不会加入。

在ServiceCollection和ServiceCollectionServiceExtensions 有一些其他的方法,最好去看一下源码,了解一下。

总结

下一章依赖注入的服务消费

原文地址:https://www.cnblogs.com/aoximin/p/12929072.html