如何从.net核心IoC容器中删除默认服务? [英] How to remove a default service from the .net core IoC container?

查看:231
本文介绍了如何从.net核心IoC容器中删除默认服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.net核心的美丽之处之一是它非常模块化且可配置.

One of the beautiful things about .net core is that it is very modular and configurable.

这种灵活性的一个关键方面是它利用IoC经常通过接口来注册服务.从理论上讲,这可以用很少的精力用该服务的自定义实现替换默认的.net服务.

A key aspect of that flexibility is that it leverages an IoC for registering services, often via interfaces. This in theory allows for replacing a default .net service with a custom implementation of that service with very little effort.

从理论上讲,这听起来真棒.但是我有一个实际的工作案例,我想用我自己的默认的.net核心服务替换,但我不知道如何删除默认的服务.

This all sounds awesome in theory. But I have a real work case where I want to replace a default .net core service with my own and I can't figure out how to remove the default service.

更具体地说,在Startup.cs ConfigureServices方法中,调用services.AddSession()时,它会注册一个DistributedSessionStore vai以下代码:

More specifically, in the Startup.cs ConfigureServices method, when services.AddSession() is called it registers a DistributedSessionStore vai following code:

 services.AddTransient<ISessionStore, DistributedSessionStore>();

在源代码中可以看到

: https://github.com/aspnet/Session/blob/rel/1.1.0/src/Microsoft.AspNetCore.Session/SessionServiceCollectionExtensions.cs

as can be seen in the source code: https://github.com/aspnet/Session/blob/rel/1.1.0/src/Microsoft.AspNetCore.Session/SessionServiceCollectionExtensions.cs

我想用自己创建的一个替换该ISessionStore.因此,如果我有一个类RonsSessionStore:ISessionStore我想用来替换当前注册的ISessionStore,该怎么办?

I'd like replace that ISessionStore with one of my own creation. So if I have a class RonsSessionStore:ISessionStore that I want to use to replace the currently registered ISessionStore, how can I do it?

我知道我可以通过以下方式在Startup.cs ConfigureServices方法中注册我的ISessionStore:

I know I can register my ISessionStore in the Startup.cs ConfigureServices method via the following:

 services.AddTransient<ISessionStore, RonsSessionStore>();

但是如何删除已经注册的DistributedSessionStore?

But how can I remove the already registered DistributedSessionStore?

我试图通过

 services.Remove(ServiceDescriptor.Transient<ISessionStore, DistributedSessionStore>());

,但没有任何效果,DistributedSessionStore仍在IoC容器中.有任何想法吗?

but it had no effect and the DistributedSessionStore was still in the IoC container. Any ideas?

如何使用startup.cs的ConfigureServices方法从IoC中删除服务?

How does one remove a service from the IoC in the ConfigureServices method of the startup.cs?

推荐答案

您的代码无法正常工作,因为ServiceDescriptor类不会覆盖Equals,并且ServiceDescriptor.Transient()返回的新实例与该实例不同在集合中.

Your code doesn't work because the ServiceDescriptor class doesn't override Equals, and ServiceDescriptor.Transient() returns a new instance, different than the one in the collection.

您将必须在集合中找到ServiceDescriptor并将其删除:

You would have to find the ServiceDescriptor in the collection and remove it:

var serviceDescriptor = services.First(s => s.ServiceType == typeof(ISessionStore));
services.Remove(serviceDescriptor);

这篇关于如何从.net核心IoC容器中删除默认服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆