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 @ 2022-10-17 21:27  孤海飞雁  阅读(61)  评论(0)    收藏  举报