我陷入困境,需要一些建议或解决方案。
使用 ASP.NET Core 3.1 的 Web API
启动.cs
services.AddSingleton<ITopicClient>(s => new TopicClient({connectionstring},{topic}));
Run Code Online (Sandbox Code Playgroud)
TopicRepository.cs
public class TopicRepository : ITopicRepository
{
private readonly ITopicClient _topicClient1;
private readonly ITopicClient _topicClient2;
public TopicRepository (ITopicClient topicClient1, ITopicClient topicClient2)
{
_topicClient1 = topicClient1;
_topicClient2 = topicClient2;
}
public async Task<Response> SendToTopicAsync(string message, string topic)
{
if( topic == "topic1")
await _topicClient1.send(message);
else if (topic == "topic2")
await _topicClient2.send(message);
}
}
Run Code Online (Sandbox Code Playgroud)
共享库中的 TopicClient.cs
public TopicClient(string serviceBusConnectionString, string topicName)
{
_topicClient = new TopicClient(_serviceBusConnectionString,topicName);
}
Run Code Online (Sandbox Code Playgroud)
我需要向不同的主题发送消息。我想在startup.cs中注册具有不同主题名称的服务。我想重用 …
c# dependency-injection azure-servicebus-topics .net-core-3.1