【发布时间】:2022-04-17 20:52:26
【问题描述】:
我正在 Dotnet 核心中的 Redis(StackExchange.Redis) PUBSUB 系统上工作,一切正常,直到我发布到我的频道。订阅者永远不会被触发。
这是我的代码:
program.cs
public static void Main(string[] args)
{
_ = RunConsumer();
BuildWebHost(args).Run();
}
private static async Task RunConsumer()
{
LogWriter logger = new LogWriter();
IEnvironment envProvider = new EnvironmentProvider();
try
{
ICache cache = new Cache(envProvider);
IDataRepository dataRepo = new DatabaseRepository();
PubBusiness publisher = new PubBusiness();
await publisher.ImportData();
}
catch (Exception ex)
{
await logger.WriteLogErrorAsync($"Exception occurred", ex);
Environment.Exit(-1);
}
}
出版商
// ... code that creates my key
// add my data to a batch and save it to redis
batchTasks.Add(batch.ListRightPushAsync("jobs", key));
batch.Execute();
Task.WaitAll(batchTasks.ToArray());
// publishing
ISubscriber sub = _connection.GetSubscriber();
await sub.PublishAsync("uploaded", key);
订阅者
var db = _connection.GetDatabase();
ISubscriber sub = _connection.GetSubscriber();
// it will never pass here
await sub.SubscribeAsync("uploaded", async (channel, value) =>
{
var key = (await db.ListLeftPopAsync("jobs")).ToString();
// do my stuff here
});
【问题讨论】:
标签: .net-core redis publish-subscribe stackexchange.redis subscribe