0
votes

I have a need to create an NServiceBus using CustomConfigurationSource to replace my app.config.

I have implemented CustomConfiguration source in my code, but I get the following exception when creating the bus:

System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=NServiceBus.Core StackTrace: at NServiceBus.Unicast.UnicastBus.NServiceBus.IStartableBus.Start(Action startupAction) in c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs:line 852

I create my bus in class A as follows:

public class A: IA
{
    public static IBus Bus { get; private set; }

    public A()
    {
        CreateBus();
    }

    private void CreateBus()
    {
        Bus = NServiceBus.Configure.With()
            .CustomConfigurationSource(new AConfigSource())
            .Log4Net()
            .DefaultBuilder()
            .XmlSerializer()
            .MsmqTransport()
                .IsTransactional(false)
                .PurgeOnStartup(false)
            .UnicastBus()
                .LoadMessageHandlers()
                .ImpersonateSender(false)
            .CreateBus()
            .Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install());
            // Exception thrown here ******
    }
}

My configuration class:

public class AConfigSource: IConfigurationSource
{
    #region Implementation of IConfigurationSource

    public T GetConfiguration<T>() where T : class, new()
    {
        if (typeof(T) == typeof(MessageForwardingInCaseOfFaultConfig))
            return MessageForwardingInCaseOfFaultConfiguration() as T;

        if (typeof(T) == typeof(MsmqTransportConfig))
            return MsmqTransportConfiguration() as T;

        if (typeof(T) == typeof(UnicastBusConfig))
            return UnicastBusConfiguration() as T;

        // leaving the rest of the configuration as is:
        return ConfigurationManager.GetSection(typeof(T).Name) as T;

    }

    #endregion Implementation of IConfigurationSource

    #region Private

    private MessageForwardingInCaseOfFaultConfig MessageForwardingInCaseOfFaultConfiguration()
    {
        return new MessageForwardingInCaseOfFaultConfig
        {
            ErrorQueue = "error"
        };
    }

    private UnicastBusConfig UnicastBusConfiguration()
    {
        return new UnicastBusConfig();
    }

    private MsmqTransportConfig MsmqTransportConfiguration()
    {
        return new MsmqTransportConfig
        {
            ErrorQueue = "error",
            InputQueue = "myInputQueue",
            MaxRetries = 5,
            NumberOfWorkerThreads = 1
        };
    }

    #endregion Private
}

What causes the NullReferenceException? What steps have I missed to override all my nservicebus configurations?

Update: I am using NServiceBus version 3.3.8.

Update: Based upon John's answer, I changed my private UnicastBusConfiguration method as follows:

    private UnicastBusConfig UnicastBusConfiguration()
    {
        return new UnicastBusConfig
        {
            ForwardReceivedMessagesTo = "",
            MessageEndpointMappings = new MessageEndpointMappingCollection()
        };
    }

I still get the same exception mentioned above.

Here is the config I need to run in class A:

<configuration>
  <configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
  </configSections>
  <MsmqTransportConfig ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5" />
  <UnicastBusConfig ForwardReceivedMessagesTo="">
    <MessageEndpointMappings></MessageEndpointMappings>
  </UnicastBusConfig>
  <MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NServiceBus" publicKeyToken="9fc386479f8a226c" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="NServiceBus.Core" publicKeyToken="9fc386479f8a226c" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

More information: I am running a WPF application. I added a class library to my solution to host nservicebus. I create an instance of object "Class A" mentioned above. I need nservicebus to configure based upon app.config for class A, not app.config for my WPF startup project.

Maybe CustomConfigurationSource is overkill? Is there a better way to command NServiceBus to configure using app.config in Class A project?

Update: I receive the following error that indicates a missing UnicastBus.cs class. I have reference to NserviceBus.Host in my project...

Locating source for 'c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs'. (No checksum.) The file 'c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs' does not exist. Looking in script documents for 'c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs'... Looking in the projects for 'c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs'. The file was not found in a project.

1
can you raise an issue here with a repro github.com/Particular/NServiceBus/issuesSimon

1 Answers

0
votes

Just guessing but I would say you probably need to initialise some of the UnicastBusConfig properties.