gpt4 book ai didi

c# - 如何使用默认的asp.net core DI容器在类中注入(inject)单个接口(interface)的多个服务类实现

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:44 24 4
gpt4 key购买 nike

我们正在将项目从 .Net Framework 4.6.2 迁移到 .Net Core 2.0。我有一个情况,单个接口(interface)由多个类实现,所有这些实现都需要注入(inject)到消费客户端并按某种顺序执行(这里的顺序和类名是在运行时从配置中选择的)。我正在注入(inject) IServiceProvider 以在使用类构造函数中解析这些实现,但在构建对象时出现 StackOverflowException。

这里需要注意的是,所有这些类都实现了一个接口(interface),它们的实例必须注入(inject)到消费类中。 .Net Core 2.0 提供的默认 DI 容器不支持实现相同接口(interface)的多个类的命名(类名)或类型化(类类型)注入(inject),其他 DI 选项如 Unity(我们以前的容器)支持。

为了解决这个问题,我注入(inject)了 IServiceProvider 并将其包装在另一个构建所有实例的类中,然后根据类类型或类名过滤所需的实例。

但是,当我尝试构建所有这些实例时,运行时抛出 StackOverFlowException。

注意 - 所有类都已注册到 Transient Scope,并且不能为每个类创建单独的接口(interface)并注入(inject)它们。

我的 IServiceProvider 包装器 -

public class TypedImplementationDependencyInjector
{
private readonly IServiceProvider _serviceProvider = null;

public TypedImplementationDependencyInjector(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public T ResolveImplementation<U, T>() where T : class, U
{
if (typeof(U).IsInterface)
{
var services = _serviceProvider.GetServices<U>();
var typedImplementation = services.FirstOrDefault(o => o.GetType() == typeof(T)) as T;
return typedImplementation;
}

return default(T);
}

public T ResolveImplementation<T>(Type type)
{
if (type == null)
throw new TypeLoadException("Supplied type was null or empty");

if (typeof(T).IsInterface)
{
var services = _serviceProvider.GetServices<T>();
var typedImplementation = services.FirstOrDefault(o => o.GetType() == type);
return typedImplementation;
}

return default(T);
}

public T ResolveImplementation<T>(string assemblyName, string namespacename, string classname)
{
if (typeof(T).IsInterface)
{
Type type = Type.GetType($"{namespacename}.{classname}, {assemblyName}", true, true);
return ResolveImplementation<T>(type);
}

return default(T);
}
}

我正在实现的通用接口(interface)

public interface IEntityOperationCommand
{
void Execute(EntityOperationModel operationModel);
}

解决这些依赖关系的我的消费类 -

public class EntityOperationProvider
{
private readonly IEntityOperationCommand _TWCommandPrepare = null;
private readonly IEntityOperationCommand _TWCommandCreateDetails = null;
private readonly IEntityOperationCommand _TWCommandPostCreation = null;
private readonly IEntityOperationCommand _TWCommandRaiseEvents = null;
private readonly IEntityOperationCommand _TWCommandNotification = null;
private readonly TypedImplementationDependencyInjector _implementationDependencyInjector = null;

private const string assemblyName = "__assembly_name__";
private const string namespaceName = "__namespace__";


public EntityOperationProvider(TypedImplementationDependencyInjector implementationDependencyInjector)
{
_implementationDependencyInjector = implementationDependencyInjector;

_TWCommandPrepare = _implementationDependencyInjector.ResolveImplementation<IEntityOperationCommand>(assemblyName, namespaceName, "PrepareEntity");
_TWCommandCreateDetails = _implementationDependencyInjector.ResolveImplementation<IEntityOperationCommand>(assemblyName, namespaceName, "CreateDetails");
_TWCommandPostCreation = _implementationDependencyInjector.ResolveImplementation<IEntityOperationCommand>(assemblyName, namespaceName, "PostCreation");
_TWCommandRaiseEvents = _implementationDependencyInjector.ResolveImplementation<IEntityOperationCommand>(assemblyName, namespaceName, "RaiseEvents");
_TWCommandNotification = _implementationDependencyInjector.ResolveImplementation<IEntityOperationCommand>(assemblyName, namespaceName, "SendNotification");
}
}

注入(inject) EntityOperationProvider 时,其构造函数会尝试构建依赖项并抛出 StackOVerflowException。

我希望创建并注入(inject) EntityOperationProvider 对象。

注意 - 我无法解决导致 StackOverFlowException 的问题,没有循环依赖(比如 PrepareEntity 类需要 RaiseEvents 对象,反之亦然)或自依赖(比如 CreateDetails 试图递归地创建它自己的对象)。如有任何帮助,我们将不胜感激。

此外,如果有任何优雅的方式来注入(inject)类型化依赖项(例如 Unity 的 Dependency 属性)而不是注入(inject) IserviceProivder,那也会有所帮助。

最佳答案

如果你必须注入(inject) IServiceProvider那么设计就有问题,因为那是代码味道。

Asp.Net Core DI 允许注册一个接口(interface)的多个实现。因此,它还允许通过 IEnumerable<IInterface> 将该集合注入(inject)依赖类。进入依赖构造函数

例如

public EntityOperationProvider(IEnumerable<IEntityOperationCommand> commands) {
//...
}

这将在内部使用 IServiceProvider.GetServices<T>()扩展名

从那里只需要提取所需的实现

public class EntityOperationProvider {
private readonly IEntityOperationCommand _TWCommandPrepare = null;
private readonly IEntityOperationCommand _TWCommandCreateDetails = null;
private readonly IEntityOperationCommand _TWCommandPostCreation = null;
private readonly IEntityOperationCommand _TWCommandRaiseEvents = null;
private readonly IEntityOperationCommand _TWCommandNotification = null;


public EntityOperationProvider(IEnumerable<IEntityOperationCommand> commands) {
_TWCommandPrepare = commands.OfType<PrepareEntity>().FirstOrDefault();
_TWCommandCreateDetails = commands.OfType<CreateDetails>().FirstOrDefault();
_TWCommandPostCreation = commands.OfType<PostCreation>().FirstOrDefault();
_TWCommandRaiseEvents = commands.OfType<RaiseEvents>().FirstOrDefault();
_TWCommandNotification = commands.OfType<SendNotification>().FirstOrDefault();
}

//...
}

关于c# - 如何使用默认的asp.net core DI容器在类中注入(inject)单个接口(interface)的多个服务类实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53880625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com