我将EventHubClient注入到我的控制器中,如下所示
services.AddScoped<EventHubClient>(a =>
{
eventHubClientIncomplete = EventHubClient.CreateFromConnectionString(new EventHubsConnectionStringBuilder(eventHubSettingsIncompleteApplications.ConnectionString)
{
EntityPath = eventHubSettingsIncompleteApplications.EventHubName
}.ToString());
return eventHubClientIncomplete;
});它运行良好。但是现在我需要从不同的端点发送到多个EventHubs。我该怎么做that...any指针?
发布于 2019-12-10 16:25:20
我想到了3个解决方案:
1.为EventHubClient创建自己的工厂。然后在服务中添加工厂。通过这种方式,您将能够在需要时注入工厂实例,然后从工厂方法中获取所需的EventHubClient。
2.使用其他DI引擎。例如: Unity Container,使用它可以获得如下服务:container.Resolve<IService>(key)
3.创建一个保存EventHubClient的类。
public class EventHubClientHolder
{
public string Name;
public EventHubClient eventHubClient;
} public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSingleton<EventHubClientHolder>(_ => { return new EventHubClientHolder() { Name = "A", eventHubClient = ..... }; });
services.AddSingleton<EventHubClientHolder>(_ => { return new EventHubClientHolder() { Name = "B", eventHubClient = ..... }; });
} public HomeController(ILogger<HomeController> logger, IEnumerable<EventHubClientHolder> services)
{
_logger = logger;
_services = services;
} public IActionResult Index()
{
var eventHubClient = _services.First(_ => _.Name.Equals("A"))).eventHubClient;
return View();
}https://stackoverflow.com/questions/59260088
复制相似问题