0

Problem: NSB won't use our Custom NHB ConnectionProvider.

Im configuring NSB in code with the following setup (log4net is the only thing in the app.config file):

        NServiceBus.SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure);

        NServiceBus.Configure.With()
            .CustomConfigurationSource(ObjectFactory.GetInstance<IConfigurationSource>())
                .StructureMapBuilder()
                .XmlSerializer()
                .DBSubcriptionStorage(GetNHibernateConfiguration(), false)
                .MsmqTransport()
                    .IsTransactional(false)
                    .PurgeOnStartup(false)
                    .UnicastBus()
                    .LoadMessageHandlers( First<MyEventHandler1>
                                            .Then<MyEventHandler2>())
                    .CreateBus()
            .Start();

And this works as long as I do not use DBSubscription storage instead of MSMQ storage. But I need the DBStorage.

Currently we have our own custom ConnectionProvider, which works fine in about 10-15 other projects, but when I try to use it with NSB i get an error which dosent make any sense to me. If I leave out the ConnectionProvider and go with standard NHB then it works just fine.

Custom Provider:

    public class MyProvider : DriverConnectionProvider
    {
        public override IDbConnection GetConnection()
        {
            var oracleRoleProvider = new OracleRoleProvider();
            var dbConnection = Driver.CreateConnection();
            return oracleRoleProvider.SetUserRoles(dbConnection);            }
    }

Config which works with standard NHB provider (but this is not what I want):

        retval.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        retval.Add("connection.driver_class", "NHibernate.Driver.OracleDataClientDriver");
        retval.Add("connection.connection_string", "User Id=user;Password=pass;Pooling=False;Data Source=test");
        retval.Add("dialect", "NHibernate.Dialect.Oracle10gDialect");

Config that doesnt work, but should:

        retval.Add("connection.provider", "MyNamespace.MyProvider, MyNamespace");
        retval.Add("connection.driver_class", "NHibernate.Driver.OracleDataClientDriver");
        retval.Add("connection.connection_string", "User Id=user;Password=pass;Pooling=False;Data Source=test");
        retval.Add("dialect", "NHibernate.Dialect.Oracle10gDialect");

Exception thrown:

       Exception:
       Could not instantiate connection provider:   MyProvider

       Inner:
       Unable to cast object of type 'MyProvider' to type 'NHibernate.Connection.IConnectionProvider'.

Versions used

       NServiceBus: 2.5.0.1476
       NHibernate: 3.1.0.4000

Can anyone shed some light on this issue?

I'm pulling out my hair here sine obviously MyProvider implements IConnectionProvider via DriverConnectionProvider :) :S.

Kind regards

Christian Mikkelsen
  • 1,661
  • 2
  • 19
  • 44

1 Answers1

1

NServiceBus merges and internalizes NHibernate. This means that the class you inherited is not that same as the one NSB is referencing (DriverConnectionProvider). The only workaround in NSB 2.5 is to use the core-only version of NSB that doesn't merge any dependencies. This will all change in 3.0 when we move the NH support to a separate dll with no merging.

Another option would be to build your own substorage (read: copy & paste then NSB one)

Hope this helps!

Adam Fyles
  • 6,030
  • 1
  • 23
  • 29
Andreas Öhlund
  • 5,263
  • 20
  • 24
  • Hi, thanks for the reply and sorry for my late response. We are still only using the free version for evaluation, so we don't have access to the core only version. But as we will upg to the full version very soon and since we wont have a release for the first month or so, we should be ready to use version 3.0, when it comes out. Thx :) – Christian Mikkelsen Aug 16 '11 at 10:27