Question

I have an endpoint, called UploadService, that contains a saga started by a command defined in another assembly. The commands in that assembly are registered in the endpoint's initialization via the following lines:

public class EndpointInitialization : IWantCustomInitialization
    {
        public void Init()
        {
            NServiceBus.Configure.Features.Enable<NServiceBus.Features.Sagas>();

            NServiceBus.Configure.With()
              .Log4Net()
              .UseInMemoryTimeoutPersister()
              .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands"))
              .DefiningMessagesAs(t => t.Namespace != null && t.Namespace.Contains("Messages"))
              .UnicastBus()
                 .ImpersonateSender(false)
              .CreateBus()
              .Start();
        }
    }

However, when start the endpoint via Debug->Start new instance, NServiceBus.Core throws the following exception:

"The saga 'MySaga' implements 'IHandleMessages`1' but the message type 'MyCommand' is not classified as a message. You should either use 'Unobtrusive Mode Messages' or the message should implement either 'IMessage', 'IEvent' or 'ICommand'."

I have a seprate endpoint which references the same Commands assembly, and has custom initialization defined the same way, and starts up just fine (only difference is the other endpoint does not use sagas).

UPDATE: I'm using NServiceBus 4.2

Was it helpful?

Solution

Yes. Sagas do work in Unobtrusive mode. Call your initialization code in the class that implements IConfigureThisEndpoint, where the instance of the Bus is created and that should fix the problem for you.

For example:

 public class EndpointConfig: IConfigureThisEndpoint, AsA_Publisher,  IWantCustomInitialization
{
    public void Init()
    {
        var container = new WindsorContainer();
        Configure.With()
            .CastleWindsorBuilder(container)
            .FileShareDataBus(@"..\..\..\DataBusShare\")
            .DefiningDataBusPropertiesAs(p => p.Name.EndsWith("DataBus"))
            .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands"))
            .DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events"))
            .DefiningMessagesAs(t => t.Namespace == "Messages")
            .DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted"))
            .UseNHibernateSagaPersister()
            .UseNHibernateTimeoutPersister()
            .UseNHibernateSubscriptionPersister();
        Configure.Serialization.Json();

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