1
votes

I have an experimental NServiceBus project running which has:

  • MyProject.Core - message publisher using NServiceBus.Host.exe
  • MyProject.Subscriber = message subscriber 1 using NServiceBus.Host.exe
  • MyProject.ConsoleSubscriber - .NET console app with explicit init and subscription
  • MyProject.WebApp - ASP.NET MVC 2 web application

When I fire up the project, the NSB hosted subscriber is receiving messages from the core - but the web app and console app aren't; the breakpoint inside the message handler is never hit and I don't get any diagnostic output in the VS output window.

The console app has the following code:

using System;
using MyProject.Messages.PublishedByCore;
using NServiceBus;

namespace MyProject.ConsoleSubscriber {
    class Program {
        static void Main(string[] args) {

            var bus = NServiceBus.Configure.With()
                .Log4Net()
                .DefaultBuilder()
                .XmlSerializer()
                .MsmqTransport()
                    .IsTransactional(false)
                    .PurgeOnStartup(false)
                .UnicastBus()
                    .ImpersonateSender(false)
                .CreateBus()
                .Start();
            bus.Subscribe<FileChangedMessage>();
            while (Console.ReadLine() != null) { }
        }
    }

    public class FileChangedMessageHandler : IHandleMessages<FileChangedMessage> {
        public void Handle(FileChangedMessage message) {
            Console.WriteLine(message.FilePath);
        }
    }
}

and the following App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
        <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
    </configSections>
    <MsmqTransportConfig InputQueue="MyConsoleClient" ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5" />
    <UnicastBusConfig>
        <MessageEndpointMappings>
            <add Messages="MyProject.Messages.PublishedByCore" Endpoint="MyProjectCoreInputQueue" />
        </MessageEndpointMappings>
    </UnicastBusConfig>
</configuration>

The web application's Global.asax.cs is as follows:

using System.Diagnostics;
using System.Web.Mvc;
using System.Web.Routing;
using MyProject.Messages.PublishedByCore;
using NServiceBus;

namespace MyProject.WebApp {
    public class MvcApplication : System.Web.HttpApplication {
        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        public static IBus Bus { get; set; }

        protected void Application_Start() {
            Bus = NServiceBus.Configure.WithWeb()
                .Log4Net()
                .DefaultBuilder()
                .XmlSerializer()
                .MsmqTransport()
                    .IsTransactional(false)
                    .PurgeOnStartup(false)
                .UnicastBus()
                    .ImpersonateSender(false)
                .CreateBus()
                .Start();
            Bus.Subscribe<FileChangedMessage>(); RegisterRoutes(RouteTable.Routes);
        }
    }

    public class FileChangedMessageHandler : IHandleMessages<FileChangedMessage> {
        public void Handle(FileChangedMessage message) {
            Debug.WriteLine(message.FilePath);
        }
    }
}

and the relevant section of the Web.config file is:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                    <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
                    <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                    <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                    <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                </sectionGroup>
            </sectionGroup>
        </sectionGroup>
        <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
        <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
    </configSections>
    <MsmqTransportConfig InputQueue="MyWebClient" ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5" />
    <UnicastBusConfig>
        <MessageEndpointMappings>
            <add Messages="MyProject.Messages.PublishedByCore" Endpoint="MyProjectCoreInputQueue" />
        </MessageEndpointMappings>
    </UnicastBusConfig>

As far as I can see, the configuration and initialisation code in both the web and console subscribers is identical other than Congfigure.With() vs Configure.WithWeb() - so I can't understand why this isn't working. Is there some additional configuration required to actually subscribe to a message queue from a .NET web application that I'm missing?

1

1 Answers

3
votes

D'oh.

Both apps need to call .LoadMessageHandlers() - and I need to learn to distinguish between NServiceBus diagnostic logging and my own message handler output...

var bus = NServiceBus.Configure.With()
    .Log4Net()
    .DefaultBuilder()
    .XmlSerializer()
    .MsmqTransport()
        .IsTransactional(false)
        .PurgeOnStartup(false)
    .UnicastBus()
        // THIS IS THE LINE THAT MAKES IT WORK!
        .LoadMessageHandlers()
        .ImpersonateSender(false)
    .CreateBus()
    .Start();

Oops.