3
public static void SendREsbDx(Job job)
{
    using (var adapter = new BuiltinContainerAdapter())
    {
        adapter.Handle<ReplyMsg>(msg =>
        {
            string mss = msg.message;
        });

        Configure.With(adapter)
            .Logging(l => l.ColoredConsole(LogLevel.Warn))
            .MessageOwnership(o => o.FromRebusConfigurationSection())
            .Transport(t => t.UseSqlServer("server=.;initial catalog=rebus_test;integrated security=true","consumerx","error")
                             .EnsureTableIsCreated())
            .CreateBus()
            .Start();
        adapter.Bus.Send<Job>(job);
    }
}

我正在使用上面的代码向消费者发送消息。消费者会使用bus.Reply,但是上面的代码显然是行不通的。

我只是希望能够收到消费者的回复。这将如何实现?

4

1 回答 1

3

听起来您的消费者没有Job消息处理程序。

在您的情况下,听起来您需要两个总线实例 - 一个具有实现的消费者实例IHandleMessages<Job>will bus.Reply(new ReplyMsg {...}),以及具有实现的生产者实例IHandleMessages<ReplyMsg>willbus.Send(new Job{...})并执行回复处理程序中需要执行的任何操作。

如果您有兴趣查看一些演示请求/回复的示例代码,请查看Rebus 示例存储库中的集成示例,该示例在客户端之间进行了一些简单的请求/回复(对应于您的生产者case)和IntegrationService(对应于消费者)。

下面的代码片段演示了它是如何完成的:

var producer = new BuiltinContainerAdapter();
var consumer = new BuiltinContainerAdapter();

consumer.Handle<Job>(job => {
    ...
    consumer.Bus.Reply(new ReplyMsg {...});
});

producer.Handle<ReplyMsg>(reply => {
    ....
});

Configure.With(producer)
     .Transport(t => t.UseSqlServer(connectionString, "producer.input", "error")
                      .EnsureTableIsCreated())
     .MessageOwnership(o => o.FromRebusConfigurationSection())
     .CreateBus()
     .Start();

Configure.With(consumer)
     .Transport(t => t.UseSqlServer(connectionString, "consumer.input", "error")
                      .EnsureTableIsCreated())
     .MessageOwnership(o => o.FromRebusConfigurationSection())
     .CreateBus()
     .Start();

// for the duration of the lifetime of your application
producer.Bus.Send(new Job {...});


// when your application shuts down:
consumer.Dispose();
producer.Dispose();

Job并且在您的 app.config 中必须有一个映射到的端点映射consumer.input

<rebus>
    <endpoints>
         <add messages="SomeNamespace.Job, SomeAssembly" endpoint="consumer.input"/>
    </endpoints>
</rebus>

我希望您现在可以看到为什么您的代码不起作用。请让我知道我是否应该进一步详细说明:)

我已经向 Rebus 示例存储库添加了一个请求/回复示例,以证明上面显示的代码可以实际运行(当然前提是您删除了 etc - 您需要对 C# 有基本的了解才能使用此代码)....

于 2014-10-19T10:59:07.297 回答