DotNetCorePlugins icon indicating copy to clipboard operation
DotNetCorePlugins copied to clipboard

enable runtime view compilation view error

Open lishuyiba opened this issue 5 years ago • 3 comments
trafficstars

When I started run-time view compilation, I reported an error. How do I fix it?

enable view compilation follow:

var mvcBuilder = services.AddRazorPages()
        .AddRazorRuntimeCompilation();

error message:

An unhandled exception occurred while processing the request.

InvalidOperationException: Cannot find compilation library location for package 'MvcAppPlugin1'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List<string> assemblies)
Stack Query Cookies Headers Routing
InvalidOperationException: Cannot find compilation library location for package 'MvcAppPlugin1'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List<string> assemblies)
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPartExtensions+<>c.<GetReferencePaths>b__0_0(CompilationLibrary library)
System.Linq.Enumerable+SelectManySingleSelectorIterator<TSource, TResult>.MoveNext()
System.Collections.Generic.List<T>.InsertRange(int index, IEnumerable<T> collection)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetReferencePaths()
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetCompilationReferences()
System.Threading.LazyInitializer.EnsureInitializedCore<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
System.Threading.LazyInitializer.EnsureInitialized<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.get_CompilationReferences()
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.LazyMetadataReferenceFeature.get_References()
Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.ExecuteCore(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document)
Microsoft.AspNetCore.Razor.Language.DefaultRazorProjectEngine.ProcessCore(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Process(RazorProjectItem projectItem)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(string relativePath)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(string normalizedPath)
Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultRazorPageFactoryProvider.CreateFactory(string relativePath)
Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet<IChangeToken> expirationTokens, string relativePath, bool isMainPage)
Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, string pageName, bool isMainPage)
Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, string viewName, bool isMainPage)
Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, string viewName, bool isMainPage)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Show raw exception details

lishuyiba avatar Jan 09 '20 15:01 lishuyiba

I'm not sure, but I'm guessing you will need to manually configure the runtime compilation options to point to your plugin on disk. If you figure it out, would you mind sharing how you fixed it?

.AddRazorRuntimeCompilation(compilationOptions =>
{
   compilationOptions.AdditionalReferencePaths.Add( " <insert additional path to .dlls here >");
});

The docs for this aren't very good, but I found this API by looking here; https://github.com/dotnet/aspnetcore/blob/v3.1.0/src/Mvc/Mvc.Razor.RuntimeCompilation/ref/Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.netcoreapp.cs#L26-L39

natemcmaster avatar Jan 10 '20 04:01 natemcmaster

was this ever solved, i am using Net5 framework and still getting this error.

aloksharma1 avatar May 24 '21 15:05 aloksharma1

@natemcmaster i found a different library which solved this issue for net 5/6 , even though his architecture is different you will be able to merge or create similar solution for this library solving razor bug when using runtime compilation.

the library is found here LazyMan.ModularLoader, the solution to razor bug is here https://github.com/John0King/LazyMan.ModularLoader/blob/master/test/SubAppTest/HostApp/Startup.cs

// razor bug fix
            AssemblyLoadContext.Default.Resolving += (alc, asbn) =>
            {
                return AssemblyLoadContext.All.OfType<PluginAssemblyLoadContext>()
                .Where(x => x.PluginInfo.PluginName.Equals(asbn.Name, StringComparison.OrdinalIgnoreCase))
                .FirstOrDefault()?.LoadFromAssemblyName(asbn);
                //if (AssemblyLoadContext.CurrentContextualReflectionContext == null)
                //{
                //    return null;
                //}
                //if (AssemblyLoadContext.CurrentContextualReflectionContext == alc)
                //{
                //    return null;
                //}
                //if (dic.TryGetValue(asbn.FullName, out var c))
                //{
                //    if (c > 0)
                //    {
                //        return null;
                //    }
                //}
                //if (dic.ContainsKey(asbn.FullName))
                //{
                //    dic[asbn.FullName] = dic[asbn.FullName] + 1;
                //}
                //else
                //{
                //    dic[asbn.FullName] = 0;
                //}
                //return AssemblyLoadContext.CurrentContextualReflectionContext.LoadFromAssemblyName(asbn);
            };

aloksharma1 avatar Feb 08 '22 19:02 aloksharma1

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

github-actions[bot] avatar Feb 09 '23 02:02 github-actions[bot]

Closing due to inactivity. If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.

github-actions[bot] avatar Feb 24 '23 02:02 github-actions[bot]