重新整理.net core 计1400篇[九] (.net core 中的依赖注入的服务的消费)
2023-09-14 09:01:09 时间
前言
包含服务注册信息IServiceCollection 集合最终被用来创建作为依赖注入容器的IServiceProvider 对象。
当需要创建某个服务实例的时候(服务消费),我们通过指定服务类型调用IServiceProvider 接口GetService 方法即可。
那么来看下和IServiceProvider 相关的东西吧。
IServiceProvider
这里面只有一个提供服务的方法:
/// <summary>Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</summary>
public interface IServiceProvider
{
/// <summary>Gets the service object of the specified type.</summary>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
/// <returns>A service object of type <paramref name="serviceType">serviceType</paramref>.
/// -or-
/// null if there is no service object of type <paramref name="serviceType">serviceType</paramref>.</returns>
object GetService(Type serviceType);
}
在创建IServiceProvider的对象创建上,有三个构造函数:
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" />
/// optionally 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" />
/// optionally 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)
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
if (services == null)
{
throw new ArgumentNullException("services");
}
if (options == null)
{
throw new ArgumentNullException("options");
}
return new ServiceProvider((IEnumerable<ServiceDescriptor>)services, options);
}
}
我们看到无论其如何构造,那么都有一个ServiceProviderOptions配置类。
这个配置我前面8中解释过,就是对注入服务的范围检测。
那么还有一个重要的问题,难道我们只能根据getservice 去获取服务对象?来看看获取服务的扩展。
查看ServiceProviderServiceExtensions类:
里面有一些:
public static T GetService<T>(this IServiceProvider provider)
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
if (provider == null)
{
throw new ArgumentNullException("provider");
}
return (T)provider.GetService(Type.GetTypeFromHandle(typeof(T).TypeHandle));
}
/// <summary>
/// Get service of type <paramref name="serviceType" /> from the <see cref="T:System.IServiceProvider" />.
/// </summary>
/// <param name="provider">The <see cref="T:System.IServiceProvider" /> to retrieve the service object from.</param>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
/// <returns>A service object of type <paramref name="serviceType" />.</returns>
/// <exception cref="T:System.InvalidOperationException">There is no service of type <paramref name="serviceType" />.</exception>
public static object GetRequiredService(this IServiceProvider provider, Type serviceType)
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
if (provider == null)
{
throw new ArgumentNullException("provider");
}
if (serviceType == (Type)null)
{
throw new ArgumentNullException("serviceType");
}
ISupportRequiredService supportRequiredService = provider as ISupportRequiredService;
if (supportRequiredService != null)
{
return supportRequiredService.GetRequiredService(serviceType);
}
object service = provider.GetService(serviceType);
if (service == null)
{
throw new InvalidOperationException(Resources.FormatNoServiceRegistered(serviceType));
}
return service;
}
/// <summary>
/// Get service of type <typeparamref name="T" /> from the <see cref="T:System.IServiceProvider" />.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="provider">The <see cref="T:System.IServiceProvider" /> to retrieve the service object from.</param>
/// <returns>A service object of type <typeparamref name="T" />.</returns>
/// <exception cref="T:System.InvalidOperationException">There is no service of type <typeparamref name="T" />.</exception>
public static T GetRequiredService<T>(this IServiceProvider provider)
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
if (provider == null)
{
throw new ArgumentNullException("provider");
}
return (T)provider.GetRequiredService(Type.GetTypeFromHandle(typeof(T).TypeHandle));
}
/// <summary>
/// Get an enumeration of services of type <typeparamref name="T" /> from the <see cref="T:System.IServiceProvider" />.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="provider">The <see cref="T:System.IServiceProvider" /> to retrieve the services from.</param>
/// <returns>An enumeration of services of type <typeparamref name="T" />.</returns>
public static IEnumerable<T> GetServices<T>(this IServiceProvider provider)
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
if (provider == null)
{
throw new ArgumentNullException("provider");
}
return provider.GetRequiredService<IEnumerable<T>>();
}
其余的可以自己去查看。
服务的创建
我们知道服务的创建一般都是ioc自动选择的,那么假如我们一个class 里面有多个构造函数,那么会选择哪一个呢?
public interface IFoo { }
public interface IBar { }
public interface IBaz { }
public interface IQux { }
public class Foo : IFoo { }
public class Bar : IBar { }
public class Baz : IBaz { }
public class Qux : IQux
{
public Qux(IFoo foo) => Console.WriteLine("Selected constructor: Qux(IFoo)");
public Qux(IFoo foo, IBar bar) => Console.WriteLine("Selected constructor: Qux(IFoo, IBar)");
public Qux(IFoo foo, IBar bar, IBaz baz) => Console.WriteLine("Selected constructor: Qux(IFoo, IBar, IBaz)");
}
static void Main()
{
new ServiceCollection()
.AddTransient<IFoo, Foo>()
.AddTransient<IBar, Bar>()
.AddTransient<IQux, Qux>()
.BuildServiceProvider()
.GetServices<IQux>();
Console.ReadKey();
}
上面Qux有三个控制类,那么选择哪一个呢?
看下结果:
那么为什么会是这一个呢?
第一:IBaz类型没有注册,那么会排除掉Qux(IFoo foo, IBar bar, IBaz baz)
第二就是里面的一个规则:每一个候选构造参数类型的集合都是这个构造函数参数类型的子集。
好的,那么好了,剩下:Qux(IFoo foo, IBar bar) 包含了Qux(IFoo foo) 那么就是Qux(IFoo foo, IBar bar),如果没有包含全部子集的那么就会报错。
Qux(IFoo foo, IBar bar) 也就是超集的意思。
改一下:
public interface IFoo { }
public interface IBar { }
public interface IBaz { }
public interface IQux { }
public class Foo : IFoo { }
public class Bar : IBar { }
public class Baz : IBaz { }
public class Qux : IQux
{
public Qux(IFoo foo, IBaz baz) => Console.WriteLine("Selected constructor: Qux(IFoo, IBar)");
public Qux(IFoo foo, IBar bar) => Console.WriteLine("Selected constructor: Qux(IFoo, IBar, IBaz)");
}
static void Main()
{
new ServiceCollection()
.AddTransient<IFoo, Foo>()
.AddTransient<IBar, Bar>()
.AddTransient<IBaz,Baz>()
.AddTransient<IQux, Qux>()
.BuildServiceProvider()
.GetServices<IQux>();
Console.ReadKey();
}
这时候报错为:
下一节
服务的生命周期
相关文章
- 用.NET开发的磁力搜索引擎——btbook.net「建议收藏」
- 如何在ASP.NET MVC中获取客户端的IP地址?
- ASP.NET Core解析Protobuf格式数据
- 【愚公系列】2022年10月 .NET CORE文章1(待写)
- .NET Core 的Generic Host 之Generic Host Builder
- Asp.net core自定义依赖注入容器,替换自带容器
- [接上篇]在Window10/11的Linux子系统Docker上部署VB.NET Asp.Net Core WebAPI应用
- 【愚公系列】2022年12月 .NET CORE工具案例-.NET Core使用ExcelMapper
- 【愚公系列】2023年01月 .NET CORE工具案例- Magick.NET神级图片和视频操作库
- 【愚公系列】2023年02月 .NET CORE工具案例-办公文档神器Toxy的使用
- 【愚公系列】2023年02月 .NET CORE工具案例-Workflow-Core轻量级工作流引擎(数据流转)
- 【愚公系列】2023年02月 .NET CORE工具案例-Workflow-Core轻量级工作流引擎(流程事件)
- ASP.NET Core 6 RazorPages 开发项目实战教程
- InfoQ 2022 年趋势报告:.NET 篇
- 微软的.NET Core开始支持Raspberry Pi 3详解编程语言
- .NET Core 全新认识详解编程语言
- .NET Core 2.0版本预计于2017年春季发布详解编程语言
- 开发积极探索Mysql.Net 开发之路(mysql.net)
- .NET访问MySQL:简单实用的方法(.net访问mysql)
- 新手指南:通过Docker在Linux上托管.NET Core
- Net不再压着 MSSql,语音变得前景无限(net mssql语音)
- Net调用MySQL技术实践(.net 调用mysql)
- NET技术结合MySQL实现数据库访问极致体验(.net数据库mysql)
- Net中连接MySQL技术简述(.net中引用mysql)
- NET和MySQL的无缝搭配实现创新的技术路径(.net mysql支持)
- Oracle Net服务重新上线,确保数据安全(oracle net重启)
- ASP.NET数据库编程之Access连接失败
- ACCESS的参数化查询,附VBSCRIPT(ASP)和C#(ASP.NET)函数
- asp.net网页动态查询条件的实现
- ASP.NET生成树形显示的GridView实现思路
- NET弹出页面窗口选择返回值