找到你要的答案

Q:How to do request/response with a consumer

Q:如何做请求/响应与消费者

On the Request/Respond page in the documentation it shows a response like this

Bus.Initialize(sbc => { 
   sbc.UseMsmq(); 
   sbc.UseMulticastSubscriptionClient(); 
   sbc.ReceiveFrom("msmq://localhost/message_responder"); 
   sbc.Subscribe(subs=> { 
     subs.Handler<BasicRequest>( (cxt, msg )=> { 
       cxt.Respond(new BasicResponse{Text = "RESP"+msg.Text}); 
     }); 
   });
})

In my application I'm using Consumers rather than Handlers (because I need dependency injection via Autofac) so my consumer looks like this

public class DoctorImporter : Consumes<ImportDoctorCommand>.For<Guid>
{
    public void Consume(ImportDoctorCommand cmd) {
        foo.DoStuff();
        var ev = new ImportDoctorCommand.Occurred() { CorrelationId = cmd.CorrelationId };
        bus.Publish(ev);
    }
    readonly IServiceBus bus;
    readonly Foo foo;
    public DoctorImporter(IServiceBus bus, Foo foo) {
        this.bus = bus;
        this.foo = foo;
    }
    public Guid CorrelationId { get; private set; }
}

but this doesn't work because it seems like I need .Respond() rather than .Publish() and there's no .Respond() on IServiceBus.

So how do I do request/response from a consumer?

在文档中的请求/响应页,它显示了这样的响应

Bus.Initialize(sbc => { 
   sbc.UseMsmq(); 
   sbc.UseMulticastSubscriptionClient(); 
   sbc.ReceiveFrom("msmq://localhost/message_responder"); 
   sbc.Subscribe(subs=> { 
     subs.Handler<BasicRequest>( (cxt, msg )=> { 
       cxt.Respond(new BasicResponse{Text = "RESP"+msg.Text}); 
     }); 
   });
})

在我的应用程序中我使用的消费者而不是处理(因为我需要依赖注入通过autofac)所以我的消费者看起来像这样

public class DoctorImporter : Consumes<ImportDoctorCommand>.For<Guid>
{
    public void Consume(ImportDoctorCommand cmd) {
        foo.DoStuff();
        var ev = new ImportDoctorCommand.Occurred() { CorrelationId = cmd.CorrelationId };
        bus.Publish(ev);
    }
    readonly IServiceBus bus;
    readonly Foo foo;
    public DoctorImporter(IServiceBus bus, Foo foo) {
        this.bus = bus;
        this.foo = foo;
    }
    public Guid CorrelationId { get; private set; }
}

但这并不是因为它似乎是我需要的。respond()不如。publish()没有respond()在iservicebus。

那么,我如何做消费者的请求/响应?

c#  masstransit