views:

238

answers:

4

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?

A: 

You can set up a repository, with the NServiceBus code behind it.

Robert Harvey
How does this help me to reply a message to the Asp.Net Application
Peter Mueller
I have one Asp.Net Mvc 2 Webapplication and some Services. For example I have a Userservice to manage the users. I use NServiceBus to send Messages between the webapplication and the userservice. Now I want to list all Users in the webapplication. To do this the webapplication send a message like "GetAllUsersRequestMessage" to the userservice. What the webapplication has to do now is to wait for a reply from the userservice. The userservice has to reply a "GetAllUserResponseMessage" with a list of all Users. When this Message arrived at the webapp it has should to show the users in a webpage.
Peter Mueller
OK. Do you know how to create a repository for a database, in an ASP.NET MVC application? You would do the same thing for the NServiceBus. Once you have created the repository, you will have a method in the repository that will call the UserService, and return a collection of user names. You would display this list of names in your web page the same way you would display a list of names from a database repository.
Robert Harvey
See also http://nerddinnerbook.s3.amazonaws.com/Part3.htm
Robert Harvey
The code on the userservice is not the problem. The problem is how to send the request-message from the webapplication and say that the webapplication has to wait until the response-message with the userlist arrived or until a timeout. I see a helpfull sample here: http://blog.codebrain.co.uk/post/2009/05/07/Asynchronous-MVC-Controllers-2b-NServiceBus.aspx But this sample only can return an errorcode and not a response message. Also this sample use The MVC 1 and not the MVC 2.
Peter Mueller
That is a good code example you linked there. ASP.NET MVC 1.0 code is compatible with ASP.NET MVC 2.0. All you will need to do is modify it so that it returns the data you want, rather than an error code.
Robert Harvey
+5  A: 

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.blogspot.com/2010/04/messaging-shouldn-be-used-for-queries.html

If you still want to do reg/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!

Andreas
+2  A: 

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*/);
Udi Dahan
Thanks, that works very nice and do all what I want.
Peter Mueller
A: 

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.

mgamer