如何在rebus中实现简单的回复

时间:2014-10-18 23:37:02

标签: rebus

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);
    }
}

我正在使用上面的代码向消费者发送消息。消费者将使用总线。回复,但上面的代码显然不起作用。

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

1 个答案:

答案 0 :(得分:0)

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

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

如果您有兴趣查看一些演示请求/回复的示例代码,请查看the integration sample中的the Rebus samples repository,其中有一些简单的请求/回复在客户端之间进行(这对应于您的情况下的生产者)和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();

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

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

我希望您现在可以看到为什么您的代码不起作用。如果我需要进一步说明,请告诉我。)

我已将request/reply sample添加到the Rebus samples repository,以证明上面显示的代码可以实际运行(前提是您删除....等 - 当然基本了解C#以便能够使用此代码)