IOC 生命周期-服务范围

复制代码
public class ServicesPrpvoder:IServicesPrpvoder  服务提供者
可以利用IserviceScoprFactory 创建一个 服务范围 IServiceScope对象

IServicesScope 的包含IServicesPrpvoder
任何一个 IServiceProvider都具有对跟容器的引用

源码

public interface IServiceScope : IDisposable
{
/// <summary>
/// The <see cref="System.IServiceProvider"/> used to resolve dependencies from the scope.
/// </summary>
IServiceProvider ServiceProvider { get; }
}

public interface IServiceScopeFactory
{
/// <summary>
/// Create an <see cref="Microsoft.Extensions.DependencyInjection.IServiceScope"/> which
/// contains an <see cref="System.IServiceProvider"/> used to resolve dependencies from a
/// newly created scope.
/// </summary>
/// <returns>
/// An <see cref="Microsoft.Extensions.DependencyInjection.IServiceScope"/> controlling the
/// lifetime of the scope. Once this is disposed, any scoped services that have been resolved
/// from the <see cref="Microsoft.Extensions.DependencyInjection.IServiceScope.ServiceProvider"/>
/// will also be disposed.
/// </returns>
IServiceScope CreateScope();
}



ServiceProviderServiceExtensions.cs

public static IServiceScope CreateScope(this IServiceProvider provider)
{
  return provider.GetRequiredService<IServiceScopeFactory>().CreateScope();
}

复制代码

 

 

posted @   孤海飞雁  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示