New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssemblyScanner enforcing core reference prevents ILMerging #5311

Open
danielmarbach opened this Issue Dec 17, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@danielmarbach
Copy link
Member

danielmarbach commented Dec 17, 2018

As part of a customer case I got asked whether core can be ILMerged. The requirement of ILMerging core comes from Dynamics CRM 365. Apparently, Dynamics CRM 365 allows a plugin to be only one assembly.

https://blogs.msdn.microsoft.com/crm/2010/11/09/how-to-reference-assemblies-from-plug-ins/
https://community.dynamics.com/365/sales/b/thrivestechnicaldynamics365blog/archive/2017/02/27/external-assemblies-in-dynamics-crm-plugins-with-ilmerge

The problem is though that the core can't be ILMerged anymore starting from v6 and higher. When NServiceBus is ILMerged into an assembly the scanner logic skips the assembly because it looks to the scanner like it does not reference the core (GetReferencedAssemblies() will not return the core as a reference, see https://github.com/Particular/NServiceBus/blob/develop/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs#L186). Loading an assembly with an ILMerged core will lead to

System.Exception: Can't find any behaviors/connectors for the root context (NServiceBus.Pipeline.ITransportReceiveContext)
   at NServiceBus.PipelineModelBuilder.Build()
   at NServiceBus.StepRegistrationsCoordinator.BuildPipelineModelFor[TRootContext]()
   at NServiceBus.Pipeline`1..ctor(IBuilder builder, PipelineModifications pipelineModifications)
   at NServiceBus.ReceiveComponent.<Initialize>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at NServiceBus.StartableEndpoint.<Start>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at NServiceBus.Endpoint.<Start>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ConsoleApp2.Program.<Main>d__0.MoveNext()

see also https://discuss.particular.net/t/cant-find-any-behaviors-connectors-for-the-root-context/441/2

Tried to hack around that problem by doing the following

            // workaround to avoid 
            //  Can't find any behaviors/connectors for the root context (NServiceBus.Pipeline.ITransportReceiveContext)
            var types = Assembly.GetExecutingAssembly().GetTypes();
            var methodInfo = endpointConfiguration.GetType()
                .GetMethod("TypesToScanInternal", BindingFlags.Instance | BindingFlags.NonPublic);
            methodInfo.Invoke(endpointConfiguration, new object[] { types });

which then leads to the next problem

2018-12-17 15:52:44.514 FATAL NServiceBus.Features.LicenseReminder Failed to initialize the license
System.Exception: No ReleaseDateAttribute could be found in assembly, please make sure GitVersion is enabled
   at Particular.Licensing.ReleaseDateReader.GetReleaseDate()
   at Particular.Licensing.LicenseExpirationChecker.HasLicenseExpired(License license)
   at Particular.Licensing.LicenseSourceResult.<>c.<DetermineBestLicenseSourceResult>b__12_3(LicenseSourceResult p)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at Particular.Licensing.LicenseSourceResult.DetermineBestLicenseSourceResult(LicenseSourceResult[] sourceResults)
   at Particular.Licensing.LicenseSourceHKLMRegKey.Find(String applicationName)
   at Particular.Licensing.ActiveLicense.<>c__DisplayClass0_0.<Find>b__0(LicenseSource licenseSource)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Particular.Licensing.ActiveLicense.Find(String applicationName, LicenseSource[] licenseSources)
   at NServiceBus.LicenseManager.InitializeLicense(String licenseText, String licenseFilePath)
   at NServiceBus.Features.LicenseReminder.Setup(FeatureConfigurationContext context)

Repro https://github.com/danielmarbach/ILMerge.NServiceBus

@danielmarbach danielmarbach added Bug and removed Bug labels Dec 17, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment