gpt4 book ai didi

c# - NServiceBus IUniformSession 不适用于 Azure WebJobs SDK

转载 作者:行者123 更新时间:2023-12-03 03:35:17 29 4
gpt4 key购买 nike

我在使用通用主机让 IUniformSession 在 Azure WebJobs SDK 项目中工作时遇到问题。我正在使用 .NET 内置 DI。我创建了一个紧凑的复制品,如下所示。

我正在使用

  • NServiceBus 7.7.4
  • NServiceBus.Extensions.Hosting 1.1.0
  • NServiceBus.UniformSession 2.2.0
  • Microsoft.Azure.WebJobs 3.0.33
  • .NET 6

预期行为

当类(“作业”)实例化时,IUniformSession 被解析为依赖项。

实际行为

IUniformSession 始终解析为 NULL。一些快速断言(如下所示)表明 IUniformSession 已在 DI 容器中注册,因此 .EnableUniformSession() 正确执行此操作,但当请求实例时,它始终解析为 NULL。

重现步骤

csproj

<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" Version="4.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="NServiceBus" Version="7.7.4" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="1.1.0" />
<PackageReference Include="NServiceBus.UniformSession" Version="2.2.0" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
</ItemGroup>

程序.cs

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NServiceBus;
using NServiceBus.UniformSession;
using NSUniformSessionRepro.DemoClasses;
using Serilog;

// Setup the ability to read config files
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
// then read the environment variables (in Azure) which will override some settings from the local settings
.AddEnvironmentVariables();
var configuration = configurationBuilder.Build();

var builder = new HostBuilder();
builder
.UseSerilog(Log.Logger)
.ConfigureAppConfiguration((context, config) =>
{
config.AddConfiguration(configuration);
})
.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices()
.AddAzureStorageBlobs()
.AddAzureStorageQueues()
.AddTimers();
});

IServiceCollection _serviceCollection = null;

builder.ConfigureServices(services =>
{
services.AddTransient<IMessagingService, MessagingService>();
_serviceCollection = services;

});

builder.UseNServiceBus(context =>
{
var endpointConfiguration = new EndpointConfiguration("MyEndpoint");

endpointConfiguration.EnableUniformSession();
endpointConfiguration.UseTransport<LearningTransport>();
endpointConfiguration.UsePersistence<LearningPersistence>();
// configure endpoint here
return endpointConfiguration;
});


var host = builder.Build();

var isUniformSessionRegistered = _serviceCollection.Any(x => x.ServiceType == typeof(IUniformSession));
Console.WriteLine("UniformSession is registered in DI: " + isUniformSessionRegistered);

using (host)
{
await host.RunAsync();
}



static void ConfigureLogging(IConfigurationRoot configuration)
{
var loggerConfiguration = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console();

Log.Logger = loggerConfiguration
.CreateLogger();
}

MessagingService.cs(只是一个虚拟类,其本身依赖于 IUniformSession)

public interface IMessagingService
{
// public for testing
IUniformSession UniformSession { get; set; }
Task Send(object message);
}

public class MessagingService : IMessagingService
{
// public for testing
public IUniformSession UniformSession { get; set; }

public MessagingService(IUniformSession uniformSession)
{
UniformSession = uniformSession;
}

public async Task Send(object message)
=> await UniformSession.Send(message);
}

演示作业

public class DemoJob
{
private readonly IMessagingService _messagingService;

public DemoJob(IMessagingService messagingService)
{
_messagingService = messagingService;
}

public void DummyJob([TimerTrigger("0 0 12 * * *", RunOnStartup = true, UseMonitor = false)] TimerInfo timer, TextWriter log)
{
// no-op
// for testing puporses
if (_messagingService.UniformSession == null)
Console.Write("UniformSession was NULL");
}
}

任何帮助将不胜感激。

最佳答案

这有点棘手,因为主机的设置方式会因启动顺序而导致一些问题。

首先,我不确定你为什么要在 IUniformSession 上构建自己的抽象。通过IMessagingService所以我就不提这个了。请记住IUniformSession已经是 IMessageSession 的抽象(这就是你所需要的)和 IMessageHandlerContext .

您应该能够依赖 IUniformSessionIMessageSession将调用移至 builder.UseNServiceBus在调用 builder.ConfigureWebJobs 之前,例如:

builder
.UseSerilog(Log.Logger)
.ConfigureAppConfiguration((context, config) =>
{
config.AddConfiguration(configuration);
})
.UseNServiceBus(context =>
{
var endpointConfiguration = new EndpointConfiguration("MyEndpoint");

endpointConfiguration.EnableUniformSession();
endpointConfiguration.UseTransport<LearningTransport>();
endpointConfiguration.UsePersistence<LearningPersistence>();
// configure endpoint here
return endpointConfiguration;
})
.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices()
.AddAzureStorageBlobs()
.AddAzureStorageQueues()
.AddTimers();
});

这将确保 NServiceBus 在 Web 作业相关代码之前配置并启动,以便可以在正确的时间点通过 DI 注入(inject)所需的服务。

请注意,使用IMessageSession应该早点指出这一点,因为 NServiceBus 通用主机支持有 explicit code to detect such ordering issues统一 session 没有。

关于c# - NServiceBus IUniformSession 不适用于 Azure WebJobs SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73574255/

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