Search code examples
nservicebusnservicebus3

Type ... was not registered in the serializer (non web app)


I am trying to send a command with NServiceBus 3.0. Everything works when I send a command that implements ICommand. However, it does not work if I use the conventions. The source is below. Can someone tell me what I am doing wrong?

public class Program
{
    public static NServiceBus.IBus Bus { get; private set; }

    static void Main(string[] args)
    {
        ConfigureBus();

        var command = new RouteTradeCommand() { TradeXml = "<trade />" };
        Bus.Send("BrokerQueue@DATPCDI041", command);
    }

    private static void ConfigureBus()
    {
        Bus = Configure.With()
            .DefaultBuilder()
            .XmlSerializer()
            .MsmqTransport()
            .UnicastBus()
            .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Messages"))
            .SendOnly();
    }
}

The command looks like this:

namespace Messages
{
    public class RouteTradeCommand : IRouteTradeCommand
    {
        public string TradeXml { get; set; }
    }   
}

Solution

  • Unfortunately, NSB is dependent on the ordering of the config methods. It'll work if you move the DefiningCommandsAs method to right after With:

    NServiceBus.Configure.WithWeb()
    .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith(".Commands"))
    .Log4Net()
    .DefaultBuilder()
    .XmlSerializer()
    .MsmqTransport()
    .DefineEndpointName("Web")
    .UnicastBus()
    .SendOnly();