gpt4 book ai didi

.net - NServiceBus 行为的作用域依赖使用

转载 作者:行者123 更新时间:2023-12-04 01:25:38 24 4
gpt4 key购买 nike

我正在尝试使用来自 NServiceBus BehaviorScoped 依赖项。

来自 NServiceBus Behavior文档:

Behaviors are only created once and the same instance is reused on every invocation of the pipeline. Consequently, every behavior dependency will also behave as a singleton, even if a different option was specified when registering it in dependency injection. Furthermore, the behavior, and all dependencies called during the invocation phase, need to be concurrency safe and possibly stateless. Storing state in a behavior instance should be avoided since it will cause the state to be shared across all message handling sessions. This could lead to unwanted side effects.

由于 Behavior 是单例,BehaviorInvoke 方法不允许注入(inject)任何依赖项(例如 invoke 网络核心中间件的方法,因为在这种情况下它是一个常规接口(interface)实现),我不能从这里使用 scoped 依赖项。

我试图通过在构造函数中传递 IServiceCollection 来解决我在 Invoke 方法中对每个传入/传出消息的依赖关系:

private readonly IServiceCollection _services;

public MyIncomingMessageBehavior(IServiceCollection services)
{
_services = services;
}

public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
{
var myScopedDependency = _services.BuildServiceProvider().GetService<IMyScopedDependency>();
// always
}

But this doesn't work :

That's because when you inject IServiceProvider into your middleware - that's "global" provider, not request-scoped. There is no request when your middleware constructor is invoked (middleware is created once at startup), so it cannot be request-scoped container.

总而言之,我的作用域依赖包含当前上下文的数据,我想从我的Behavior 单例的Invoke 方法访问这些数据?

有什么办法吗?

最佳答案

您需要在解决依赖关系之前创建一个范围:

private readonly IServiceScopeFactory _scopeFactory;

public MyIncomingMessageBehavior(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}

public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
{
using(var scope = _scopeFactory.CreateScope())
{
var myScopedDependency = scope.ServiceProvider.GetService<IMyScopedDependency>();
}
}

此外,请注意您的依赖项与范围一起处理。

关于.net - NServiceBus 行为的作用域依赖使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52606124/

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