Net Core removes registered Service

The project needs to refactor a certain function in a certain package

把源码拿下来之后发现需要重写其中一个服务
但是因为该服务访问级别为internal, 需要把原来internal服务从service collection注册器中移除
注入重写之后的服务

The DI container in ASP.NET Core is ultimately embodied as an IServiceProvider interface. We collectively refer to all types and instances that implement this interface as ServiceProvider. It only provides a single GetService method, which provides you with services based on the type of service provided. Corresponding service instance

What ASP.NET Core actually uses internally is an internal type that implements the IServiceProvider interface (the name of this type is "ServiceProvider"). We cannot directly create this object and can only obtain it indirectly by calling the extension method BuildServiceProvider of the IServiceCollection interface. The IServiceCollection interface is defined in the "Microsoft.Extensions.DependencyInjection" namespace

The IServiceCollection interface actually represents a collection whose elements are ServiceDescriptor objects. It directly inherits another interface IList, and the ServiceCollection class implements this interface.

The reason why the ServiceProvider embodied in the DI container can provide an out-of-the-box service instance based on the service type we give (usually an interface type) is because we have pre-registered the corresponding service description information, which guides the ServiceProvider correctly The service description that implements the service provision operation is embodied as the following ServiceDescriptor type.

public class ServiceDescriptor
{
    public ServiceDescriptor(Type serviceType, object instance);
    public ServiceDescriptor(Type serviceType, Func<IServiceProvider, object> factory, ServiceLifetimelifetime);
    public ServiceDescriptor(Type serviceType, TypeimplementationType, ServiceLifetime lifetime);

    public Type ServiceType {  get; }
    public ServiceLifetime Lifetime {  get; }

    public Type ImplementationType {  get; }
    public object ImplementationInstance {  get; }
    public Func<IServiceProvider, object> ImplementationFactory{  get; }      
}

The ServiceProvider in ASP.NET Core is created based on an IServiceCollection object that represents the ServiceDescriptor collection. When we call its GetService method, it will find the corresponding ServiceDecriptor object based on the service type we provide. If the ImplementationInstance property of the ServiceDescriptor object returns a specific object, that object will be used directly as the provided service instance.

So to remove a service, just find its corresponding ServiceDesriptor registered in the ServiceCollection.

public static IServiceCollection RemoveRegisteredServiceExtension(this IServiceCollection services)
{
     var descriptorDbContext = services.FirstOrDefault(descriptor => descriptor.Name == nameof(RemoveService));
            
     services.Remove(descriptorDbContext);
     return services;
}

Guess you like

Origin blog.csdn.net/Helloantoherday/article/details/126420508