我有两个不同的应用程序,我以相同的方式使用服务总线注册,一个可以工作,另一个则不能。那么问题出在哪里呢?
根据 AddServiceBusClient(),我放置了连接字符串表单设置,并且我 100% 确定此 cs 存在。
public static IServiceCollection AddProjectMessagingSubscriptions(this IServiceCollection services, IConfiguration configuration)
{
services.AddHostedService<HumanResourcesTopicReciever>();
services.AddHostedService<SelfServiceTopicReciever>();
var messagingConfiguration = configuration.GetSection("MessagingOptions");
services.AddAzureClients(builder =>
{
builder.AddServiceBusClient(connectionString: messagingConfiguration["ConnectionString"]);
builder.AddClient<ServiceBusReceiver, ServiceBusClientOptions>(
(_, _, provider) => provider.GetService<ServiceBusClient>()
.CreateReceiver(messagingConfiguration["SelfServiceTopic"], messagingConfiguration["SelfServiceTopicSubscriptionName"])).WithName("SelfServiceTopic");
builder.AddClient<ServiceBusReceiver, ServiceBusClientOptions>(
(_, _, provider) => provider.GetService<ServiceBusClient>()
.CreateReceiver(messagingConfiguration["HumanResourcesTopic"], messagingConfiguration["HumanResourcesTopicSubscriptionName"])).WithName("HumanResourcesTopicSubscription");
});
return services;
}
当我运行应用程序时,我收到此错误
没有意义,因为cs已经转移了!看起来库正在为我覆盖它并在那里插入一个 null,但为什么?
使用骑手我调试服务总线库,我确信连接字符串正确传递。在某些时候,该值会被替换为 null
我使用了下面的代码,它对我来说效果很好:
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"RithBus": {
"ConStr": "Endpoint=sb://rithwik.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=uqmgMXqXd14="
}
}
程序.cs:
using Azure.Messaging.ServiceBus;
var rith = WebApplication.CreateBuilder(args);
rith.Services.AddRazorPages();
rith.Services.AddSingleton(new ServiceBusClient(rith.Configuration["RithBus:ConStr"]));
rith.Services.AddHostedService<RithServiceBusReciver>();
var app = rith.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
RithServiceBusReciver.cs:
using Azure.Messaging.ServiceBus;
public class RithServiceBusReciver : BackgroundService
{
private readonly ServiceBusProcessor _rith;
private readonly ILogger<RithServiceBusReciver> r_log;
public RithServiceBusReciver(ServiceBusClient client, ILogger<RithServiceBusReciver> logger)
{
_rith = client.CreateProcessor("rithtopic", "rithsub");
_rith.ProcessMessageAsync += Rith_ProcessMessages;
_rith.ProcessErrorAsync += Rith_ErrorHandlerAsync;
r_log = logger;
}
private Task Rith_ProcessMessages(ProcessMessageEventArgs rags)
{
string body = rags.Message.Body.ToString();
r_log.LogInformation($"Hello Rithwik, The Received message is : {body}");
return rags.CompleteMessageAsync(rags.Message);
}
private Task Rith_ErrorHandlerAsync(ProcessErrorEventArgs rags)
{
r_log.LogError(rags.Exception, $"Hello Rithwik, The Error message is : {rags.Exception.Message}");
return Task.CompletedTask;
}
protected override async Task ExecuteAsync(CancellationToken ch_token)
{
await _rith.StartProcessingAsync(ch_token);
await Task.Delay(Timeout.Infinite, ch_token);
}
public override async Task StopAsync(CancellationToken ch_token)
{
await _rith.StopProcessingAsync(ch_token);
await base.StopAsync(ch_token);
}
}
输出:
已发送消息:
收到消息: