【问题标题】:dotnet core Subscriber is not being triggered after Publishing发布后未触发 dotnet core 订阅者
【发布时间】: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


    【解决方案1】:

    默认禁用消息传递,点击链接了解详情 但是如果很短,只需在 redis cli 中输入此命令即可启用所有:

    CONFIG SET notify-keyspace-events KEA

    https://redis.io/docs/manual/keyspace-notifications/#configuration

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      相关资源
      最近更新 更多