Razor view compilation does not publish AppName.PrecompiledViews.dll - asp.net-core

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?

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.

Related

ASP.NET Core Controllers Fail to Register with Authorization Package

My asp.net core 3.1 project throws an exception if I include this package (context: I’m trying to create a custom Authorization policy):
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
This is the exception and method where it is thrown in startup.cs:
services.AddControllers();
System.ArgumentNullException: 'Value cannot be null. (Parameter 'configure')'
This is the complete list of packages. Is there some conflict that I'm not aware of? The package was suggested by VS when I created an AuthorizationHandler class.
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.0.1" />
<PackageReference Include="Azure.Identity" Version="1.2.3" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.1.0" />
<PackageReference Include="CorrelationId" Version="3.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.9" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.2" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.2.0" />
<PackageReference Include="NSwag.AspNetCore" Version="13.8.2" />
Oops, looks like the answer was already available with a little more effort on my part. I resolved this by manually downgrading the package version to 3.1.12 as described here. Looks like VS gave me the .net version of the package instead of the .net core version.
I was using an authorization handler form a .NET Standard 2 library where v.6.0.x was installed. Downgrading was not an option for me.
Exchanging services.AddControllers() with services.AddMvcCore() in my Core 3.1 project solved the problem for me.

Why do I need to install the EntityFrameworkCore nuget package when moving my project to a new machine?

Maybe I didn't install the package correctly to begin with, please let me know, but after I moved my project to a new machine, installed .Net Core SDK 3.1 (latest) and cloned my project from Github, I had to go and install this nuget package manually:
https://www.nuget.org/packages/Microsoft.EntityFrameworkCore
When I first opened up the project in VS Code I had like 200+ errors for missing files.
I installed the EntityFrameworkCore package in my project folder and that fixed it.
QUESTION - I didn't have to install any of the other packages below (ex. SendGrid), so why this one?
Did I not install it correctly initially on my other computer? So maybe it didn't check in the reference or something? But the reference is in the .csproj file below!
Here are all the other packages I'm using and none had to be manually installed except
Microsoft.EntityFrameworkCore 3.1.3
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="3.1.3" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.6.0" />
<!-- <PackageReference Include="NetTopologySuite" Version="2.0.0" /> -->
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="CloudinaryDotNet" Version="1.8.0" />
<PackageReference Include="SendGrid" Version="9.12.0" />

Upgrade .net core 2.2.0 to 2.2.6 or above

Looking for steps to upgrade .Net core 2.2.0 to 2.2.6 or higher (not 3.0)
I am new to .net core. So looking for guidance on how to upgrade .net core 2.2 to 2.2.6 as app is failing while pushing to Pivotal cloud foundry. Our PCF environment dotnetcore buildpack version is 2.2.12; Used to work before using 2.2.5 build pack. Please find error message below. To resolve this error , though of upgrading .net core app to versions higher than 2.2.5. But would like to seek help on steps to follow for upgrading the app & respective dependencies.
Here is targetframework from .csproj
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
And dependencies are
<ItemGroup>
<PackageReference Include="AutoMapper" Version="8.1.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="6.1.0" />
<PackageReference Include="CsvHelper" Version="12.1.2" />
<PackageReference Include="EFCore.BulkExtensions" Version="2.4.6" />
<PackageReference Include="FluentValidation.AspNetCore" Version="8.3.0" />
<PackageReference Include="Manulife.Logging.DotNet" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="Sendgrid" Version="9.11.0" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Steeltoe.CloudFoundry.Connector.EFCore" Version="2.2.0" />
<PackageReference Include="Steeltoe.Extensions.Configuration.CloudFoundryCore" Version="2.2.0" />
<PackageReference Include="Steeltoe.Extensions.Logging.DynamicLogger" Version="2.2.0" />
<PackageReference Include="Steeltoe.Security.Authentication.CloudFoundryCore" Version="2.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>
Error while pushing app to Pivotal (during dotnet publish)
depends on runtime.linux-x64.Microsoft.NETCore.App (>= 2.2.5) but run
time is not found. Picked up 2.2.6 which is available
/tmp/app/Basischangeapp/Basischange.csproj : error NU1605: Detected
package downgrade: Microsoft.NETCore.DotNetAppHost from 2.2.6 to 2.2.5.
Reference the package directly from the project to select a different
version.
It will happen because of version mismatch between local SDK and the build server.
Add this line to your .csproj file:
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
After that, RuntimeFrameworkVersion will use LatestNetCorePatchVersion.

Creating a NuGet Metapackage as a MSBuild Project

Is there any real way to create a NuGet metapackage as an msbuild .proj instead of directly using a nuspec? I want to do this so I can leverage other MSBuild functionality including PackageVersioning, including content files, etc.
What I mean is that I want to have a simple project for my metapackage (let's say MyMetaPackage.proj:
<Project>
<Import Project="$(MSBuildSDKsPath)\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets" />
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta5" PrivateAssets="None" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0-beta5" PrivateAssets="None" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.0-beta5" PrivateAssets="None" />
<PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="3.0.0-beta5" PrivateAssets="None" />
<PackageReference Include="MyOtherPackage" Version="4.5.0" />
<Content PackagePath="readme/$(PackageVersion).txt" Include="readme.txt" />
</ItemGroup>
</Project>
The problem with this is that MyMetaPackage.nupkg ends up having all contentFiles and customs props/targets imported into it that screw it up, even though it's only meant to be a metapackage - those things should only be done for the CONSUMERS of MyMetaPackage.
Adding PrivateAssets to MyMetaPackage.proj isn't at all helpful, because then the nuspec excludes them as well and they won't be passed on to the consumers either.

Class library lost intellisense in VS2017 after .NET Core MVC auto-migration

After migrating a VS2015 MVC Core application with two projects (web app and class library) to VS2017 I've lost intellisense on all views within the class library. Pretty much everything in every single view is broken, so I'm sure it's something fairly basic that the migration tool didn't take care of for me. Even the #model directive in each razor view is an error.
The csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<AssemblyName>Library</AssemblyName>
<PackageId>Library</PackageId>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Views\**" Exclude="bin\**;obj\**;**\*.xproj;packages\**;#(EmbeddedResource)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="1.1.0" />
</ItemGroup>
</Project>
I tried duplicating the web.config from the web application to the root of the class library but that didn't help.
Looks like you've hit this bug:
https://github.com/aspnet/Mvc/issues/5975
Old Answer below
With the RTM version of VS 2017, you need to install the Razor Language Service extension to get back your Razor intellisense.
Unfortunately this component missed an internal ship date and as such did not make it as part of the actual VS 2017 release.
Uninstall the Razor Language Services by going to "Tools" > "Extensions and Updates".
Restart Visual Studio as per indication...
Reinstall Razor Language Services.
You will see razor intellisense working again such as asp-* tag helpers...
Two things were required to fix this issue, a change to the project SDK (<Project Sdk="Microsoft.NET.Sdk.Web">) and adding an element to set the output type to a library (<OutputType>Library</OutputType>). Full information on this GitHub issue I cross-posted.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<OutputType>Library</OutputType>
<AssemblyName>Library</AssemblyName>
<PackageId>Library</PackageId>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Views\**" Exclude="bin\**;obj\**;**\*.xproj;packages\**;#(EmbeddedResource)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="1.1.0" />
</ItemGroup>
</Project>