Search code examples
c#nservicebusnservicebus-sagas

NServiceBus saga data not created


I was wondering if anybody can provide some insight into why in the mysterious world of NServiceBus my saga data isn't created.

Behind all the configuration and XML hell that comes with NBus (by default) I can't seem to get my saga data working. The table for whatever reason is not being created though I am running the endpoint via debug mode using my windows credentials which I have access to SQL and can create tables, sprocs etc.

I can copy and paste my config but this is extensive however I will provide what I deem useful to this post but if you need further info then please shout out and I shall oblige.

I guess I am wondering what additional magic needs to be plumbed in, in order for this to work?

Cheers, DS.

public class EndpointConfig : 
    IConfigureThisEndpoint, 
    AsA_Publisher, 
    IWantCustomInitialization,
    IWantToRunBeforeConfigurationIsFinalized
{
    public void Init()
    {
        Configure.Serialization.Json();
        Configure.With(ScanAssemblies())
            .DefaultBuilder()
            .UseNHibernateSubscriptionPersister()
            .UseNHibernateTimeoutPersister()
            .UseNHibernateSagaPersister()
            .EnablePerformanceCounters()
            .DefiningCommandsAs(MessageTypeDefinition.IsCommand)
            .DefiningEventsAs(MessageTypeDefinition.IsEvent)
            .UnicastBus();

        Configure.Transactions.Advanced(x => x.DisableDistributedTransactions());
    }

    private static Assembly[] ScanAssemblies()
    {
        return new[] { typeof(Foo).Assembly, typeof(EndpointConfig).Assembly };
    }
}

The saga in action..

public class MySaga :
    Saga<SagaData>,
    IAmStartedByMessages<SomeContract >,
    IHandleMessages<SomeOtherContract>
{
    public override void ConfigureHowToFindSaga()
    {
        this.ConfigureMapping<SomeContract>(x => x.Blah).ToSaga(y => y.UniqueRef);
        this.ConfigureMapping<SomeOtherContract>(x => x.Hello).ToSaga(y => y.UniqueRef);
    }

    public void Handle(SomeContract message)
    {
        this.Data.HasFoo = true;
        this.DoStuff();
    }

    public void Handle(SomeOtherContract message)
    {
        this.Data.HasBar = true;
        this.DoStuff();
    }

    private void DoStuff()
    {
        if (this.HasCompleted())
        {
            this.Bus.Send(new Something());
            this.MarkAsComplete();
        }
    }

    private bool HasCompleted()
    {
        return this.Data.HasFoo && this.Data.HasBar;
    }
}

public class SagaData : IContainSagaData
{
    public SagaData()
    {
    }

    public virtual Guid Id { get; set; }

    public virtual string OriginalMessageId { get; set; }

    public virtual string Originator { get; set; }

    public virtual bool HasFoo { get; set; }

    public virtual bool HasBar { get; set; }

    [Unique]
    public virtual string UniqueRef { get; set; }
}

EDIT Adding what version currently being used.

<package id="NHibernate" version="3.3.3.4001" targetFramework="net451" />
  <package id="NServiceBus" version="4.6.10" targetFramework="net451" />
  <package id="NServiceBus.Host" version="4.6.10" targetFramework="net451" />
  <package id="NServiceBus.Interfaces" version="4.6.10" targetFramework="net451" />
  <package id="NServiceBus.NHibernate" version="4.5.5" targetFramework="net451" />

Solution

  • Pretty sure you have to include NServiceBus.NHibernate in the list of assemblies to scan.

    Change Configure.With(ScanAssemblies()) to Configure.With()