Net6 DI源码分析Part4 CallSiteFactory ServiceCallSite
Net6 CallSiteFactory ServiceCallSite, CallSiteChain
abstract class ServiceCallSite
ServiceCallSite是个抽象类,实现ConstantCallSite、ConstructorCallSite、 FactoryCallSite、ServiceProviderCallSite、IEnumerableCallSite
ServiceCallSite对一个服务的描述,CallSiteFactory提供了它的构建。engine就是根据此类的描述来进行创建对应的服务。
public abstract Type ServiceType { get; }
public abstract Type? ImplementationType { get; }
public abstract CallSiteKind Kind { get; }
public ResultCache Cache { get; }
public object? Value { get; set; }
public bool CaptureDisposable =>
CallSiteFactory
可以说整个DI中它最忙了。它的作用是为engine提供一个描述服务信息的ServiceCallSite对象。
CallSiteFactory(ICollection descriptors)
- 创建了个堆栈卫士
- copy一份ICollection给_descriptors
- 调用Populate整理出Dictionary<Type, ServiceDescriptorCacheItem> _descriptorLookup
Populate()
- 做了一系列验证并把验证后的数据添加到Dictionary<Type, ServiceDescriptorCacheItem> _descriptorLookup
ServiceCallSite? GetCallSite(Type serviceType, CallSiteChain callSiteChain)
- 先去缓存拿_callSiteCache.TryGetValue 如果又直接返回。
- 走CreateCallSite流程
- 会走堆栈卫士
- CreateCallSite会为每个type类型做个锁
- callSiteChain.CheckCircularDependency(serviceType); 参考CallSiteChain
- 创建ServiceCallSite
- TryCreateExact // 普通的
- TryCreateOpenGeneric// 泛型的
- TryCreateEnumerable // 同一个类型注册了多个的。
ServiceCallSite? GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
- 此方法是个重载方法,其内部直接调用TryCreateExact(TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, int slot))与另外一个
GetCallSite相比,此方法跳过了堆栈卫士,以及锁,和先从缓存_callSiteCache拿的过程。所以速度更快。也仅用于初始化验证使用,但是TryCreateExact内部调用链中也会调用 CreateArgumentCallSites这样就又使用到第一个重载方法了。也会从新走堆栈卫士等等一系列的操作。算是相对优化。
- 此方法是个重载方法,其内部直接调用TryCreateExact(TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, int slot))与另外一个
ServiceCallSite? TryCreateExact(Type serviceType, CallSiteChain callSiteChain)
- 拿到对应的ServiceDescriptor交给重载方法。
ServiceCallSite? TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, int slot)
根据ServiceDescriptor创建CallSite(ConstantCallSite/ImplementationFactory/ ConstructorCallSite)用户就涉及到这三种类型。其余的是系统用的。
if (descriptor.ImplementationInstance != null)
{
callSite = new ConstantCallSite(descriptor.ServiceType, descriptor.ImplementationInstance);
}
else if (descriptor.ImplementationFactory != null)
{
callSite = new FactoryCallSite(lifetime, descriptor.ServiceType, descriptor.ImplementationFactory);
}
else if (descriptor.ImplementationType != null)
{
callSite = CreateConstructorCallSite(lifetime, descriptor.ServiceType, descriptor.ImplementationType, callSiteChain);
}
主要是CreateConstructorCallSite的创建比较特殊。。
- 调用CreateConstructorCallSite -> CreateArgumentCallSites -> GetCallSite -> 递归回TryCreateExact的调用;
缓存并返回
CreateConstructorCallSite
准备出一个需要DI“new”出来的一个实例的ConstructorCallSite对象,- 先确认是否有可以用的构造函数。没有肯定抛错误了
- 如果只有一个构造函数,且没有参数,拿就简单了直接new 一个ConstructorCallSite返回
- 如果只有一个构造函数,有构造参数,获取参数列表并调用 CreateArgumentCallSites 且内部递归调用回GetCallSite
- 如果有多个构造参数。找出一个合适的构造函数
- 根据构造函数的参数数量做了个降序
Array.Sort(constructors,(a, b) => b.GetParameters().Length.CompareTo(a.GetParameters().Length)); - 参数最多的构造函数
- 校验其余构造函数参数内是否有被选构造函数没有的参数,如果存在没有的参数就抛异常。
- 根据构造函数的参数数量做了个降序
ServiceCallSite[]? CreateArgumentCallSites(
Type implementationType,
CallSiteChain callSiteChain,
ParameterInfo[] parameters,
bool throwIfCallSiteNotFound)- 内部循环递归调用GetCallSite(parameterType, callSiteChain);并创建一个ServiceCallSite[]返回。
CallSiteChain
它的出现就是为了防止构造函数形式提供服务时循环依赖项 比如服务A的构造方法依赖服务B,服务B的构造方法又依赖函数A。
以下为两个关键节点。
- CreateCallSite时先确认 CreateCallSite -> callSiteChain.CheckCircularDependency()
- 以构造方法模式创建对象时进行累加。CreateConstructorCallSite => callSiteChain.Add(serviceType, implementationType)
伪代码调用过着如下
1. CallSiteFactory.CreateCallSite(serviceType, new callSiteChain()) => callSiteChain.CheckCircularDependency(); => GetCallSite();
GetCallSite => TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, int slot) => CreateConstructorCallSite
CreateConstructorCallSite => callSiteChain.Add(serviceType, implementationType) ; CreateArgumentCallSites;
CreateArgumentCallSites => 递归回GetCallSite();
CallSiteKind
描述CallSiteService类别的枚举
Factory,Constructor,Constant,IEnumerable,ServiceProvider,
在CallSiteVisitor.VisitCallSiteMain根据此枚举去调用对应创建实例的Visit方法 如VisitFactory/VisitIEnumerable/VisitConstructor/VisitConstant/ VisitServiceProvider/
CallSiteResultCacheLocation
描述创建好的服务缓存位子,也对应着服务注册时的生命周期。。
在CallSiteVisitor->VisitCallSite根据此枚举去调用不同的VisitCache 如VisitRootCache/VisitScopeCache
switch (lifetime) { case ServiceLifetime.Singleton: Location = CallSiteResultCacheLocation.Root; break; case ServiceLifetime.Scoped: Location = CallSiteResultCacheLocation.Scope; break; case ServiceLifetime.Transient: Location = CallSiteResultCacheLocation.Dispose; break; default: Location = CallSiteResultCacheLocation.None; break; }
ResultCache
用来描述CallSiteService 对ServiceCacheKey/CallSiteResultCacheLocation 的封装。
internal struct ResultCache
{
public ResultCache(ServiceLifetime lifetime, Type type, int slot)
{
switch (lifetime)
{
case ServiceLifetime.Singleton:
Location = CallSiteResultCacheLocation.Root;
break;
case ServiceLifetime.Scoped:
Location = CallSiteResultCacheLocation.Scope;
break;
case ServiceLifetime.Transient:
Location = CallSiteResultCacheLocation.Dispose;
break;
default:
Location = CallSiteResultCacheLocation.None;
break;
}
Key = new ServiceCacheKey(type, slot);
}
public CallSiteResultCacheLocation Location { get; set; }
public ServiceCacheKey Key { get; set; }
}
ServiceCacheKey
1. CallSiteFacotry根据此类型去缓存CallSiteServie。
2. ServiceProviderEngineScope根据此类型去缓存 ResolvedServices
CallSiteValidator
//以下没啥好说的。
ConstantCallSite
ConstructorCallSite
FactoryCallSite
IEnumerableCallSite
ServiceProviderCallSite
Net6 DI源码分析Part4 CallSiteFactory ServiceCallSite的更多相关文章
- Net6 DI源码分析Part5 在Kestrel内Di Scope生命周期是如何根据请求走的?
Net6 DI源码分析Part5 在Kestrel内Di Scope生命周期是如何根据请求走的? 在asp.net core中的DI生命周期有一个Scoped是根据请求走的,也就是说在处理一次请求时, ...
- Net6 DI源码分析Part2 Engine,ServiceProvider
ServiceProvider ServiceProvider是对IServiceProvider实现,它有一个internal的访问修饰符描述的构造,并需要两个参数IServiceCollectio ...
- Net6 DI源码分析Part1 ServiceCollection、ServiceDescriptor、ServiceLifetime、IServiceProvider
ServiceCollection.ServiceDescriptor.ServiceLifetime.IServiceProvider Microsoft.Extensions.Dependency ...
- Net6 DI源码分析Part3 CallSiteRuntimeResolver,CallSiteVisitor
CallSiteRuntimeResolver CallSiteRuntimeResolver是实现了CallSiteVisitor之一. 提供的方法主要分三个部分 自有成员方法 Resolve提供服 ...
- 小白都能看懂的 Spring 源码揭秘之依赖注入(DI)源码分析
目录 前言 依赖注入的入口方法 依赖注入流程分析 AbstractBeanFactory#getBean AbstractBeanFactory#doGetBean AbstractAutowireC ...
- Net6 Configuration & Options 源码分析 Part2 Options
Net6 Configuration & Options 源码分析 Part2 Options 第二部分主要记录Options 模型 OptionsConfigurationServiceCo ...
- 一个由正则表达式引发的血案 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. 一些特殊字符,如“&”,“- ...
- Net6 Configuration & Options 源码分析 Part1
Net6 Configuration & Options 源码分析 Part1 在Net6中配置系统一共由两个部分组成Options 模型与配置系统.它们是两个完全独立的系统. 第一部分主要记 ...
- .net core 轻量级容器 ServiceProvider 源码分析
首先看 ServiceCollection 的定义 //定义 public class ServiceCollection : IServiceCollection { private readonl ...
随机推荐
- 记一次log4j2引发的渗透测试
前言 记一次log4j2打入内网并用CVE-2021-42287.CVE-2021-42278获取到DC权限的靶场渗透. 外网打点 首先对web进行端口扫描,发现38080端口和22端口 访问一下38 ...
- 一键抠除路人甲,昇腾CANN带你识破神秘的“AI消除术”
摘要:都说人工智能改变了生活,你感觉到了么?AI的魔力就在你抠去路人甲的一瞬间来到了你身边.今天就跟大家聊聊--神秘的"AI消除术". 引语 旅途归来,重温美好却被秀丽河山前的路人 ...
- 初识python 之 爬虫:爬取某网站的壁纸图片
用到的主要知识点:requests.get 获取网页HTMLetree.HTML 使用lxml解析器解析网页xpath 使用xpath获取网页标签信息.图片地址request.urlretrieve ...
- Centos7 安装LAMP以及nextcloud
第一步:安装apache 在centos中 apache叫httpd yum update #更新源 yum install httpd #安装apache systemctl stop firewa ...
- 总结关于spring security 使用 JWT 和 账户密码登录 整合在一起的新感悟
(1)jwt登录拦截,需要在账户密码认证之前进行jwt认证,因此jwt拦截需要在 UsernamePasswordAuthenticationFilter 之前: (2)jwt验证通过则不需要执行账户 ...
- Java语言学习案例雷霆战机
1.Java雷霆战机学习笔记(一)-资源加载 https://www.toutiao.com/i6631331313259381255/ 2.Java雷霆战机学习笔记(二)-音乐播放 https:// ...
- Unity3D开发入门教程(三)——添加启动脚本
五邑隐侠,本名关健昌,12年游戏生涯. 本教程以 Unity 3D + VS Code + C# + tolua 为例. 一.启动脚本 第一篇 "搭建开发环境",在 "配 ...
- 手写Webserver
一.反射 反射Reflection:把java类中的各种结构(方法.属性.构造器.类名)映射成一个个的java对象.利用反射技术可以对一个类进行解剖,反射是框架设计的灵魂 //在运行期间,一个类,只有 ...
- Rust学习(一)
为什么学习Rust 最近在看Linux相关新闻的时候,看到了Linux内核正在将Rust集成至内核内的消息,且越来越多的嵌入式开发可以使用Rust编程.以往笔者的技术栈只有 C语言 ,C++也只是浅尝 ...
- sort排序出现安卓与苹果系统排序不一致问题
sort排序出现安卓与苹果系统排序不一致问题