Open afucher opened 3 years ago
I experienced the same problem but with a different error message.
Everything works fine locally, but when I publish it I get:
RazorLight: Can't load metadata reference from the entry assembly. Make sure
PreserveCompilationContext is set to true in *.csproj file
Stack trace:
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly, DependencyContext dependencyContext)
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly)
at RazorLight.Compilation.RoslynCompilationService.EnsureOptions()
at RazorLight.Compilation.RoslynCompilationService.get_ParseOptions()
at RazorLight.Compilation.RoslynCompilationService.CreateSyntaxTree(SourceText sourceText)
at RazorLight.Compilation.RoslynCompilationService.CreateCompilation(String compilationContent, String assemblyName)
at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate)
at RazorLight.Compilation.RazorTemplateCompiler.CompileAndEmit(RazorLightProjectItem projectItem)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
--- End of stack trace from previous location ---
at RazorLight.EngineHandler.CompileTemplateAsync(String key)
at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
at MyApplication.MyTemplateParser.CompileAsync(TemplateDto templateDto)
...
If I set PublishSingleFile to False in the Publish Profile, then the error disappears:
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
...
<PublishSingleFile>False</PublishSingleFile>
...
</PropertyGroup>
</Project>
Additional information
This also happens to me on linux (Ubuntu 20.04).
I experienced the same problem but with a different error message.
Everything works fine locally, but when I publish it I get:
RazorLight: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file Stack trace: at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly, DependencyContext dependencyContext) at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly) at RazorLight.Compilation.RoslynCompilationService.EnsureOptions() at RazorLight.Compilation.RoslynCompilationService.get_ParseOptions() at RazorLight.Compilation.RoslynCompilationService.CreateSyntaxTree(SourceText sourceText) at RazorLight.Compilation.RoslynCompilationService.CreateCompilation(String compilationContent, String assemblyName) at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate) at RazorLight.Compilation.RazorTemplateCompiler.CompileAndEmit(RazorLightProjectItem projectItem) at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey) --- End of stack trace from previous location --- at RazorLight.EngineHandler.CompileTemplateAsync(String key) at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag) at MyApplication.MyTemplateParser.CompileAsync(TemplateDto templateDto) ...
If I set PublishSingleFile to False in the Publish Profile, then the error disappears:
<?xml version="1.0" encoding="utf-8"?> <!-- https://go.microsoft.com/fwlink/?LinkID=208121. --> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> ... <PublishSingleFile>False</PublishSingleFile> ... </PropertyGroup> </Project>
Additional information
* OS: Windows Server 2012, IIS version 8.5 * Platform .NET 5.0 * RazorLight version 2.0.0-rc.3 * Are you using the OFFICIAL RazorLight package? Yes * Visual Studio version: Professional 2019 version 16.8.1.
@marcosdimitrio : You need to add the following into a
<PropertyGroup>
your csproj-file:<PreserveCompilationContext>true</PreserveCompilationContext>
Unfortunately that will not help you fix all errors but the one you're currently encountering. See this for details.
I've done some more digging an am not sure that there is anything this project can do.
RazorLight.Compilation.RoslynCompilationService.GetDependencyContextCompilationOptions
calls
Microsoft.Extensions.DependencyModel.DependencyContext.Load
which calls
Microsoft.Extensions.DependencyModel.DependencyContextLoader.GetDepsJsonPath
That method calls
string assemblyLocation = assembly.Location;
which will always set null
because there is no meaningful value to set in a single-file bundle as it can no longer point to a filename.
(See the file here)
Since no valid value is set the code falls back to calling assembly.CodeBase
which suffers the same problem but will raise an exception instead of returning null
.
@marcosdimitrio : You need to add the following into a
<PropertyGroup>
your csproj-file:<PreserveCompilationContext>true</PreserveCompilationContext>
Unfortunately that will not help you fix all errors but the one you're currently encountering. See this for details.
I did include it in both projects (webapi and razorlight project), it only worked when PublishSingleFile is set to false. When I set PublishSingleFile to true it stops working.
RazorLight indeed does not work in a single-file bundle yet. This is relatively new feature and nobody did any work to support that. But it should be supported, imo
Hi @toddams, is there any work going on in the matter of supporting single-file bundles?
Any update on this? I'm affected by this issue.
Describe the bug I'm trying to use this package but when I publish using
-p:PublishSingleFile=true
, my application throws an error when the code touch this library.To Reproduce Steps to reproduce the behavior:
-p:PublishSingleFile=true
, for example:dotnet publish . -c Release -r win-x64 -p:PublishSingleFile=true
Expected behavior I expected that my application works.
Information (please complete the following information):
Additional context