RazorLight icon indicating copy to clipboard operation
RazorLight copied to clipboard

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

Open haifengwang opened this issue 7 years ago • 8 comments

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.<CompileAsync>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.<CreateFactoryAsync>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.<CompileTemplateAsync>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.<CompileRenderAsync>d__11.MoveNext()

haifengwang avatar Feb 28 '18 14:02 haifengwang

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.

colinbull avatar Feb 28 '18 14:02 colinbull

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.<CompileAsync>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.<CreateFactoryAsync>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.<CompileTemplateAsync>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.<CompileRenderAsync>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.<CompileAsync>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.<CreateFactoryAsync>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.<CompileTemplateAsync>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.<CompileRenderAsync>d__11.MoveNext()<---

Project file contents

haifengwang avatar Feb 28 '18 16:02 haifengwang

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>

haifengwang avatar Feb 28 '18 16:02 haifengwang

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

huanbd avatar Apr 04 '18 09:04 huanbd

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 avatar Jun 28 '18 14:06 ragnarek81997

@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 avatar Jul 05 '18 14:07 penihel

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

rybczak avatar Sep 02 '18 14:09 rybczak

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

huanbd avatar Sep 04 '18 01:09 huanbd