如何在 .net Core 3.0 中将 SignalR 添加到 Worker Service
How to Add SignalR to Worker Service in .net Core 3.0
我正在开发一个使用 Kafka 消息的辅助服务。
我想在检索到消息时创建一个 signalR 事件,但目前无法使其正常工作。
public static async Task Main(string[] args)
{
using (var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddTransient<IKafkaClient, KafkaClient>();
services.AddSignalR();
})
.Build())
{
// Start the host
await host.StartAsync();
// Wait for the host to shutdown
await host.WaitForShutdownAsync();
}
}
在我的工人服务中
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly IKafkaClient _kafkaClient;
private readonly IHubContext<NotificationHub> _notificationHub;
public Worker(ILogger<Worker> logger,
IKafkaClient kafkaClient,
IHubContext<NotificationHub> notificationHub)
{
_logger = logger;
_kafkaClient = kafkaClient;
_notificationHub = notificationHub;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// code here
}
}
在启动时 AddAdditionalServices
方法添加:
services.AddSingleton<NotificationHub>()
然后将其注入您的工作器 class,例如:
private readonly IServiceProvider serviceProvider;
private NotificationHub NotificationHub
{
get
{
return this.serviceProvider.GetRequiredService<NotificationsHub>();
}
}
您需要创建要从 class 中调用的集线器方法,例如:
public async Task BroadcastMessage(string message){
// your code here
}
所以你可以在 worker 中调用它。
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// code here
string message = // serialize kafka message
await NotificationHub.BroadcastMessage(message)
}
我通过在后台服务的 ExecuteAsync 方法中添加 Task.Yield() 来解决我的问题。
我正在开发一个使用 Kafka 消息的辅助服务。 我想在检索到消息时创建一个 signalR 事件,但目前无法使其正常工作。
public static async Task Main(string[] args)
{
using (var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddTransient<IKafkaClient, KafkaClient>();
services.AddSignalR();
})
.Build())
{
// Start the host
await host.StartAsync();
// Wait for the host to shutdown
await host.WaitForShutdownAsync();
}
}
在我的工人服务中
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly IKafkaClient _kafkaClient;
private readonly IHubContext<NotificationHub> _notificationHub;
public Worker(ILogger<Worker> logger,
IKafkaClient kafkaClient,
IHubContext<NotificationHub> notificationHub)
{
_logger = logger;
_kafkaClient = kafkaClient;
_notificationHub = notificationHub;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// code here
}
}
在启动时 AddAdditionalServices
方法添加:
services.AddSingleton<NotificationHub>()
然后将其注入您的工作器 class,例如:
private readonly IServiceProvider serviceProvider;
private NotificationHub NotificationHub
{
get
{
return this.serviceProvider.GetRequiredService<NotificationsHub>();
}
}
您需要创建要从 class 中调用的集线器方法,例如:
public async Task BroadcastMessage(string message){
// your code here
}
所以你可以在 worker 中调用它。
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// code here
string message = // serialize kafka message
await NotificationHub.BroadcastMessage(message)
}
我通过在后台服务的 ExecuteAsync 方法中添加 Task.Yield() 来解决我的问题。