Windows 10 专业版
.NET Core 控制台应用程序
我有一个看起来像这样的抽象消息接收器。在此代码中,entity
是Subscription
(例如user
)的名称。
public class AzureMessageReceiver : ITdlMessageReceiver
{
private readonly ServiceBusConnection serviceBusConnection;
private readonly ILogger<AzureMessageReceiver> logger;
public AzureMessageReceiver(ServiceBusConnection serviceBusConnection, ILogger<AzureMessageReceiver> logger)
{
this.serviceBusConnection = serviceBusConnection;
this.logger = logger;
}
public async Task<TdlMessage<T>> ReceiveAsync<T>(string topic, string entity) where T : class
{
try
{
var subscriptionPath = EntityNameHelper.FormatSubscriptionPath(topic, entity);
var messageReceiver = new MessageReceiver(serviceBusConnection, subscriptionPath, ReceiveMode.ReceiveAndDelete);
var message = await messageReceiver.ReceiveAsync();
if (message == null)
{
return null;
}
var messageString = Encoding.UTF8.GetString(message.Body);
return JsonConvert.DeserializeObject<TdlMessage<T>>(messageString);
}
catch (Exception ex)
{
logger.LogError(ex, "Error receiving Azure message.");
return null;
}
}
}
注入ServiceBusConnection的构造是这样的。注:此相同的连接的初始化工作,以写邮件到同Topic和Subscription。
services.AddSingleton(serviceProvider =>
new ServiceBusConnection(configuration[$"{DurableCommunicationKey}:AzureConnectionString"]));
每次我执行这一行时,var message = await messageReceiver.ReceiveAsync();
它都会使控制台应用程序崩溃。没有Exception
,也没有Event Viewer
。
使用Secondary Connection String
来自 ASB
提供超时,如 messageReceiver.ReceiveAsync(TimeSpan.FromMinutes(1));
将注入的内容topic
从主题名称更改为主题的整个 URL(例如https://{...}.servicebus.windows.net/{topicName}
)
更改ReceiveMode
为PeekLock
后跟踪ConfigureAwait(false)
到ReceiveAsync
的呼叫。
将超时更改为TimeSpan.Zero
. 注意:这并没有崩溃的应用程序,但实际上抛出Exception
该得到记录。
侃侃尔雅
相关分类