NServiceBus Transport is NULL when using a custom ServiceHostFactory, WAS

Go To StackoverFlow.com

0

We are currently trying to implement NServiceBus 3.0 in our WCF services.

We use a custom ServiceHostFactory to initialize our services in WAS. We use net.tcp to access the services and the following code to set it up:

public class DoServiceServiceHostFactory : DefaultServiceHostFactory
{
    private static IWindsorContainer _container;

    public DoServiceServiceHostFactory()
        : base(CreateKernel())
    {
    }

    private static IKernel CreateKernel()
    {
        _container = new WindsorContainer();

        IocModules.Configure(_container, new WcfConfigurationModule());
        IocModules.Configure(_container, new WcfAdapterModule());
        IocModules.Configure(_container, new ManagerModule());
        IocModules.Configure(_container, new FactoryModule());

        IBus bus = Configure.WithWeb()
                .DefineEndpointName("OurProjectPublisher")
                .DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("MY.Bus.Contracts.Events"))
                .CastleWindsorBuilder()
                .Log4Net()
                .XmlSerializer()
                .MsmqTransport()
                .UnicastBus()
                .CreateBus()
                .Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install());
        //_container.Register(Component.For<IBus>().Instance(bus).LifeStyle.Singleton);

        WindsorServiceLocator serviceLocator = new WindsorServiceLocator(_container);
        ServiceLocator.SetLocatorProvider(() => serviceLocator);

        return _container.Kernel;
    }
}

when we call the service we receive this error:

    Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 33:             IocModules.Configure(_container, new FactoryModule());
Line 34: 
Line 35:             IBus bus = Configure.WithWeb()
Line 36:                     .DefineEndpointName("OurProjectPublisher")
Line 37:                     .DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("MY.Bus.Contracts.Events"))

Source File: xxx\ServiceHostFactory.cs    Line: 35 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   NServiceBus.Unicast.UnicastBus.NServiceBus.IStartableBus.Start(Action startupAction) in d:\BuildAgent-03\work\nsb.masterbuild0\src\unicast\NServiceBus.Unicast\UnicastBus.cs:762
   xxx.ServiceHostFactory.CreateKernel() in xxx\ServiceHostFactory.cs:35
   xxx.ServiceHostFactory..ctor() in xxx\ServiceHostFactory.cs:21

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct& signature, RuntimeType declaringType) +0
   System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +651
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath) +1204
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +50
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +1132

[ServiceActivationException: The service '/MembershipService.svc' cannot be activated due to an exception during compilation.  The exception message is: Exception has been thrown by the target of an invocation..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +890624
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +180062
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +136

And the config section in the web.config

    <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
<section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
<section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
<section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
</configSections>
<MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />
<MsmqTransportConfig NumberOfWorkerThreads="1" MaxRetries="5" />
<UnicastBusConfig ForwardReceivedMessagesTo="auditqueue">
<MessageEndpointMappings>
  <add Messages="MY.Bus.Contracts" Endpoint="OurProjectPublisher" />
</MessageEndpointMappings>
</UnicastBusConfig>

Any ideas about this?

2012-04-04 08:04
by Bjorn Bailleul
Is this only failing in WAS? Have you tried this configuration using a regular host - Udi Dahan 2012-04-04 15:57
If we run the service using NET.TCP and use a factory in the .svc file , NServiceBus fails to start in the factory implementation. If we move the init of NServiceBus to global.asax and use a HTTP binding, it works - Bjorn Bailleul 2012-05-11 08:11


0

Usually (at least in 2.6), you would pass the implementation of your Windsor Container to the .CastleWindsorBuilder configuration. This allows NSB to create the correct object graph when initializing. So, it would look like:

        _container = new WindsorContainer();

        IocModules.Configure(_container, new WcfConfigurationModule());
        IocModules.Configure(_container, new WcfAdapterModule());
        IocModules.Configure(_container, new ManagerModule());
        IocModules.Configure(_container, new FactoryModule());

        IBus bus = Configure.WithWeb()
                .DefineEndpointName("OurProjectPublisher")
                .DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("MY.Bus.Contracts.Events"))
                .CastleWindsorBuilder(_container)  // added here
                .Log4Net()
                .XmlSerializer()
                .MsmqTransport()
                .UnicastBus()
                .CreateBus()
                .Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install());
        //_container.Register(Component.For<IBus>().Instance(bus).LifeStyle.Singleton);

Does that help?

2012-04-04 08:41
by Davin Tryon
No, I tried that before, forgot to put it back -> thanks for pointing it out, but the problem still remains - Bjorn Bailleul 2012-04-04 08:53
@BjornBailleul And I'm assuming that you have configuration section defined for MsmqTransportConfig - Davin Tryon 2012-04-04 09:07
Yes, I added the web.config relevant part abov - Bjorn Bailleul 2012-04-04 09:11
Ads