我正在使用servicestack和iredissubscriber。我已经让它工作了,触发了onmessage。然而,有时它不会触发,我试图找出原因。
基本设置为:
RedisClientManager = new PooledRedisClientManager("localhost:6379");
_mqServer = new RedisMqServer(RedisClientManager, retryCount: 2)
{
RequestFilter = RequestFilter
};
_mqServer.Start(); //Starts listening for messages
在另一节课上,我有:
public MqChannelSubscriber(string eventChannelName, Action<CoreRequest> onMessageReceived)
{
_redisClient = MqClientFactory.Instance.GetRedisClient();
_subscription = _redisClient.CreateSubscription();
_subscription.OnSubscribe = channel => Log.Instance.LogInfo($"Subscription started on {eventChannelName}");
_subscription.OnUnSubscribe = channel => Log.Instance.LogWarning($"Unsubscribed from {eventChannelName}");
_subscription.OnMessage = (channel, msg) =>
{
try
{
onMessageReceived(GetRequest(msg));
}
catch (Exception ex)
{
Log.Instance.LogException(ex);
}
};
Task.Run(() => _subscription.SubscribeToChannels(eventChannelName));
}
在本例中,eventchannelname是“objectbroadcast”。
问题1:如果我手动地,例如使用redis insight,添加一个类型为list的名为“objectbroadcast”的新键,并添加一个条目,我希望上面会触发onmessage,但事实并非如此。为什么?
问题2:我有另一个应用程序生成这些“广播”,通过这样做:
public static void Broadcast<T>(T coreBroadcast) where T : CoreBroadcast
{
// Option 1: this will trigger the OnMessage above
using (var redisClient = MqClientFactory.Instance.GetRedisClient())
{
string json = JsonConvert.SerializeObject(coreBroadcast, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
redisClient.PublishMessage(coreBroadcast.BroadcastChannel, json);
}
// Option 2: this will not trigger the OnMessage above
using (var messageQueueClient = MqClientFactory.Instance.CreateMessageQueueClient())
{
messageQueueClient.Publish(coreBroadcast.BroadcastChannel, new Message<T>(coreBroadcast));
}
}
以上是两种发送方式:iredisclient.publishmessage和imessagequeueclient.publish。如注解中所述,选项1将触发onmessage,但选项2不会。为什么?
在选项2中,我在redis中的objectbroadcast中看到生成的json,挥之不去,永远不会被检索到:
暂无答案!
目前还没有任何答案,快来回答吧!