Asp.net core MassTransit 5.2,信号员:如何在我的消费者中获取IHubContext?
我的主要问题是获得信号中心的正确实例 上下文:我正在构建一个与几个外部系统通信的Web应用程序。我的应用程序中的CRUD操作会更新外部系统的数据库 在本例中,我有3个服务正在运行: 外部系统|状态机|.NET核心WebAPI 当我发布“createemployee”表单时,一条RabbitMQ消息将从WebAPI发送到statemachine。然后,状态机向更新数据库的外部系统服务发送两条create消息。此后,它更新状态机以跟踪createoperation 表单->API->状态机->外部系统->状态机->API 到目前为止还不错。现在我想使用signar将状态更新发送到客户端。所以我在API中实现了这个消费者:Asp.net core MassTransit 5.2,信号员:如何在我的消费者中获取IHubContext?,asp.net-core,signalr,masstransit,Asp.net Core,Signalr,Masstransit,我的主要问题是获得信号中心的正确实例 上下文:我正在构建一个与几个外部系统通信的Web应用程序。我的应用程序中的CRUD操作会更新外部系统的数据库 在本例中,我有3个服务正在运行: 外部系统|状态机|.NET核心WebAPI 当我发布“createemployee”表单时,一条RabbitMQ消息将从WebAPI发送到statemachine。然后,状态机向更新数据库的外部系统服务发送两条create消息。此后,它更新状态机以跟踪createoperation 表单->API->状态机->外部系
public class UpdatesConsumer :
IConsumer<IExternalSystemUpdateMessage>
{
private readonly IHubContext<UpdatesHub> _updaterHubContext;
public UpdatesConsumer(IHubContext<UpdatesHub> hubContext)
{
_updaterHubContext = hubContext;
}
public Task Consume(ConsumeContext<IExternalSystemUpdateMessage> context)
{
//return _updaterHubContext.Clients.Group(context.Message.CorrelationId.ToString()).SendAsync("SEND_UPDATE", context.Message.Message);// this.SendUpdate(context.Message.CorrelationId, context.Message.Message);
return _updaterHubContext.Clients.All.SendAsync("SEND_UPDATE", context.Message.Message);
}
}
这就是总线和消费者的实例化方式:
public void ConfigureServices(IServiceCollection services)
{
_services = services;
services.AddMvc();
services.AddSignalR();
//services.AddSingleton<IHubContext<UpdatesHub>>();
WebAPI.CreateBus();
}
public static IServiceCollection _services;
static IBusControl _busControl;
public static IBusControl Bus
{
get
{
return _busControl;
}
}
public static void CreateBus()
{
IRMQConnection rmqSettings = Config.GetRMQConnectionConfig("rmq-settings.json", "connection");
_busControl = MassTransit.Bus.Factory.CreateUsingRabbitMq(x =>
{
var host = x.Host(BusInitializer.GetUri("", rmqSettings), h =>
{
h.Username(rmqSettings.UserName);
h.Password(rmqSettings.Password);
});
x.ReceiveEndpoint(host, "externalsystems.update",
e => { e.Consumer(() => new UpdatesConsumer((IHubContext<UpdatesHub>)Startup.__serviceProvider.GetService(typeof(IHubContext<UpdatesHub>)))); });
});
TaskUtil.Await(() => _busControl.StartAsync());
}
public void配置服务(IServiceCollection服务)
{
_服务=服务;
services.AddMvc();
services.AddSignalR();
//services.AddSingleton();
WebAPI.CreateBus();
}
公共电子收款服务;
静态IBusControl(总线控制);;
公共静态IBusControl总线
{
得到
{
返回总线控制;
}
}
公共静态void CreateBus()
{
IRMQConnection rmqSettings=Config.GetRMQConnectionConfig(“rmq settings.json”,“connection”);
_busControl=MassTransit.Bus.Factory.CreateUsingRabbitMq(x=>
{
var host=x.host(BusInitializer.GetUri(“,rmqSettings),h=>
{
h、 用户名(rmqSettings.Username);
h、 密码(rmqSettings.Password);
});
x、 ReceiveEndpoint(主机,“externalsystems.update”,
e=>{e.Consumer(()=>newupdateconsumer((IHubContext)启动。uu serviceProvider.GetService(typeof(IHubContext));});
});
TaskUtil.Await(()=>\u busControl.StartAsync());
}
=========================================================================
所以问题是,我的Consumer类中的_updaterHubContext.Clients总是为空。我已经测试了在控制器中访问集线器,客户端确实出现了:
public class TestController : Controller
{
private readonly IHubContext<UpdatesHub> _hubContext;
public TestController(IHubContext<UpdatesHub> hubContext)
{
_hubContext = hubContext;
}
[HttpGet]
[Route("api/Test/")]
public IActionResult Index()
{
return View();
}
}
公共类TestController:Controller
{
私有只读IHubContext\u hubContext;
公共TestController(IHubContext-hubContext)
{
_hubContext=hubContext;
}
[HttpGet]
[路线(“api/测试/”)]
公共IActionResult索引()
{
返回视图();
}
}
如何在我的Consumer类中获得正确的hub实例?或者如何访问.net正在使用的IServiceCollection
提前通知 为什么不使用注册总线。它应该可以解决您的问题,它将使用
IServiceProvider
解决您的消费者。您可以注册您的消费者,以便MassTransit使用MassTransit.Extensions.DependencyInjection包中提供的支持从IServiceProvider
解决此问题
x.ReceiveEndpoint(host, "externalsystems.update", e =>
{
e.Consumer<UpdatesConsumer>(_serviceProvider);
});
x.ReceiveEndpoint(主机,“externalsystems.update”,e=>
{
e、 消费者(服务提供者);
});
确保也在容器中注册您的
UpdatesConsumer
。这应该为端点上接收的每条消息解析一个新的使用者实例。Thnx!这就是答案。Thnx代表示例页面。这使我的解决方案更加优雅。特别是链接中的示例项目
x.ReceiveEndpoint(host, "externalsystems.update", e =>
{
e.Consumer<UpdatesConsumer>(_serviceProvider);
});