public interface IServiceProvider
{
object GetService(Type serviceType);
}
ASP.NET Core内部真正使用的是一个实现了IServiceProvider接口的内部类型(该类型的名称为“ServiceProvider”),我们不能直接创建该对象,只能间接地通过调用IServiceCollection接口的扩展方法BuildServiceProvider得到它。IServiceCollection接口定义在“Microsoft.Extensions.DependencyInjection”命名空间下,如果没有特别说明,本系列文章涉及到的与ASP.NET Core依赖注入相关的类型均采用此命名空间。 如下面的代码片段所示,IServiceCollection接口实际上代表一个元素为ServiceDescriptor对象的集合,它直接继承了另一个接口IList<ServiceDescriptor>,而ServiceCollection类实现了该接口。
public static class ServiceCollectionExtensions
{
public static IServiceProvider BuildServiceProvider(this IServiceCollection services);
}
public interface IServiceCollection : IList<ServiceDescriptor>
{}
Public class ServiceCollection: IServiceCollection
{
//省略成员
}
public class ServiceDescriptor
{
public ServiceDescriptor(Type serviceType, object instance);
public ServiceDescriptor(Type serviceType, Func<IServiceProvider, object> factory, ServiceLifetime lifetime);
public ServiceDescriptor(Type serviceType, Type implementationType, ServiceLifetime lifetime);
public Type ServiceType { get; }
public ServiceLifetime Lifetime { get; }
public Type ImplementationType { get; }
public object ImplementationInstance { get; }
public Func<IServiceProvider, object> ImplementationFactory { get; }
}
ASP.NET Core针对依赖注入的编程主要体现在两个方面:其一,创建一个ServiceCollection对象并将服务注册信息以ServiceDescriptor对象的形式添加其中;其二,针对ServiceCollection对象创建对应的ServiceProvider并利用它提供我们需要的服务实例。
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddScoped<TService>(this IServiceCollection services) where TService: class;
//其他AddScoped<TService>重载
public static IServiceCollection AddSingleton<TService>(this IServiceCollection services) where TService: class;
//其他AddSingleton<TService>重载
public static IServiceCollection AddTransient<TService>(this IServiceCollection services) where TService: class;
//其他AddTransient<TService>重载
}
public static class ServiceProviderExtensions
{
public static T GetService<T>(this IServiceProvider provider);
public static object GetRequiredService(this IServiceProvider provider, Type serviceType);
public static T GetRequiredService<T>(this IServiceProvider provider);
}
public interface IFoo {}
public interface IBar {}
public interface IBaz {}
public interface IGux
{
IFoo Foo { get; }
IBar Bar { get; }
IBaz Baz { get; }
}
public class Foo : IFoo {}
public class Bar : IBar {}
public class Baz : IBaz {}
public class Gux : IGux
{
public IFoo Foo { get; private set; }
public IBar Bar { get; private set; }
public IBaz Baz { get; private set; }
public Gux(IFoo foo, IBar bar, IBaz baz)
{
this.Foo = foo;
this.Bar = bar;
this.Baz = baz;
}
}
public static class ServiceProviderExtensions
{
public static IEnumerable<T> GetServices<T>(this IServiceProvider provider);
public static IEnumerable<object> GetServices(this IServiceProvider provider, Type serviceType);
}
public interface IServiceProvider
{
object GetService(Type serviceType);
}
ASP.NET Core内部真正使用的是一个实现了IServiceProvider接口的内部类型(该类型的名称为“ServiceProvider”),我们不能直接创建该对象,只能间接地通过调用IServiceCollection接口的扩展方法BuildServiceProvider得到它。IServiceCollection接口定义在“Microsoft.Extensions.DependencyInjection”命名空间下,如果没有特别说明,本系列文章涉及到的与ASP.NET Core依赖注入相关的类型均采用此命名空间。 如下面的代码片段所示,IServiceCollection接口实际上代表一个元素为ServiceDescriptor对象的集合,它直接继承了另一个接口IList<ServiceDescriptor>,而ServiceCollection类实现了该接口。
public static class ServiceCollectionExtensions
{
public static IServiceProvider BuildServiceProvider(this IServiceCollection services);
}
public interface IServiceCollection : IList<ServiceDescriptor>
{}
Public class ServiceCollection: IServiceCollection
{
//省略成员
}
public class ServiceDescriptor
{
public ServiceDescriptor(Type serviceType, object instance);
public ServiceDescriptor(Type serviceType, Func<IServiceProvider, object> factory, ServiceLifetime lifetime);
public ServiceDescriptor(Type serviceType, Type implementationType, ServiceLifetime lifetime);
public Type ServiceType { get; }
public ServiceLifetime Lifetime { get; }
public Type ImplementationType { get; }
public object ImplementationInstance { get; }
public Func<IServiceProvider, object> ImplementationFactory { get; }
}
ASP.NET Core针对依赖注入的编程主要体现在两个方面:其一,创建一个ServiceCollection对象并将服务注册信息以ServiceDescriptor对象的形式添加其中;其二,针对ServiceCollection对象创建对应的ServiceProvider并利用它提供我们需要的服务实例。
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddScoped<TService>(this IServiceCollection services) where TService: class;
//其他AddScoped<TService>重载
public static IServiceCollection AddSingleton<TService>(this IServiceCollection services) where TService: class;
//其他AddSingleton<TService>重载
public static IServiceCollection AddTransient<TService>(this IServiceCollection services) where TService: class;
//其他AddTransient<TService>重载
}
public static class ServiceProviderExtensions
{
public static T GetService<T>(this IServiceProvider provider);
public static object GetRequiredService(this IServiceProvider provider, Type serviceType);
public static T GetRequiredService<T>(this IServiceProvider provider);
}
public interface IFoo {}
public interface IBar {}
public interface IBaz {}
public interface IGux
{
IFoo Foo { get; }
IBar Bar { get; }
IBaz Baz { get; }
}
public class Foo : IFoo {}
public class Bar : IBar {}
public class Baz : IBaz {}
public class Gux : IGux
{
public IFoo Foo { get; private set; }
public IBar Bar { get; private set; }
public IBaz Baz { get; private set; }
public Gux(IFoo foo, IBar bar, IBaz baz)
{
this.Foo = foo;
this.Bar = bar;
this.Baz = baz;
}
}
public static class ServiceProviderExtensions
{
public static IEnumerable<T> GetServices<T>(this IServiceProvider provider);
public static IEnumerable<object> GetServices(this IServiceProvider provider, Type serviceType);
}