我正在尝试更新这个函数,使用Azure.Messaging.ServiceBus代替Microsoft.Azure.ServiceBus,但是似乎找不到任何相关资源。有人知道如何使用这个包发送主题消息吗?
旧的函数是:
public async Task SendMessageToServiceBusTopic<T>(T request, string topicSubName, string submissionNumber)
{
ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder =
new ServiceBusConnectionStringBuilder(settings.ServiceBusConnectionString)
{
EntityPath = settings.ServiceBusTopic
};
TopicClient topicClient = new TopicClient(serviceBusConnectionStringBuilder);
byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));
await topicClient.SendAsync(new Message(bytes)
{
CorrelationId = context.CorrelationId,
Label=topicSubName,
UserProperties = { new KeyValuePair<string, object>("TrackingId", submissionNumber) }
});
}
到目前为止,我已经成功地完成了:
我是否朝着正确的方向前进?
public async Task SendMessageToServiceBusTopic<T>(T request, string topicSubName, string submissionNumber)
{
ServiceBusClient client = new ServiceBusClient(settings.ServiceBusConnectionString);
ServiceBusSender s = client.CreateSender(settings.ServiceBusTopic);
byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));
await s.SendMessageAsync(new ServiceBusMessage(bytes));
}
ServiceBusClient
及其实例化的“AMQP链接”对象,例如从CreateSender
创建的ServiceBusSender
,必须进行处理以避免句柄泄漏。文档对此解释不够清晰,而是侧重于重复连接和链接创建的性能影响。这里的示例在必要时处理了客户端和发送器,但仍会由于不断重新连接而导致过多的网络流量。无法分配更多句柄。句柄的最大数量为4999。(QuotaExceeded)。 - mkjeldsen