Particular / NServiceBus

Build, version, and monitor better microservices with the most powerful service platform for .NET
https://particular.net/nservicebus/
Other
2.1k stars 647 forks source link

NServiceBus.Host scanning all assemblies despite having EndpointConfigurationType configured #5172

Closed Richiban closed 5 years ago

Richiban commented 6 years ago

I have an endpoint that I am trying to "upgrade" to Core 5.2.19, Host 6.0.0. For legacy reasons, we can't go any higher than 5.2.19.

When running my endpoint I get assembly scanning exceptions:

Unhandled Exception: Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type WindowsHost, key "********.Search.IndexFeeder.EndpointConfiguration, ********.Search.IndexFeeder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" ---> System.Exception: Could not enumerate all types for 'C:\Source\********\Marketplace\********.Search\********.Search.IndexFeeder\bin\Debug\SolrNet.dll'.
Exceptions:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

System.IO.FileNotFoundException: Could not load file or assembly 'Ninject, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' or one of its dependencies. The system cannot find the file specified.
File name: 'Ninject, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7'

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

System.TypeLoadException: Method 'CreateQuery' in type 'NHibernate.SolrNet.Impl.DelegatingSession' from assembly 'SolrNet, Version=0.4.0.2002, Culture=neutral, PublicKeyToken=864c75541c350ae7' does not have an implementation.
System.TypeLoadException: Method 'CreateQuery' in type 'NHibernate.SolrNet.Impl.DelegatingSession' from assembly 'SolrNet, Version=0.4.0.2002, Culture=neutral, PublicKeyToken=864c75541c350ae7' does not have an implementation.
System.IO.FileNotFoundException: Could not load file or assembly 'StructureMap, Version=2.6.2.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223' or one of its dependencies. The system cannot find the file specified.
File name: 'StructureMap, Version=2.6.2.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223'

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

System.IO.FileNotFoundException: Could not load file or assembly 'Autofac, Version=2.5.2.830, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The system cannot find the file specified.
File name: 'Autofac, Version=2.5.2.830, Culture=neutral, PublicKeyToken=17863af14b0044da'

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

   at NServiceBus.Hosting.Helpers.AssemblyScanner.ScanAssembly(String assemblyPath, AssemblyScannerResults results) in C:\Build\src\NServiceBus.Core\Hosting\Helpers\AssemblyScanner.cs:line 153
   at NServiceBus.Hosting.Helpers.AssemblyScanner.GetScannableAssemblies() in C:\Build\src\NServiceBus.Core\Hosting\Helpers\AssemblyScanner.cs:line 63
   at NServiceBus.GenericHost..ctor(IConfigureThisEndpoint specifier, String[] args, List`1 defaultProfiles, String endpointName, IEnumerable`1 scannableAssembliesFullName) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\GenericHost.cs:line 33
   at NServiceBus.Hosting.Windows.WindowsHost..ctor(Type endpointType, String[] args, String endpointName, IEnumerable`1 scannableAssembliesFullName) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\WindowsHost.cs:line 21
   at NServiceBus.Hosting.Windows.HostServiceLocator.DoGetInstance(Type serviceType, String key) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\HostServiceLocator.cs:line 31
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 49
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 53
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 103
   at Topshelf.Internal.ServiceController`1.BuildInstance() in c:\Projects\TopShelfForNSB\src\Topshelf\Internal\ServiceController.cs:line 93
   at Magnum.StateMachine.EventActionList`1.Execute(T stateMachine, Event event, Object parameter) in :line 0   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at NServiceBus.Hosting.Windows.Program.Main(String[] args) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\Program.cs:line 150

I have manually configured the assemblies to scan in my Customize(BusConfiguration) method, but this exception is being thrown before my endpoint configuration is even being run.

Reading the docs at https://docs.particular.net/nservicebus/hosting/nservicebus-host/?version=host_6#endpoint-configuration it explicitly states that "The scanning process can be avoided if the class containing endpoint's configuration is explicitly stated".

I have the following nservicebus.host.exe.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="EndpointConfigurationType"
         value="********.Search.IndexFeeder.EndpointConfiguration, ********.Search.IndexFeeder"/>
  </appSettings>
</configuration>

but the error still occurs.

This seems like a bug. Has it been fixed in NSB v5?

andreasohlund commented 6 years ago

The method you describes avoids the "outer" scanning, ie when the host scans to find your "IConfigureThisEndpoint". The scanning to find handlers etc still takes place in the separate appdomain that is created to run the actual endpoint. You need to use the scanner settings to configure exclusions:

https://docs.particular.net/nservicebus/hosting/assembly-scanning?version=core_5

Does that make any sense?

I will see if I can update the doco to make this more clear.

Richiban commented 6 years ago

Hi @andreasohlund, I'm pretty sure it's the "outer" scanning that's throwing the exception.

My IConfigureThisEndpoint is not even being run.

To prove this to myself, I have replaced the entire thing with:

    public class EndpointConfiguration : IConfigureThisEndpoint
    {
        public void Customize(BusConfiguration config)
        {
            throw new Exception("Bang");
        }
    }

in addition to putting in a breakpoint, Sure enough, it's not being run.

If it's not even being run, then I can't configure assembly scanning?

andreasohlund commented 6 years ago

Ok I see it now, you're correct since the outer scanning doesn't happen we're doing a fresh scan immediately

https://github.com/Particular/NServiceBus.Host/blob/support-6.0/src/NServiceBus.Hosting.Windows/GenericHost.cs#L29

The only workaround I see is to actually fix the scanning error with some asm redirect in your app.config or to pass the assemblies that you want scanned on the cmd-line like described in

https://docs.particular.net/nservicebus/hosting/nservicebus-host/#endpoint-configuration-assembly-scanning

@Particular/nservicebus-maintainers @Particular/host-maintainers thoughts on this one?

Our docs seems to be incorrectly stating that:

Alternatively, it's possible to control which assemblies should be scanned. That can be done in code by implementing IConfigureThisEndpoint interface:

That isn't true since we will blow up before reaching user code?

Richiban commented 6 years ago

Thanks; while passing assemblies on the command line does work (I've just tried it) it's fine for dev; it's not going to be a good solution for us for production--it's going to be a real ****ache to fit it into our deployment process.

I shall try fixing the scanning error first. I'm not very confident however, since there assemblies that are "not found" that I'm not even referencing and are not present in the bin folder. What's looking for them?

andreasohlund commented 6 years ago

Any chance you could create a small repro solution that we can look at to help you get the scanning issue sorted?

andreasohlund commented 6 years ago

@Richiban any progress on this, perhaps we can get on a webcall and look at the problem if you prefer that?

Richiban commented 6 years ago

@andreasohlund Hi Andreas, thanks so much for the offer! Unfortunately I've had to put this down for the moment as something more urgent has come up (as it always seems to do!), so as soon as I pick it up again I might take you up on that offer.

andreasohlund commented 6 years ago

As an aside we're deprecating the Host (as of core v8) and recommend our users to move to self hosting instead so that might be an option as well

https://docs.particular.net/nservicebus/upgrades/host-7to8#migrating-procedure

timbussmann commented 5 years ago

I'm closing this as there hasn't been any updates for more than a year. If this issue is still a problem, please get in touch with our support at https://particular.net/support.