Using NServiceBus with Asp.Net MVC 2
Asked Answered
N

3

7

Is there a way to using NServiceBus with Asp.Net MVC 2? I want to send a request message from a Asp.Net MVC2 Application to a Service, which handle the message and reply with a response message. is there a way to do this clearly?

Nicolle answered 8/9, 2010 at 14:17 Comment(1)
What is your specific use case? Can you provide more detail about what you are trying to accomplish, so I don't have to guess?Chop
P
14

If you really want to do this, here's how:

var sync = Bus.Send<SomeMessage>(/*data*/)
    .Register((AsyncCallback)delegate(IAsyncResult ar) {
        var result = ar.AsyncState as CompletionResult;

        // do something with result.Messages
    },
    null
);

sync.AsyncWaitHandle.WaitOne(/*timeout*/);
Ppm answered 9/9, 2010 at 3:55 Comment(0)
A
18

There is a reason that NServiceBus only supports registering callback for status codes and ints. That reason is that you shouldn't use NServiceBus for sync request/response style communications, those scenarios is best solved with frameworks like Wcf, NNibernate , EF, Ado.net etc.

You should look at only use NSB for async parts of your application like to send off "commands" to backend services for processing.

An in depth explanation can be found here:

http://andreasohlund.net/2010/04/22/messaging-shouldnt-be-used-for-queries

If you still want to do request/response with NSB you'll have to work for it :) using a messagehandler for your response that updates some cache in your MVC app. With that in place you can do some ajax style polling to determine when the data arrives.

Hope this helps!

Apoenzyme answered 8/9, 2010 at 16:32 Comment(1)
+1 I was trying to understand why I need NServiceBus for sync req/resp(as you said it is not meant to be a sync req/resp technology) and I found your answer very usefulPopulace
P
14

If you really want to do this, here's how:

var sync = Bus.Send<SomeMessage>(/*data*/)
    .Register((AsyncCallback)delegate(IAsyncResult ar) {
        var result = ar.AsyncState as CompletionResult;

        // do something with result.Messages
    },
    null
);

sync.AsyncWaitHandle.WaitOne(/*timeout*/);
Ppm answered 9/9, 2010 at 3:55 Comment(0)
P
0

If this is a greenfield project, I would strongly recommend thinking about using Command Query Separation pattern which would help you logically separate concerns regarding that change state in the business domain (command) and queries which acts only as state description mechanism. Then Commands can be implemented in NServiceBus and Queries using WCF for instance.

Plumbism answered 9/9, 2010 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.