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

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

问题描述

.net core 的优点之一是它非常模块化且可配置.

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 以下代码:

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 类没有覆盖 EqualsServiceDescriptor.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 core IoC 容器中删除默认服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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