Question

I got a issue with NServiceBus 4.4.2, which has me stomped. I self host NSB inside a MVC application. The instance is put into an Autofac container and injected into the controllers. So far, so good.

Sending a message with SendLocal (a command) works as expected. The message turns up in MSMQ in JSON, as I have NSB configured to do so.

Now, my message handler is called, which is also part of my MVC app. For simplicity, I do not inject anything into this handler right now. In any case, the message, which turns up, is empty (e.g. properties are null or Guid.Empty).

When I change the serialization of NSB to XML, it works. It looks like, as if the same instance uses JSON for serialization und XML for deserialization.

Here is the code I use to initialize the Bus:

        Configure.Transactions.Enable();
        Configure.Serialization.Json();
        Configure.Features.Disable<Sagas>();
        Feature.Disable<XmlSerialization>();

        Instance =
            Configure.With(
                AllAssemblies.Matching("Messages1.dll").And("Handler1.dll").And("Messages2.dll").And("Handler2.dll"))
                .DefineEndpointName(ConfigurationManager.AppSettings["ServiceBusEndpointName"])
                .DefaultBuilder()
                .Log4Net<RollingFileAppender>(x => x.File = ConfigurationManager.AppSettings["ServiceBusLog"])
                .RavenSubscriptionStorage()
                .UseTransport<Msmq>()
                .PurgeOnStartup(false)
                .UnicastBus()
                .RunHandlersUnderIncomingPrincipal(false)
                .CreateBus()
                .Start(() => Configure.Instance.ForInstallationOn<Windows>()
                    .Install());

If have no clue what's going on here :-( Does anyone have an idea?

Kind regards Marco Heine

Was it helpful?

Solution

Thanks for all the help,

but as so often, the error lies between chair and keyboard :-/

Yes, all my observations were seemingly all correct, but for all the wrong reasons. The problem was simple: My message class had no parameterless constructor and only private setters. It seems, that while the XML-Serializer doesn't have a problem with this, the JSON serializer does.

After making the properties get / setters public, I finally get my message returned. Bummer.

Especially a big thank you towards Particular, who made a big effort in trying to help me. I guess, all I needed were a few days off =)

OTHER TIPS

Are you referencing NServiceBus.Autofac in your solution?

Seems like the bus is configured to use two different containers and this is causing weird side effects.

   Instance =
        Configure.With(
            AllAssemblies.Matching("Messages1.dll").And("Handler1.dll").And("Messages2.dll").And("Handler2.dll"))
            .DefineEndpointName(ConfigurationManager.AppSettings["ServiceBusEndpointName"])
            .DefaultBuilder()

NServiceBus has the Autofac merged into it. Calling DefaultBuilder() uses the internal container.

Have a read of their documentation

You basically need to call

Configure.With.AutofacBuilder(pass container here) and there's no need to register the instance returned from Start() function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top