Search code examples
nservicebus5

NServiceBus 5.2 Nhibernate fluent mapping


Please help? My NServiceBus endpoint is:

    public class EndpointConfig : IConfigureThisEndpoint
{
    public void Customize(BusConfiguration busConfiguration)
    {
        var windsorContainer = new WindsorContainer();
        windsorContainer.Install(new IocInstaller());

        busConfiguration.UseContainer<WindsorBuilder>(x => x.ExistingContainer(windsorContainer));

        var nhConfiguration = new NHibernate.Cfg.Configuration();
        nhConfiguration.Properties["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
        nhConfiguration.Properties["connection.driver_class"] = "NHibernate.Driver.Sql2008ClientDriver";
        nhConfiguration.Properties["dialect"] = "NHibernate.Dialect.MsSql2008Dialect";
        nhConfiguration.GetClassMapping(typeof(ProductAchievementMap));

        busConfiguration.UsePersistence<NHibernatePersistence>().UseConfiguration(nhConfiguration);

        busConfiguration.UseSerialization<XmlSerializer>();
    }
}

My handler is:

    public class ProductAchievementAuditCommandHandler : IHandleMessages<ProductAchievementAuditCommand>
{
    public ISession Session { get; set; }

    public void Handle(ProductAchievementAuditCommand message)
    {
        var productAchievementAudit = new ProductAchievement
        {
            Id = Guid.NewGuid(),
            SapComId = message.SapComId,
            MessageId = message.MessageId
        }; 

        Session.Save(productAchievementAudit);
    }
}

My fluent mapping is:

    public class ProductAchievementMap : ClassMap<ProductAchievement>
{
    public ProductAchievementMap()
    {
        Table("ProductAchievementMessage");
        Id(x => x.Id);
        Map(x => x.SapComId);
        Map(x => x.MessageId);
    }
}

The error I'm getting is:"ERROR NServiceBus.GenericHost Exception when starting endpoint. System.InvalidOperationException: No NHibernate properties found in your config". According to the documentation I need to use busConfiguration.UsePersistence<NHibernatePersistence>().RegisterManagedSessionInTheContainer();. To get public ISession Session { get; set; } in the handler. How do I get the fluent mapping to work? Any help greatly appreciated.


Solution

  • I finally figured it out HOORAY!! The key is the changes in syntax in NServiceBus 5.2 Not sure if this is the best way but it works. The Endpoint Config now looks like this.

        public class EndpointConfig : IConfigureThisEndpoint
    {
        public void Customize(BusConfiguration busConfiguration)
        {
            var nhConfiguration = new Configuration();
            nhConfiguration.Properties["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
            nhConfiguration.Properties["connection.driver_class"] = "NHibernate.Driver.Sql2008ClientDriver";
            nhConfiguration.Properties["dialect"] = "NHibernate.Dialect.MsSql2008Dialect";
            nhConfiguration.Properties["connection.connection_string"] = @"Data Source=SQL_DEV\DEVELOPMENT;Initial Catalog=SPM.Auditlog;Integrated Security=True";
    
            var newConfig = Fluently.Configure(nhConfiguration)
                .Mappings(x => {
                                    x.FluentMappings.AddFromAssemblyOf<ProductAchievementMap>();
                                }).BuildConfiguration();
    
            busConfiguration.UsePersistence<NHibernatePersistence>().UseConfiguration(newConfig);
            busConfiguration.UsePersistence<NHibernatePersistence>().RegisterManagedSessionInTheContainer();
    
            var windsorContainer = new WindsorContainer();
            windsorContainer.Install(new IocInstaller());
    
            busConfiguration.UseContainer<WindsorBuilder>(x => x.ExistingContainer(windsorContainer));
    
            busConfiguration.UseSerialization<XmlSerializer>();
        }
    }