Razorlight: Reference to Win32.Registry for 2.0?

Created on 16 Oct 2017  路  6Comments  路  Source: toddams/RazorLight

Hi,

upgrading to AspNetCore 2.0 - i tried using the current alpha3 which works fine on windows. (using the following code to initialize because no prerelease was made for RazorLight.Mvc):

private static void AddRazorLightSelf(this IServiceCollection services, string root)
        {
            if (services == null)
                throw new ArgumentNullException(nameof(services));

            if (string.IsNullOrEmpty(root))
                throw new ArgumentNullException(nameof(root));

            if (root.StartsWith(value: "/"))
            {
                root = root.Substring(1);
            }
            else if (root.StartsWith(value: "~/"))
            {
                root = root.Substring(startIndex: 2);
            }

            // resolve complete root path 
            var serviceProvider = services.BuildServiceProvider();
            var env = serviceProvider.GetService<IHostingEnvironment>();
            var absoluteRootPath = System.IO.Path.Combine(env.ContentRootPath, root);

            // register options
            services.AddSingleton<RazorLightOptions>();

            // register razor-light project
            services.AddSingleton<RazorLightProject, RazorLightProject>(p => new FileSystemRazorProject(absoluteRootPath));

            // register razorsourcegenerator
            services.AddSingleton<RazorSourceGenerator, RazorSourceGenerator>();

            // register metadatareferencemanager
            services.AddSingleton<IMetadataReferenceManager, DefaultMetadataReferenceManager>();

            // register compiler
            services.AddSingleton<RoslynCompilationService>();

            // register templatefactory
            services.AddSingleton<ITemplateFactoryProvider, TemplateFactoryProvider>();

            // register cachingprovider
            services.AddSingleton<ICachingProvider, DefaultCachingProvider>();

            // register engine
            services.AddSingleton<IRazorLightEngine, RazorLightEngine>(p =>
            {
                var engine = new RazorLightEngine(
                    p.GetRequiredService<RazorLightOptions>(),
                    p.GetRequiredService<ITemplateFactoryProvider>(),
                    p.GetRequiredService<ICachingProvider>());

                return engine;
            });
        }

Unfortunately - using RazorLight on Linux with this approach - fires an InvalidOperationException for not being able to find assembly "Microsoft.Win32.Registry", see the following stacktrace:

System.AggregateException: One or more errors occurred. (Cannot find compilation                                                                                                                                                              library location for package 'Microsoft.Win32.Registry') ---> System.InvalidOpe                                                                                                                                                             rationException: Cannot find compilation library location for package 'Microsoft                                                                                                                                                             .Win32.Registry'
   at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePa                                                                                                                                                             ths(ICompilationAssemblyResolver resolver, List`1 assemblies)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at Microsoft.AspNetCore.Mvc.Razor.Compilation.MetadataReferenceFeatureProvide                                                                                                                                                             r.PopulateFeature(IEnumerable`1 parts, MetadataReferenceFeature feature)
   at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateF                                                                                                                                                             eature[TFeature](TFeature feature)
   at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorReferenceManager.GetCo                                                                                                                                                             mpilationReferences()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boole                                                                                                                                                             an& initialized, Object& syncLock, Func`1 valueFactory)
   at Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
   at Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.Execu                                                                                                                                                             teCore(RazorCodeDocument codeDocument)
   at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDo                                                                                                                                                             cument document)
   at RazorLight.RazorSourceGenerator.<GenerateCodeAsync>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.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at RazorLight.RazorSourceGenerator.<GenerateCodeAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at RazorLight.TemplateFactoryProvider.<CreateFactoryAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at RazorLight.RazorLightEngine.<CompileTemplateAsync>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.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at RazorLight.RazorLightEngine.<CompileRenderAsync>d__9.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotifica                                                                                                                                                             tion)
   at FluiTec.AppFx.Mail.MailKitTemplatingMailService.<>c__DisplayClass4_0`1.<Se                                                                                                                                                             ndEmailAsync>b__0()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C                                                                                                                                                             ontextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at FluiTech.CallRouting.AspCoreHost.Controllers.HomeController.<Error>d__6.Mo                                                                                                                                                             veNext()
---> (Inner Exception #0) System.InvalidOperationException: Cannot find compilat                                                                                                                                                             ion library location for package 'Microsoft.Win32.Registry'
   at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePa                                                                                                                                                             ths(ICompilationAssemblyResolver resolver, List`1 assemblies)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at Microsoft.AspNetCore.Mvc.Razor.Compilation.MetadataReferenceFeatureProvide                                                                                                                                                             r.PopulateFeature(IEnumerable`1 parts, MetadataReferenceFeature feature)
   at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateF                                                                                                                                                             eature[TFeature](TFeature feature)
   at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorReferenceManager.GetCo                                                                                                                                                             mpilationReferences()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boole                                                                                                                                                             an& initialized, Object& syncLock, Func`1 valueFactory)
   at Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
   at Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.Execu                                                                                                                                                             teCore(RazorCodeDocument codeDocument)
   at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDo                                                                                                                                                             cument document)
   at RazorLight.RazorSourceGenerator.<GenerateCodeAsync>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.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at RazorLight.RazorSourceGenerator.<GenerateCodeAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at RazorLight.TemplateFactoryProvider.<CreateFactoryAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at RazorLight.RazorLightEngine.<CompileTemplateAsync>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.HandleNonSuccessAndDebuggerNot                                                                                                                                                             ification(Task task)
   at RazorLight.RazorLightEngine.<CompileRenderAsync>d__9.MoveNext()<---

Is there anything i can do to avoid this error?

Most helpful comment

@benbuckland I'm sorry - in case u hit the exactly same error - u're still using PreCompilation, that RazorLight currently doesnt support. You'll probably have to dig into deactivating that...
The settings in the csproj im normally using are:
<PreserveCompilationContext>true</PreserveCompilationContext> <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>

You can also try to add the following:
<ItemGroup> <MvcRazorFilesToCompile Include="**\*.cshtml" Exclude="MailViews\**\*.cshtml" /> </ItemGroup>

All 6 comments

Sorry - although i did not include the CLI-Reference for PreCompilation - it seems AspNetCore halfway enabled this feature. Disabling any PreCompilation using
false
resolved this issue.

@IInvocation, can you explain how did you manage to turn off PreCompilation?

Referring to the documentation (see https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?tabs=aspnetcore2x) - i'm not entirely sure what i did - but i'm pretty sure it was:

MvcRazorCompileOnPublish=false
If that wont work - you may also try:

PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="None"

Both in your csproj-File.

Also - be sure to delete all publish-output before.

@IInvocation I have just hit this issue, have tried both switches with no luck. Any further suggestions?

@benbuckland I'm sorry - in case u hit the exactly same error - u're still using PreCompilation, that RazorLight currently doesnt support. You'll probably have to dig into deactivating that...
The settings in the csproj im normally using are:
<PreserveCompilationContext>true</PreserveCompilationContext> <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>

You can also try to add the following:
<ItemGroup> <MvcRazorFilesToCompile Include="**\*.cshtml" Exclude="MailViews\**\*.cshtml" /> </ItemGroup>

Thanks for coming back to me @IInvocation the following did the trick for me!<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tresoldigiorgio picture tresoldigiorgio  路  4Comments

TheColonel2688 picture TheColonel2688  路  6Comments

btran021111 picture btran021111  路  4Comments

WaltDaniels picture WaltDaniels  路  6Comments

shobman picture shobman  路  4Comments