NServiceBus.GenericHost Exception when starting endpoint

Hi,

I am currently facing error while running the NSB worker. The error details as logged in NSB log file is as follows :-

2017-12-05 10:09:28.494 INFO DefaultFactory Logging to 'C:<project path worker>\bin\Debug' with level Info
2017-12-05 10:09:28.711 ERROR NServiceBus.GenericHost Exception when starting endpoint.
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.Assembly.GetTypes()
at NServiceBus.Hosting.Profiles.ProfileManager.GetImplementor[T](Type openGenericType) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Profiles\ProfileManager.cs:line 60
at NServiceBus.GenericHost.PerformConfiguration(Action`1 moreConfiguration) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\GenericHost.cs:line 99
at NServiceBus.GenericHost.Start() in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\GenericHost.cs:line 58

The exception i.e. captured in visual studio is :-
System.Reflection.ReflectionTypeLoadException was unhandled
Message: An unhandled exception of type ‘System.Reflection.ReflectionTypeLoadException’ occurred in mscorlib.dll
Additional information: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

I have tried looking for help in SO based on the post -

However, the debugger is not hitting the endpoint config. The exception is throw prior to the same. ’
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{// debugger here
public void Customize(BusConfiguration configuration)
{
try
{
var endPointName = ConfigurationManager.AppSettings[“endpoint”];
//other config params.
}
catch
{}
}
}

Any suggestions.

There seems to be some issues with the assembly scanning done by the NServiceBus host

can you list the exact versions of the assemblies that you are using and any assembly redirects you have in place?

The exact assembly version are -
“EntityFramework” version=“6.1.3” targetFramework="net451"
“Microsoft.Data.Edm” version=“5.6.0” targetFramework="net451"
“Microsoft.Data.OData” version=“5.6.0” targetFramework="net451"
“Microsoft.Data.Services.Client” version=“5.6.0” targetFramework="net451"
“Microsoft.WindowsAzure.ConfigurationManager” version=“1.8.0.0” targetFramework="net451"
“Newtonsoft.Json” version=“5.0.6” targetFramework="net451"
“NLog” version=“3.2.1” targetFramework="net451"
“NServiceBus” version=“5.2.17” targetFramework="net451"
“NServiceBus.Azure.Transports.WindowsAzureStorageQueues” version=“6.2.1” targetFramework="net451"
“NServiceBus.Host” version=“6.0.0” targetFramework="net451"
“System.Spatial” version=“5.6.0” targetFramework="net451"
“WindowsAzure.Storage” version=“3.1.0.1” targetFramework=“net451”

Do you have any assembly redirects in your app.config or in the hosts NServiceBus.Host.exe.config ?

I don’t know about Rakesh and his redirects, but I’m experiencing a similar problem and I believe we’ve got the redirects in place, i.e.

2018-01-30 13:41:28.258 INFO  DefaultFactory Logging to 'D:\Octopus\Applications\aTest\JDM.Deploy.NettWebAdmin.Services.Messaging.Endpoint\0.1.0-unstable0191\' with level Info
2018-01-30 13:41:28.274 WARN  NServiceBus.Hosting.Helpers.AssemblyScanner Could not enumerate all types for 'D:\Octopus\Applications\aTest\JDM.Deploy.NettWebAdmin.Services.Messaging.Endpoint\0.1.0-unstable0191\NServiceBus.NHibernate.dll'.
It looks like you may be missing binding redirects in the config file for the following assemblies:
NHibernate, Version=4.0.4.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4
For more information see http://msdn.microsoft.com/en-us/library/7wd6ex19(v=vs.100).aspx

This is the NServiceBus.Host.exe.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.4.4000" newVersion="4.0.4.4000" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Iesi.Collections" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Castle.Windsor" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.4.0.0" newVersion="3.4.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

(The same entries are found in app.config as well). Because of this (annoying) error we’re unable to deploy this particular endpoint and it is getting a wee-bit frustrating to say the least.

What version of NServiceBus.NHibernate are you running?

Would you be able to share with solution with us so that we can debug it?

Cheers,

Andreas

We’re running NServiceBus.NHibernate v7.2.2
Not sure if I’m allowed to share the project with you. I’ll have to check with the “gold owner” tomorrow.

I think it might be the Iesi.Collections reference that is causing the issue, NServiceBus.NHibernate is compiled against 4.0.1.4000 can you try to change that redirect as well?

So I made sure that Iesi.Collections v4.0.1.4000 is used by the project in question.

<package id="Iesi.Collections" version="4.0.1.4000" targetFramework="net452" />

      <dependentAssembly>
        <assemblyIdentity name="Iesi.Collections" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.4000" newVersion="4.0.1.4000" />
      </dependentAssembly>

Still the same run-time error

I’ve created a new project with the following references

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Iesi.Collections" version="4.0.1.4000" targetFramework="net471" />
  <package id="NHibernate" version="4.0.4.4000" targetFramework="net471" />
  <package id="NServiceBus" version="6.4.3" targetFramework="net471" />
  <package id="NServiceBus.Host" version="7.0.2" targetFramework="net471" />
  <package id="NServiceBus.NHibernate" version="7.2.2" targetFramework="net471" />
</packages>

and it starts up fine without any assembly redirects, are you using the same version?

1 Like

Ours is a 4.5.2 project. I made sure that we’re referencing the same assembly versions as you outline above, i.e.

<packages>
  <package id="BouncyCastle" version="1.8.1" targetFramework="net452" />
  <package id="Castle.Core" version="3.3.3" targetFramework="net452" />
  <package id="Castle.WcfIntegrationFacility" version="3.1.0" targetFramework="net452" />
  <package id="Castle.Windsor" version="3.4.0" targetFramework="net452" />
  <package id="Dapper" version="1.50.2" targetFramework="net452" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  <package id="FluentTime" version="0.1.0.0" targetFramework="net452" />
  <package id="Iesi.Collections" version="4.0.1.4000" targetFramework="net452" />
  <package id="log4net" version="1.2.10" targetFramework="net452" />
  <package id="MailKit" version="1.18.1.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.SignalR.Core" version="2.2.2" targetFramework="net452" />
  <package id="Microsoft.AspNet.SignalR.SqlServer" version="2.2.2" targetFramework="net452" />
  <package id="Microsoft.Owin" version="3.1.0" targetFramework="net452" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net452" />
  <package id="MimeKit" version="1.18.1" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
  <package id="NHibernate" version="4.0.4.4000" targetFramework="net452" />
  <package id="NServiceBus" version="6.4.3" targetFramework="net452" />
  <package id="NServiceBus.CastleWindsor" version="6.0.0" targetFramework="net452" />
  <package id="NServiceBus.Host" version="7.0.2" targetFramework="net452" />
  <package id="NServiceBus.Metrics" version="1.1.1" targetFramework="net452" />
  <package id="NServiceBus.NHibernate" version="7.2.2" targetFramework="net452" />
  <package id="NServiceBus.Serilog" version="3.0.3" targetFramework="net452" />
  <package id="NServiceBus.SqlServer" version="3.0.1" targetFramework="net452" />
  <package id="Owin" version="1.0" targetFramework="net452" />
  <package id="Polly" version="4.3.0" targetFramework="net452" />
  <package id="Serilog" version="2.3.0" targetFramework="net452" />
  <package id="Serilog.Enrichers.Environment" version="2.1.0" targetFramework="net452" />
  <package id="Serilog.Settings.AppSettings" version="2.0.0" targetFramework="net452" />
  <package id="Serilog.Sinks.RollingFile.Extension" version="1.0.5" targetFramework="net452" />
  <package id="simple-config" version="1.4.0" targetFramework="net452" />
</packages>

Without any assembly redirects NServiceBus chokes on the Castle.Windsor reference, so the NServiceBus.Host.exe.config file now contains

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Castle.Windsor" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.4.0.0" newVersion="3.4.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>

What happens when I run the endpoint using F5 is that I don’t see any loader exception(s), only the following message and then the end point shuts down.

2018-01-30 15:50:08.804 INFO  DefaultFactory Logging to 'C:\repos\opp-nettwebadmin\BackEnd\NettWebAdmin.Services.Messaging.Endpoint\bin\Debug\' with level Info

Seems to be caused by more loader exceptions.

We’re up and running. Thx, Andreas.

Btw, have you got a link to an example on how to use this EndpointConfiguration.UseTransport<T>().Routing()?

Happy to hear you got it going!

In general this is how you use the routing API to route commands:

I see that you’re on SqlServer transport so for events you would need explicit routing as well since it’s utilizing “Message driven pub sub”:

Does this help?

Cheers,

Andreas

Yes. we’re using SqlServer transport due to high availability requirements. I know I’m a bit lazy now, but what I’m trying to do is to go from this:

  <UnicastBusConfig>
    <MessageEndpointMappings>
      <add Messages="Admin.Services.Messaging.Endpoint.Contracts" Endpoint="admin.services.messaging.endpoint" />
      <add Messages="ADifferentSystem.Messages" Endpoint="ADifferentSystem.Messaging.Endpoint@adsTransport" />
    </MessageEndpointMappings>
  </UnicastBusConfig>

to configuration in code. I’ll read the articles provided, but if you’ve got a code example for me using the settings above I’d be more than happy :wink:

That depends if the messages are events or not, for events:

var routing = transport.Routing();
routing.RegisterPublisher(
    assembly: typeof(SomeTypeInTheAsm).Assembly,
    destination: "admin.services.messaging.endpoint");

for commands

var routing = transport.Routing();
routing.RouteToEndpoint(
    assembly: typeof(SomeTypeInTheAsm).Assembly,
    destination: "admin.services.messaging.endpoint");

Look at the articles mentioned for options to only register subsets of the assembly types if needed.

Hope this help!

Cheers,

Andreas

Thanks, Andreas. Just what I was looking for. :slight_smile: