Reputation: 934
TLDR; Razor view precompilation on the full framework (net471) only seems to work if the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation nuget package is added directly to the project and not within another package. Does anyone know a way around this?
I have a .net core 2.0 MVC project running on the full framework (net471). According to the docs, in order to perform view precompilation on a full framework app, you need to include the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation nuget package. This seems to work ONLY if you have added the package directly to the MVC project. If you have this package within another nuget package, the AppName.PrecompiledViews.dll never gets published.
I spun up a new .net core MVC project in visual studio running on net471 with the following packages. All works great, the dll shows up and performance indicates the views are compiled correctly:
Packages Used for Reference:
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.2" />
I next created a class library, added the same packages and bundled it in a nuget package (let's call it CommonLib). I created a second .net core 2.0 MVC project running on net471 but instead included the CommonLib nuget package. Everything runs normally, except on publish there is no AppName.PrecompiledViews.dll:
If I then add just the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation package to directly to the project with the CommonLib package, the PrecompiledViews.dll shows up:
I am trying to create a common class library project that contains dependencies for my projects (including .net core components) to simplify upgrades across all common micro projects. Has anyone had luck with doing this?
Upvotes: 0
Views: 322
Reputation: 387507
In order for Razor view compilation to run, you need to include special MSBuild targets that will automatically run as part of your project’s build process. These tasks are included in the Microsoft.AspNetCore.Razor.ViewCompilation
package.
Currently, the only way to expose additional MSBuild targets with NuGet is by explicitly referencing the package. There is no transitive property with build targets, so you will always have to explicitly reference the package in order to activate those targets for your project.
I just found this issue on GitHub which suggests that build targets already flow transitively to the top project, but I actually haven’t found a way to trigger this behavior.
Upvotes: 1