ASP.NET Core – InvalidOperationException: Cannot find compilation library location for package ‘Microsoft.Win32.Registry’

TL;DR

Add following line to your .csproj file:

<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>

Introduction

I was publishing a project I am working on to web server (Linux) and I had an issue with Razor Pages.

However, it seems that this error occurs only when you precompile your Razor views (MVC views, Razor Pages). Why would you do that? Well, precompiling your views results in a smaller published bundle and faster startup time.

Precompile your views if you can!

For you to be able to do that you need to have enabled MvcRazorCompileOnPublish. You will do this inside of your .csproj file, by adding the following line:

<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>

If you are not running ASP.NET Core 2 and using All meta package then you need to install separate NuGet package for that: Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.

 

My concrete problem

My application was complaining about following:

InvalidOperationException: Cannot find compilation library location for package ‘Microsoft.Win32.Registry’ 

This happens when you have enabled precompilation of your views AND you have a class library that has embedded view or you are using a plugin that does the same thing (embedding views). In this case, you need to add an additional setting to your .csproj file:

<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>

This will continue publishing referenced DLLs inside of your publish directory.

Ibrahim Šuta

Software Engineer, Mentor and Consultant interested and specialising in ASP.NET Core, C#, JavaScript, Angular, React.js. Experienced in MeteorJS, Node.js, ASP.NET, Windows Forms and Web Forms as well.