gpt4 book ai didi

c# - 将 Microsoft.Azure.ServiceBus 替换为 Azure.Messaging.ServiceBus

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

我有一个使用 Microsoft.Azure.ServiceBus 的消息发布者,需要将其替换为 Azure.Messaging.ServiceBus,因为它现已弃用。

这是代码:

using Microsoft.Azure.ServiceBus;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Threading.Tasks;

namespace gbx.infra.ware.Services
{
public interface IMessagePublisher
{
public Task Publish<T>(T obj);
public Task Publish(string raw);
public Task<long> PublishScheduled<T>(T obj, DateTimeOffset time);

}
public class MessagePublisher : IMessagePublisher
{
private readonly ITopicClient _topicClient;

public MessagePublisher(ITopicClient topicClient)
{
_topicClient = topicClient;
}

public Task Publish<T>(T obj)
{
string objAsText = JsonConvert.SerializeObject(obj);

Message message = new Message(Encoding.UTF8.GetBytes(objAsText));
message.UserProperties["messageType"] = typeof(T).Name;
return _topicClient.SendAsync(message);
}

public Task Publish(string raw)
{
Message message = new Message(Encoding.UTF8.GetBytes(raw));
message.UserProperties["messageType"] = "Raw";
return _topicClient.SendAsync(message);
}

public Task<long> PublishScheduled<T>(T obj, DateTimeOffset time)
{
string objAsText = JsonConvert.SerializeObject(obj);

Message message = new Message(Encoding.UTF8.GetBytes(objAsText));
message.UserProperties["messageType"] = typeof(T).Name;
return _topicClient.ScheduleMessageAsync(message, time);
}
}
}

有什么简单的方法可以让我做出改变吗?我找不到这方面的任何信息。

发布者的注册方式如下:

services.AddSingleton<ITopicClient>(x => new TopicClient(Configuration["ServiceBus:ConnectionString"], Configuration["ServiceBus:TopicName"]));
services.AddSingleton<IMessagePublisher, MessagePublisher>();

并根据需要注入(inject)。

我想要做的是仅更改消息发布器中的代码,以便不需要在使用它的代码中进行任何更改。

最佳答案

如果您只是使用 migration guide你最终会得到

public interface IMessagePublisher
{
public Task Publish<T>(T obj);
public Task Publish(string raw);
public Task<long> PublishScheduled<T>(T obj, DateTimeOffset time);

}

public class MessagePublisher : IMessagePublisher
{
private readonly ServiceBusSender _serviceBusSender;

public MessagePublisher(ServiceBusSender serviceBusSender)
{
_serviceBusSender = serviceBusSender;
}

public Task Publish<T>(T obj)
{
string objAsText = JsonConvert.SerializeObject(obj);

ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes(objAsText));
message.ApplicationProperties["messageType"] = typeof(T).Name;
return _serviceBusSender.SendMessageAsync(message);
}

public Task Publish(string raw)
{
ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes(raw));
message.ApplicationProperties["messageType"] = "Raw";
return _serviceBusSender.SendMessageAsync(message);
}

public Task<long> PublishScheduled<T>(T obj, DateTimeOffset time)
{
string objAsText = JsonConvert.SerializeObject(obj);

ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes(objAsText));
message.ApplicationProperties["messageType"] = typeof(T).Name;
return _serviceBusSender.ScheduleMessageAsync(message, time);
}
}

要注册MessagePublisher,请尝试:

services.AddSingleton<IMessagePublisher>(p => 
new MessagePublisher(
new ServiceBusClient(Configuration["ServiceBus:ConnectionString"]).CreateSender(Configuration["ServiceBus:TopicName"])));

注意:还有其他方法可以注册消息发布者,参见How to register ServiceBusClient for dependency injection? 。不过,您可能需要对 MessagePublisher 进行一些细微修改。

关于c# - 将 Microsoft.Azure.ServiceBus 替换为 Azure.Messaging.ServiceBus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73359526/

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