首先谈一下IServiceProvider

  IServiceProvider只提供给了一个根据类型获取对象的功能,试想一下IOC总得有一个找到对象,具体如下

public interface IServiceProvider
{
  object GetService(Type serviceType);
}

  再谈一下ServiceProvider

  ServiceProvider实例化了IServiceProvider,也就是实现了GetService,用于获取容器中实际对象,但是它是借助于IServiceProviderEngine(这个接口稍后再介绍),具体如下:

public object GetService(Type serviceType)
{
return this._engine.GetService(serviceType);
}

  另外ServiceCollection中有一个方法BuildServiceProvider(存在于类ServiceCollectionContainerBuilderExtensions中),返回值对象类型就是ServiceProvider

/// <summary>
/// Extension methods for building a <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceProvider" /> from an <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />.
/// </summary>
public static class ServiceCollectionContainerBuilderExtensions
{
/// <summary>
/// Creates a <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceProvider" /> containing services from the provided <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> containing service descriptors.</param>
/// <returns>The <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceProvider" />.</returns>
public static ServiceProvider BuildServiceProvider(
this IServiceCollection services)
{
return services.BuildServiceProvider(ServiceProviderOptions.Default);
} /// <summary>
/// Creates a <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceProvider" /> containing services from the provided <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />
/// optionaly enabling scope validation.
/// </summary>
/// <param name="services">The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> containing service descriptors.</param>
/// <param name="validateScopes">
/// <c>true</c> to perform check verifying that scoped services never gets resolved from root provider; otherwise <c>false</c>.
/// </param>
/// <returns>The <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceProvider" />.</returns>
public static ServiceProvider BuildServiceProvider(
this IServiceCollection services,
bool validateScopes)
{
return services.BuildServiceProvider(new ServiceProviderOptions()
{
ValidateScopes = validateScopes
});
} /// <summary>
/// Creates a <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceProvider" /> containing services from the provided <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />
/// optionaly enabling scope validation.
/// </summary>
/// <param name="services">The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> containing service descriptors.</param>
/// <param name="options">
/// Configures various service provider behaviors.
/// </param>
/// <returns>The <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceProvider" />.</returns>
public static ServiceProvider BuildServiceProvider(
this IServiceCollection services,
ServiceProviderOptions options)
{
if (services == null)
throw new ArgumentNullException(nameof (services));
if (options == null)
throw new ArgumentNullException(nameof (options));
return new ServiceProvider((IEnumerable<ServiceDescriptor>) services, options);
}
}

  必须要结合一下IServiceProviderEngine、ServiceProviderEngineServiceProviderEngineScope对象才能具体知道ServiceProvider是怎么获取IOC容器中的对象了

  在使用BuildServiceProvider的时候,最终会调用new ServiceProvider((IEnumerable<ServiceDescriptor>)services,options),然后就是调用如下函数:

internal ServiceProvider(
IEnumerable<ServiceDescriptor> serviceDescriptors,
ServiceProviderOptions options)
{
IServiceProviderEngineCallback callback = (IServiceProviderEngineCallback) null;
if (options.ValidateScopes)
{
callback = (IServiceProviderEngineCallback) this;
this._callSiteValidator = new CallSiteValidator();
}
switch (options.Mode)
{
case ServiceProviderMode.Dynamic:
this._engine = (IServiceProviderEngine) new DynamicServiceProviderEngine(serviceDescriptors, callback);
break;
case ServiceProviderMode.Runtime:
this._engine = (IServiceProviderEngine) new RuntimeServiceProviderEngine(serviceDescriptors, callback);
break;
case ServiceProviderMode.Expressions:
this._engine = (IServiceProviderEngine) new ExpressionsServiceProviderEngine(serviceDescriptors, callback);
break;
case ServiceProviderMode.ILEmit:
this._engine = (IServiceProviderEngine) new ILEmitServiceProviderEngine(serviceDescriptors, callback);
break;
default:
throw new NotSupportedException("Mode");
}
}

  就是说最终还是根据ServiceProviderMode来判断需要实例化哪种ServiceProviderEngine,现在简单举例DynamicServiceProviderEngine。DynamicServiceProviderEngine最终还是继承ServiceProviderEngine,

所以上面说的GetService其实就是调用了ServiceProviderEngine的GetService

public object GetService(Type serviceType)
{
return this.GetService(serviceType, this.Root);
}

  其中this.Root就是实例化的ServiceProviderScope。上面的this.GetService(serviceType,this.Root)会调用如下函数:

internal object GetService(
Type serviceType,
ServiceProviderEngineScope serviceProviderEngineScope)
{
if (this._disposed)
ThrowHelper.ThrowObjectDisposedException();
Func<ServiceProviderEngineScope, object> orAdd = this.RealizedServices.GetOrAdd(serviceType, this._createServiceAccessor);
this._callback?.OnResolve(serviceType, (IServiceScope) serviceProviderEngineScope);
ServiceProviderEngineScope providerEngineScope = serviceProviderEngineScope;
return orAdd(providerEngineScope);
}

  最终就是调用Func<ServiceProviderEngineScope, object> orAdd = this.RealizedServices.GetOrAdd(serviceType, this._createServiceAccessor);其中RealizedServices是ConcurrentDictionary<Type, Func<ServiceProviderEngineScope, object>> RealizedServices { get; },this._createServiceAccessor是Func<ServiceProviderEngineScope, object>,通过这个委托根据ServiceProviderEngineScope获取最终的对象,也就是说,如果服务存在的话就从字典中获取,不存在就添加到队列上,并直接返回。另外在实例化ServiceProviderEngine的时候实例化了this._createServiceAccessor = new Func<Type, Func<ServiceProviderEngineScope, object>>(this.CreateServiceAccessor);其中CreateServiceAccessor是一个函数,定义如下:

private Func<ServiceProviderEngineScope, object> CreateServiceAccessor(
Type serviceType)
{
IServiceCallSite callSite = this.CallSiteFactory.CreateCallSite(serviceType, new CallSiteChain());
if (callSite == null)
return (Func<ServiceProviderEngineScope, object>) (_ => (object) null);
this._callback?.OnCreate(callSite);
return this.RealizeService(callSite);
}

  就DynamicServiceProviderEngine而言,RealizeService的定义如下:

protected override Func<ServiceProviderEngineScope, object> RealizeService(
IServiceCallSite callSite)
{
int callCount = 0;
return (Func<ServiceProviderEngineScope, object>) (scope =>
{
if (Interlocked.Increment(ref callCount) == 2)
Task.Run<Func<ServiceProviderEngineScope, object>>((Func<Func<ServiceProviderEngineScope, object>>) (() => base.RealizeService(callSite)));
return this.RuntimeResolver.Resolve(callSite, scope);
});
}

  RuntimeResolver是ServiceProviderEngine的一个属性。到此为止,IOC容器的数据就可以获取到了。下篇博客会继续说明this.RuntimeResover.Resolve下的事情。

DOTNET CORE源码分析之IServiceProvider、ServiceProvider、IServiceProviderEngine、ServiceProviderEngine和ServiceProviderEngineScope的更多相关文章

  1. DOTNET CORE源码分析之IOC容器结果获取内容补充

    补充一下ServiceProvider的内容 可能上一篇文章DOTNET CORE源码分析之IServiceProvider.ServiceProvider.IServiceProviderEngin ...

  2. DOTNET CORE源码分析之ServiceDescriptor

    ServiceDescriptor在.net core中的作用就是DI中注入服务元素的描述.每一个元素核心内容部分包括需要注入的服务元素的类型ServiceType,它对应的接口(如果有的话)Impl ...

  3. ASP.NET Core[源码分析篇] - WebHost

    _configureServicesDelegates的承接 在[ASP.NET Core[源码分析篇] - Startup]这篇文章中,我们得知了目前为止(UseStartup),所有的动作都是在_ ...

  4. 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器

    1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...

  5. ASP.NET Core[源码分析篇] - Authentication认证

    原文:ASP.NET Core[源码分析篇] - Authentication认证 追本溯源,从使用开始 首先看一下我们通常是如何使用微软自带的认证,一般在Startup里面配置我们所需的依赖认证服务 ...

  6. ASP.NET Core[源码分析篇] - Startup

    应用启动的重要类 - Startup 在ASP.NET Core - 从Program和Startup开始这篇文章里面,我们知道了Startup这个类的重要性,它主要负责了: 配置应用需要的服务(服务 ...

  7. [asp.net core 源码分析] 01 - Session

    1.Session文档介绍 毋庸置疑学习.Net core最好的方法之一就是学习微软.Net core的官方文档:https://docs.microsoft.com/zh-cn/aspnet/cor ...

  8. ASP.NET Core[源码分析篇] - 认证

    追本溯源,从使用开始 首先看一下我们的通常是如何使用微软自带的认证,一般在Startup里面配置我们所需的依赖认证服务,这里通过JWT的认证方式讲解 public void ConfigureServ ...

  9. EF Core 源码分析

    最近在接触DDD+micro service来开发项目,因为EF Core太适合DDD模式需要的ORM设计,所以这篇博客是从代码角度去理解EF core的内部实现,希望大家能从其中学到一些心得体会去更 ...

随机推荐

  1. Trailing Zeroes (III) LightOJ - 1138 二分+找规律

    Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! ...

  2. hql错误:No data type for node: org.hibernate.hql.ast.tree.IdentNode

    今天写了一个查询,用的是hql,数据库是mysql.多表联查,结果报错了报: \-[IDENT] IdentNode: 'routerNumber' {originalText=routerNumbe ...

  3. MUI 提问框多个按钮的回调函数

    var btns = new Array("按钮1", "按钮2"); mui.confirm("这是信息", "这是标题&quo ...

  4. centos7 上安装jira调试系统

    安装mysql数据库 在windows上面下载 http://dev.mysql.com/downloads/mysql/ 在liunx系统上可直接wget, wget https://dev.mys ...

  5. python 学员管理系统

    需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上课纪录对应多条学员的 ...

  6. Y事业部打造一体化质量管理平台

    互联网企业质量管理的困惑 作为互联网时代的互联网企业,我们的研发模式和传统模式相比,最显著的不同在于发布节奏加快了,这个加快不是快了10%,20%,50%,而是加快了几倍,甚至几十倍,上百倍.面对加快 ...

  7. 理解 LinkedList

    java -version :jdk 1.8.0_191 构造 类内参数,方法 实现 基于双向链表实现. 插入时间复杂度 O(1) 查找时间复杂度 O(n) 删除时间复杂度 O(1) 修改时间复杂度 ...

  8. React Native Build Apk

    1 React Native安卓项目打包APK 1.1 产生签名的key 先通过keytool生成key 1 keytool -genkey -v -keystore demo-release-key ...

  9. css雪碧图压缩

    cssgaga下载地址 链接: https://pan.baidu.com/s/1Q9xH_XzumIc7vTLCZ3tr5A 提取码: stqe CssGaga功能特性 合并import的CSS文件 ...

  10. mysql in与exists区别

    1.exists是对外表做loop循环,每次loop循环再对内表(子查询)进行查询,那么因为对内表的查询使用的索引(内表效率高,故可用大表),而外表有多大都需要遍历,不可避免(尽量用小表),故内表大的 ...