Home > front end >  Adding MassTransit Consumer to a Asp.net Framework Web application
Adding MassTransit Consumer to a Asp.net Framework Web application

Time:11-03

I am Tring to add MassTransit Consumer to a Asp.net Framework (.net 4.8) Web application. I not see any sample that use .net framework , all sample point to .NET Core .

Is this Supported by MassTransit in .net Framework (.net 4.8). Is this Long running calls will be killed Off by IIS ?

My sample Code for Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    static IBusControl _bus;
    static BusHandle _busHandle;

    public static IBus Bus
    {
        get { return _bus; }
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        _bus = MassTransit.Bus.Factory.CreateUsingRabbitMq(cfg =>
        {
            cfg.Host(new Uri(ConfigurationManager.AppSettings["RabbitMQHost"]), h =>
            {
                h.Username("admin");
                h.Password("admin");
            });

            cfg.ReceiveEndpoint(RA_Listener.RA_Listener0, e =>
            {
                e.Consumer<EventConsumer>();
            });
        });

        _busHandle = MassTransit.Util.TaskUtil.Await<BusHandle>(() => _bus.StartAsync());
    }

CodePudding user response:

It's only a long-running call if the bus can't connect to your message broker. Otherwise, it should complete almost immediately. So if your broker is unreachable, that's likely the issue.

Also, you should be configuring an ILoggerFactory and providing it to MassTransit if you expect to see any useful log output to help you troubleshoot any issues. Configure logging using LogContext.ConfigureCurrentLogContext(loggerFactory); within the bus configuration closure.

You could just create the bus, as you've done, and use:

Task.Run(() => _bus.StartAsync());

It won't wait, but you also won't know if the bus actually started since you aren't observing the result of the task. But, at least it won't block your startup.

  • Related