Razorlight: Following the publish of the API controller project, the following error occurs.

Created on 28 Feb 2018  路  8Comments  路  Source: toddams/RazorLight

BiHu.AspNetCore.Demo.API.Controllers.ValuesController|ERROR| System.Private.CoreLib
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at BiHu.AspNetCore.Demo.API.Controllers.ValuesController.Get() in D:\CompanyProject\BiHu.AspNetCore.Demo\src\BiHu.AspNetCore.Demo.API\Controllers\ValuesController.cs:line 53 One or more errors occurred. (Cannot find compilation library location for package 'System.Data.SqlClient') System.InvalidOperationException: Cannot find compilation library location for package 'System.Data.SqlClient' at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List1 assemblies)
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(DependencyContext dependencyContext)
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.RoslynCompilationService.CompileAsync(IGeneratedRazorTemplate razorTemplate)
--- 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 RazorLight.Compilation.TemplateFactoryProvider.d__10.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 RazorLight.Compilation.TemplateFactoryProvider.d__8.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 RazorLight.RazorLightEngine.d__14.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 RazorLight.RazorLightEngine.d__11.MoveNext()

Most helpful comment

I have the same problem, but only when building on a linux (.net core 2.1)
in code

var _engine = new RazorLightEngineBuilder()
.UseFilesystemProject(Path.Combine(Directory.GetCurrentDirectory(), "Templates"))
.UseMemoryCachingProvider()
.Build();

var result = string.Empty;
var cacheResult = _engine.TemplateCache.RetrieveTemplate(viewPath);
if (cacheResult.Success)
{
    result = await _engine.RenderTemplateAsync(cacheResult.Template.TemplatePageFactory(), model);
}
else
{
    result = await _engine.CompileRenderAsync(viewPath, model);
}

exception

System.InvalidOperationException: Cannot find compilation library location for package 'Microsoft.NETCore.App'
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List`1 assemblies)
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(DependencyContext dependencyContext)
at RazorLight.Compilation.RoslynCompilationService.EnsureOptions()

All 8 comments

How are you referencing the System.Data.SqlClient library, if it is via a transient reference then I would make the reference explict. If that doesn't work try adding a MetadataReference for that library. see here for more info.

Very simple code.

`using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using RazorLight;
namespace ApiApplicationDemo.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet("{id}")]
public string Get(int id)
{
log.Info("RazorLightEngineBuilder");

        var engine = new RazorLightEngineBuilder()

.UseMemoryCachingProvider()
.Build();

        string template = "Hello, @Model.Name. Welcome to RazorLight repository";
        ViewModel model = new ViewModel() { Name = "John Doe" };

        string result = "";
        try
        {
            result = engine.CompileRenderAsync("templateKey", template, model).Result;
        }
        catch (Exception ex)
        {
            log.Info("RazorEngineException", ex);
            throw new Exception("Exception", ex);
        }

        return result;
    }
}

}
`
log:

2018-03-01 00:00:53,198 [7] INFO NETCorelog4net [(null)] - RazorEngineException
System.AggregateException: One or more errors occurred. (Cannot find compilation library location for package 'Microsoft.Win32.Registry') ---> System.InvalidOperationException: Cannot find compilation library location for package 'Microsoft.Win32.Registry'
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List1 assemblies) at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths() at System.Linq.Enumerable.SelectManySingleSelectorIterator2.MoveNext()
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(DependencyContext dependencyContext)
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.RoslynCompilationService.CompileAsync(IGeneratedRazorTemplate razorTemplate)
--- 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 RazorLight.Compilation.TemplateFactoryProvider.d__10.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 RazorLight.Compilation.TemplateFactoryProvider.d__8.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 RazorLight.RazorLightEngine.d__14.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 RazorLight.RazorLightEngine.d__11.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at ApiApplicationDemo.Controllers.ValuesController.Get(Int32 id) in C:\VProject\ApiApplicationDemo\ApiApplicationDemo\Controllers\ValuesController.cs:line 60 ---> (Inner Exception #0) System.InvalidOperationException: Cannot find compilation library location for package 'Microsoft.Win32.Registry' at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List1 assemblies)
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(DependencyContext dependencyContext)
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.RoslynCompilationService.CompileAsync(IGeneratedRazorTemplate razorTemplate)
--- 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 RazorLight.Compilation.TemplateFactoryProvider.d__10.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 RazorLight.Compilation.TemplateFactoryProvider.d__8.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 RazorLight.RazorLightEngine.d__14.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 RazorLight.RazorLightEngine.d__11.MoveNext()<---

Project file contents





The package referenced by the project.
<ItemGroup> <PackageReference Include="log4net" Version="2.0.8" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" /> <PackageReference Include="RazorLight" Version="2.0.0-beta1" /> </ItemGroup>

Hi @haifengwang, When I deployed code to server I've caught this error like this .
Have you have a solution for this ? Can you support to me.
@colinbull this is my error : _Cannot find compilation library location for package 'Microsoft.Win32.Registry'_
Can you support in this solution

var metadataReference = MetadataReference.CreateFromFile("path-to-your-assembly");

            var engine = new RazorLightEngineBuilder()
                                          .UseMemoryCachingProvider()
                                          .Build();

Note: About environment, that I've deployed code is CentOS

I have the same problem, but only when building on a linux (.net core 2.1)
in code

var _engine = new RazorLightEngineBuilder()
.UseFilesystemProject(Path.Combine(Directory.GetCurrentDirectory(), "Templates"))
.UseMemoryCachingProvider()
.Build();

var result = string.Empty;
var cacheResult = _engine.TemplateCache.RetrieveTemplate(viewPath);
if (cacheResult.Success)
{
    result = await _engine.RenderTemplateAsync(cacheResult.Template.TemplatePageFactory(), model);
}
else
{
    result = await _engine.CompileRenderAsync(viewPath, model);
}

exception

System.InvalidOperationException: Cannot find compilation library location for package 'Microsoft.NETCore.App'
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List`1 assemblies)
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(DependencyContext dependencyContext)
at RazorLight.Compilation.RoslynCompilationService.EnsureOptions()

@ragnarek81997 I had the same problem. But my app is a Azure WebApp normal. (running windows).

I'm using aspnet core 2.1. Locally works fine, but on Azure webapp i got this error

System.InvalidOperationException: Cannot find compilation library location for package 'Microsoft.NETCore.App'

@penihel I have exactly same error on Azure env. Have you discovered any solution?

I've resolved this error, this is my work.
In project file *.csproj, change to like this:

<PropertyGroup>
    ...
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
  </PropertyGroup>

and when deploy your project, copy all file/folder in bin folder

Was this page helpful?
0 / 5 - 0 ratings