NServiceBus 5 with RabbitMQ transport throwing exception while enabling UnicastBus

送分小仙女□ 提交于 2019-12-11 07:52:18

问题


I'm attempting to use NServiceBus with RabbitMQ in a self-hosted scenario. I've obtained the source for the NServiceBus and NServiceBus.RabbitMQ repos on github to track down the issues I've had so far, so the version I'm using is the source on their repos as of yesterday.

Here is my configuration:

        var busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("RMAQueue");
        busConfiguration.AssembliesToScan(typeof(RMACommand).Assembly);
        busConfiguration.Conventions()
            .DefiningCommandsAs(type => type.Namespace != null && type.Namespace.StartsWith("RMAInterfaces.Commands.", StringComparison.Ordinal));
        busConfiguration.Conventions()
            .DefiningEventsAs(type => type.Namespace != null && type.Namespace.StartsWith("RMAInterfaces.Events.", StringComparison.Ordinal));
        busConfiguration.Conventions()
            .DefiningMessagesAs(type => type.Namespace != null && type.Namespace.StartsWith("RMAInterfaces.Messages.", StringComparison.Ordinal));
        busConfiguration.UseTransport<RabbitMQTransport>();
        busConfiguration.Transactions().Disable();
        busConfiguration.PurgeOnStartup(true);
        busConfiguration.UseSerialization<NServiceBus.JsonSerializer>();

        busConfiguration.DisableFeature<SecondLevelRetries>();
        busConfiguration.DisableFeature<StorageDrivenPublishing>();
        busConfiguration.DisableFeature<TimeoutManager>();

        busConfiguration.UsePersistence<InMemoryPersistence>();
        busConfiguration.EnableInstallers();

        var bus = Bus.Create(busConfiguration);

I am getting an exception on the Bus.Create() line:

{"The given key (NServiceBus.LocalAddress) was not present in the dictionary."}

Following the stack from it leads me to see that it's failing while enabling the Feature UnicastBus.

Here is my app config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
    <section name="AuditConfig" type="NServiceBus.Config.AuditConfig, NServiceBus.Core" />
  </configSections>
  <MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />
  <UnicastBusConfig>
    <MessageEndpointMappings>
      <add Messages="RMAInterfaces" Endpoint="RMAQueue@localhost" />
    </MessageEndpointMappings>
  </UnicastBusConfig>
  <connectionStrings>
    <add name="NServiceBus/Transport" connectionString="host=localhost" />
    <add name="NServiceBus/Persistence" connectionString="host=localhost"/>
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <!--<AuditConfig 
    QueueName="The address to which messages received will be forwarded."
    OverrideTimeToBeReceived="The time to be received set on forwarded messages, specified as a timespan see http://msdn.microsoft.com/en-us/library/vstudio/se73z7b9.aspx"  />-->
  <AuditConfig QueueName="audit" />
</configuration>

What am I missing to be able to self-host NServiceBus using a RabbitMQ transport?


回答1:


I have just had this exact problem, and the fix is as Andreas suggested. I added the following to my configuration code...

configuration.AssembliesToScan(typeof(NServiceBus.Transports.RabbitMQ.IManageRabbitMqConnections).Assembly);

With that in place the error message regarding NServiceBus.LocalAddress no longer shows up. My complete setup is as follows (code, then config file)

[TestMethod]
public void TestMethod1()
{
    LogManager.Use<Log4NetFactory>();
    var configuration = new BusConfiguration();
    configuration.AssembliesToScan(typeof(NServiceBus.Transports.RabbitMQ.IManageRabbitMqConnections).Assembly);
    configuration.UseSerialization<JsonSerializer>();
    configuration.UseTransport<NServiceBus.RabbitMQTransport>();
    configuration.UsePersistence<InMemoryPersistence>();

    var bus = global::NServiceBus.Bus.Create(configuration);
    bus.Start();
}



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, Log4net" />
  </configSections>

  <MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />

  <connectionStrings>
    <add name="NServiceBus/Transport" connectionString="host=localhost" />
  </connectionStrings>

  <UnicastBusConfig>
    <MessageEndpointMappings>
      <add Assembly="TestNSBCreation" Endpoint="TestNSBCreation" />
    </MessageEndpointMappings>
  </UnicastBusConfig>

  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ContactAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="C:\logs\TestNSBCreation.log" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>

</configuration>



回答2:


The Morgan Skinner's answer helped figure out the actual root cause of my issue. I've added the suggested code configuration.AssembliesToScan(typeof(NServiceBus.Transports.RabbitMQ.IManageRabbitMqConnections).Assembly);

After that the exception was more descriptive. NServiceBus.RabbitMQ package automatically downloads RabbitMQ.Client package. The later package was updated to the latest available on nuget and this created version conflict between what expected by NserviceBus.RabbitMQ and what was actually installed. I've removed both packages completely and reinstalled NserviceBus.RabbitMQ package and the error went away. The ..assembliesToScan... line was no longer needed either.



来源:https://stackoverflow.com/questions/27275494/nservicebus-5-with-rabbitmq-transport-throwing-exception-while-enabling-unicastb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!