gpt4 book ai didi

c# - 在 ASP.net Core 中注入(inject)多个 ServiceBusClient

转载 作者:行者123 更新时间:2023-12-04 13:52:37 32 4
gpt4 key购买 nike

我想注入(inject)多个具有相同连接字符串但不同队列名称的 ServiceBusClient。

    _services.TryAddSingleton(implementationFactory =>
{
var serviceBusConfiguration = implementationFactory.GetRequiredService<Microsoft.Extensions.Options.IOptions<UserEventConsumerSetting>>().Value;

var serviceBusClient = new ServiceBusClient(serviceBusConfiguration.ServiceBusConnectionString, new ServiceBusClientOptions
{
TransportType = ServiceBusTransportType.AmqpWebSockets
});

return serviceBusClient.CreateReceiver(serviceBusConfiguration.ServiceBusQueueName, new ServiceBusReceiverOptions
{
ReceiveMode = ServiceBusReceiveMode.PeekLock
});
});
为了使用,我必须创建 ServiceBusSender 实例。
    private readonly ServiceBusSender _serviceBusSender;

public CarReservationMessagingService(ServiceBusSender serviceBusSender)
{
_serviceBusSender = serviceBusSender ?? throw new ArgumentNullException(nameof(serviceBusSender));
}

public async Task PublishNewCarReservationMessageAsync(CarReservation carReservation)
{
var carReservationIntegrationMessage = new CarReservationIntegrationMessage
{
Id = Guid.NewGuid().ToString(),
CarId = carReservation.CarId,
CustomerId = carReservation.CustomerId,
RentFrom = carReservation.RentFrom,
RentTo = carReservation.RentTo
};

var serializedMessage = JsonSerializer.Serialize(carReservationIntegrationMessage);
ServiceBusMessage message = new ServiceBusMessage(serializedMessage);
await _serviceBusSender.SendMessageAsync(message);
}
如何注入(inject)多个 servicebusclient 并以不同方式使用它们?

最佳答案

因此,在其他 IoC 容器(如 Unity)中,可以指定命名依赖项。您在注册时指定名称,如下所示:

container.RegisterSingleto<SomeClassImplementation>("Name")
然后在您使用的类中,您应该使用此名称指定属性
[DependencyAttribute("Name")]
但是.net core 中没有这样的功能。
您可以使用统一。
或者你可以模仿这个功能。
让我们创建枚举(或常量,如果需要)
enum ServiceBusQueue { ServiceBus1, ServiceBus2, ServiceBus3}
然后创建 service buc provider
public class ServiceBusQueueProvider
{
private Dictionary<ServiceBusQueue, ServiceBusSender > _senders;

public ServiceBusQueueProvider()
{
//Method to initialize all queues with all names and store it in _senders;
}

public ServiceBusSender Resolve(ServiceBusQueue queue)
{
return _senders[queue];
}
}
然后注册此提供程序并在其他需要获取不同 quees 的 clusses 中使用它:
 _services.TryAddSingleton(implementationFactory => ServiceBusQueueProvider());
并在构造函数中:
public SomeClass(ServiceBusQueueProvider provider)
{
_serviceBusSender = provider.Resolve(ServiceBusQueue.ServiceBus1);
}
还有一点是你应该改变 MessagingServiceConfiguration
此类映射到配置。在当前的实现中,您只能指定一个队列。
像这样改变它:
public class MessagingServiceConfiguration
{
public List<MessagingServiceConfigurationItem> Queues { get; set; }
}

public class MessagingServiceConfigurationItem
{
public string QueueName { get; set; }
public string ListenAndSendConnectionString { get; set; }
}

关于c# - 在 ASP.net Core 中注入(inject)多个 ServiceBusClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67970273/

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