toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.53k stars 258 forks source link

Use of Type.GetType in OverrideRuntimeNodeWriterTemplateTypeNamePhase causes crash when using ILMerge #546

Closed General-Fault closed 2 hours ago

General-Fault commented 3 weeks ago

Describe the bug RazorLight cannot be used in an application that has been merged into a single assembly using ILMerge. ILMerge combines all referenced assemblies into one. This has the side-effect of changing the assembly name for all types that have been merged. So, when the OverrideRuntimeNodeWriterTemplateTypeNamePhase attempts to locate the Microsoft.AspNetCore.Razor.Language.DefaultRazorCSharpLoweringPhase type in the Register method, it fails because the AssemblyQualifiedName for that type no longer contains "Microsoft.AspNet.Core.Razor.Language" as the assembly name. The result is that OverrideRuntimeNodeWriterTemplateTypeNamePhase.Register throws a RazorLightException with the error "SetTemplateTypePhase cannot be registered as DefaultRazorCSharpLoweringPhase could not be located".

See also #322

To Reproduce Create a console application for .Net Framework 4.8 using RazorLight. ~I used the RazorLightSandbox and updated the async using statements in Main() to comply with C#7.3.~ Install the ILMerge.MSBuild.Task package (see https://github.com/emerbrito/ILMerge-MSBuild-Task/wiki) Compile the project. There should be an ILMerge directory in the output.

Run the generated executable.

You should see the error

Exception System.AggregateException: One or more errors occurred. ---> RazorLight.RazorLightException: SetTemplateTypePhase cannot be registered as DefaultRazorCSharpLoweringPhase could not be located at RazorLight.Instrumentation.OverrideRuntimeNodeWriterTemplateTypeNamePhase.Register(RazorProjectEngineBuilder builder) at RazorLight.DefaultRazorEngine.<>cDisplayClass1_0.b0(RazorProjectEngineBuilder builder) at Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Create(RazorConfiguration configuration, RazorProjectFileSystem fileSystem, Action`1 configure) at RazorLight.DefaultRazorEngine.get_Instance() at RazorLight.RazorLightEngineBuilder.Build()

Expected behavior The library should be usable in an assembly that has been merged with ILMerge.

Information (please complete the following information):

Additional context The application that this applies to is a portable tool used by our development team to create API documentation. The tool must be a single executable with all dependencies "wrapped in". Unfortunately, other dependencies of this tool are still targeting the .Net Framework v4.8.

General-Fault commented 3 weeks ago

Admittedly, my "steps to reproduce" are not ideal. I will see if I can put together a simple project that replicates this behavior.

toddams commented 2 hours ago

ILMerge utility is not supported and I don't recall any plans to support it. Considering that it has been deprecated, I'm closing this one as "won't do"