创建订阅类:
using StackExchange.Redis; namespace TestWebApplication.BackgroundTasks { /// <summary> /// Redis 订阅者,长时间运行的后台服务 /// </summary> public class RedisSubscriber : BackgroundService { private readonly IConnectionMultiplexer m_Multiplexer; public RedisSubscriber(IConnectionMultiplexer multiplexer) { m_Multiplexer = multiplexer; } protected override Task ExecuteAsync(CancellationToken stoppingToken) { var subscribver = m_Multiplexer.GetSubscriber(); return subscribver.SubscribeAsync("message", ((channel, value) => { Console.WriteLine($"来自订阅的消息:{value}"); })); } } }
注册后台服务:
services.AddHostedService<RedisSubscriber>();//长时间运行的后台服务
发布消息:
标签:订阅,IConnectionMultiplexer,Multiplexer,Redis,value,后台 From: https://www.cnblogs.com/friend/p/16704238.html