MT3 Request-Response with asp.net mvc application

373 views
Skip to first unread message

Slava Buchkou

unread,
Oct 14, 2015, 1:50:28 PM10/14/15
to masstransit-discuss
Hi,
I have the following code inside asp.net mvc application
        public ActionResult Index()
        {
            var busControl = Bus.Factory.CreateUsingRabbitMq(
                c =>
                {
                    c.Host(
                        new Uri("rabbitmq://localhost/test"),
                        h =>
                        {
                            h.Username("guest");
                            h.Password("guest");
                        });
                });
            busControl.Start();

            busControl.CreateRequestClient<GetUserByEmailRequest, GetUserByEmailResponse>(
                new Uri("rabbitmq://localhost/test/user_services"),
                TimeSpan.FromSeconds(5))
                .Request(new GetUserByEmailRequest())
                .Wait();
            return View();
        }

This code works perfectly inside console application, but in asp.net application it just freezes application without any exceptions and messages.
RabbitMQ management plugin shows no new exchanges or queues and messages.

MT version: 3.0.14
.NET version 4.5
ASP.NET MVC version: 5.2.3

Thank you,
Slava

Chris Patterson

unread,
Oct 14, 2015, 2:12:54 PM10/14/15
to masstrans...@googlegroups.com
You should look at the example that hooks into MVC. 


For one you should not be creating a bus in a controller action method. So take a look at the patterns in the example. 

__
Chris Patterson




--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/38badc64-a2bc-4486-b288-28f68cae53ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Slava Buchkou

unread,
Oct 14, 2015, 3:53:10 PM10/14/15
to masstransit-discuss
Thank you for reference. Seems sample works.
But it uses Publish/Subscribe pattern instead of request/response.

I've tried to use bus.Request in the provided sample but without luck. I suppose, request should be sent and without response timeout exception thrown.

Please, help to get working sample for request/response pattern.

Thank you,
Slava
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

Chris Patterson

unread,
Oct 14, 2015, 5:34:44 PM10/14/15
to masstrans...@googlegroups.com
There is a separate request/response sample:



To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

Ludovic Lebel

unread,
Nov 1, 2015, 11:30:54 AM11/1/15
to masstransit-discuss
Hi Chris

I'm having some hard time too figuring out how to properly make request/response call in the context of a asp.net mvc application.
The sample on github is a console application and it seems to behave differently. There is something specific to a controller's action around threading.

If you could provide a working recipe fir this that would be great.

Thanks

Ludovic  
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

Ludovic Lebel

unread,
Nov 1, 2015, 11:56:35 AM11/1/15
to masstransit-discuss
So, here what I've figure out :

I have this controller action :

public ActionResult GetBatchSubscriptions(int districtId)
{
     ViewData["DistrictId"] = districtId;
     var response = _myCustomersReadSideClient.GetBatchSubscriptionDescriptions(districtId);
      return PartialView("BatchSubscriptions", response.List);
}

_myCustomersReadSideClient is an object encapsulating the call to MT. Inside, it looks like that :


public BatchSubscriptionDescriptionList GetBatchSubscriptionDescriptions(int districtId)
{

BatchSubscriptionDescriptionList response = null;

response = Task.Run(() =>
{
var client = CreateRequestClient<GetBatchSubscriptionDescriptionsRequest, BatchSubscriptionDescriptionList>();
return client.Request(new GetBatchSubscriptionDescriptionsRequest(districtId));
}).Result;

return response;
}

I have try to use instead, (without the thread wrapper) and is didn't worked:

var client = CreateRequestClient<GetBatchSubscriptionDescriptionsRequest, BatchSubscriptionDescriptionList>();
return client.Request(new GetBatchSubscriptionDescriptionsRequest(districtId)).Result;


Chris, does that make sense to you?

Ludovic

mald

unread,
Nov 3, 2015, 9:27:20 PM11/3/15
to masstransit-discuss
To anybody having trouble with Request/Response in MVC, please look at this sample I quickly made. Hope it helps.


Good luck!

Wayne Brantley

unread,
Nov 13, 2015, 9:54:12 PM11/13/15
to masstransit-discuss
Your issue is all around async/await.  You are encountering the classic deadlock.  If you ever use sometask.Result you have most likely messed up.  This kind of thing works find outside of asp.net as the context is not an issue.  Lots of articles about this (http://stackoverflow.com/questions/15021304/an-async-await-example-that-causes-a-deadlock) and is not specific to mass transit.
However, I know that it was not until a really recent version that ConfigureAwait(false) was put in lots of places, which is key to avoiding these deadlocks.


Make your controller action and GetBatchSubscriptionDescriptions async, then use await on that call as well as the calls in GetBatchSubscriptionDescriptions.   Your problem will go away.


For more options, visit https://groups.google.com/d/optout.

Ludovic Lebel

unread,
Nov 14, 2015, 7:40:47 PM11/14/15
to masstransit-discuss
Thanks for the answer Wayne.

--
You received this message because you are subscribed to a topic in the Google Groups "masstransit-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/masstransit-discuss/jqALcC9z0fs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

Jedidja Bourgeois

unread,
Jul 28, 2016, 4:16:14 PM7/28/16
to masstransit-discuss
Thanks! Came in handy today :)
Reply all
Reply to author
Forward
0 new messages