1

我的基础设施:

  • Main - ServiceStack 自托管控制台应用程序。'Main' 向 MQ 发送消息。
  • 背景 - ServiceStack 自托管控制台应用程序。“后台”从 MQ 接收消息。
  • 本地安装的 Redis

在 'Main' AppHost 我配置 Redis 管理器:

container.Register<IRedisClientsManager>(
    new PooledRedisClientManager("localhost:6379"));

然后我在服务的某个地方运行这段代码:

using (var client = new RedisMessageQueueClient(TryResolve<IRedisClientsManager>()))
{
    client.Publish(new TestMessage { Value = "From ping" });
}

一切都很好,我可以在我的“背景”中收到消息。但是当我在类中包装这段代码时问题就来了:

public class MessageQueuePublisher : IMessageQueuePublisher
{
    public void Publish(object message)
    {
        using (var client = new RedisMessageQueueClient(
            EndpointHost.AppHost.TryResolve<IRedisClientsManager>()))
        {
            client.Publish(message);
        }
    }
} 

当我从执行先前代码的完全相同的位置调用 MessageQueuePublisher.Publish 方法时,它似乎工作正常(没有引发异常),但我的消息没有到达“后台”。

这个可以吗?

4

1 回答 1

2

I found a solution. On my 'Background' I expect message with type TestMessage

mqService.RegisterHandler<TestMessage>(ServiceController.ExecuteMessage);

But when using MessageQueuePublisher.Publish message was of type object and went to the object queue and wasn't handled.

So to solve this problem Publish method should be generic:

public void Publish<T>(T message)

It doesn't change how method is called but code is not so good because if you look at it, it's not clear why generic is used. But at least it works.

于 2013-08-29T18:47:43.653 回答