Can build and run on Visual Studio but cannot Publish - asp.net-core

I am having a weird situation where I cannot publish my final Web project. Here is the project structure:
Foo.Services.Common: this is shared library: .NET Standard 2.0.3, Microsoft.AspNetCore.Mvc.Core 2.1.0 with the class DefaultWebSettings defined.
Foo.Services.Common.Razor: Razor Class Library, has reference back to Foo.Services.Common and uses DefaultWebSettings: NETStandard.Library 2.0.3, Microsoft.AspNetCore.Mvc 2.1.0 and some other ASP.NET Core related package (static files, Configuration)
Foo.Services.Api: ASP.NET Core API only (no View, no Razor), has reference back to Foo.Services.Common. Publish well. Uses: Microsoft.AspNetCore.App 2.1.0, Microsoft.NETCore.App 2.1.0.
Foo.Services.Web: front end project. This one is the one I cannot publish! This one has references to both Common projects. Uses: Microsoft.AspNetCore.App 2.1.0, Microsoft.NETCore.App 2.1.0
However, upon publish, this one shows up:
The type or namespace name 'DefaultWebSettings' could not be found
(are you missing a using directive or an assembly reference?)
The error location is in a file of Foo.Services.Common.Razor project, NOT the Web project.
Funnily, Visual Studio perfectly works even with IntelliSense and Go to Definition:
I can build in both Debug/Release mode, run with/without debug any project in the solution well. Moreover, publishing Foo.Services.Common.Razor works without any problem!
In short: I cannot publish my Web project and have no idea what is the problem. I have tried:
Clean/Build.
Delete/Re-add references.
Manually delete all obj and bin folders.
Please show me what I may missing.

In the end, I had to remove Reference of the Common from Common.Razor and Web projects. I guess they could not build because of some conflict between .NET Standard and .NET Core. Had to copy all code of Common into Common.Razor.

Related

Resolution of Microsoft.Net.Http.Headers assembly version 2.2.0 versus 6.0.0 depends on seemingly unrelated package references

While trying to use HeaderNames.XFrameOptions, I found that it works in one of my projects and fails in another. Both are targeting .NET 6.
In my app, I need to work with the X-Frame-Options HTTP header. When possible, for maintainability reasons, I try to evade specifying constants for such standard strings myself, relying on standard constants instead. For this header's name, there is HeaderNames.XFrameOptions, available since ASP.NET Core 3.0.
By navigating to the class's implementation in VS, I found that the project where it works uses version 6.0.0.0 (.NET 6, x64) of the Microsoft.Net.Http.Headers assembly:
C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\6.0.12\Microsoft.Net.Http.Headers.dll
The project where it does not work uses version 2.2.0.0 (.NET Standard 2.0, MSIL) of the assembly:
C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.net.http.headers\2.2.0\lib\netstandard2.0\Microsoft.Net.Http.Headers.dll
The working project is the main project of my console app and the broken project is a library in the same solution, directly referenced by the console app.
When I looked into the broken project's obj\project.assets.json, I found that Microsoft.Net.Http.Headers is really used in version 2.2.0 and it is referenced by Microsoft.AspNetCore.Http.Extensions/2.2.0, which is also used in the broken project.
I used that info to reproduce the failure:
dotnet new globaljson --sdk-version 6.0.404 --roll-forward disable
dotnet new console -f net6.0
dotnet add package Microsoft.AspNetCore.Http.Extensions --version 2.2.0
echo "Console.WriteLine(Microsoft.Net.Http.Headers.HeaderNames.XFrameOptions);" > Program.cs
dotnet run
C:\Repos\playground\Program.cs(1,58): error CS0117: 'HeaderNames' does not contain a definition for 'XFrameOptions' [C:\Repos\playground\playground.csproj]
The build failed. Fix the build errors and run again.
I tried adding all the packages referenced by the working project to the repro project and it fixed it! By elimination and following the dependencies of the last remaining project, I found that the fix can be achieved by adding just a single package (AspNetCore.HealthChecks.UI.Client/6.0.5):
dotnet add package AspNetCore.HealthChecks.UI.Client --version 6.0.5
dotnet run
X-Frame-Options
I see that AspNetCore.HealthChecks.UI.Client references the Microsoft.Net.Http.Headers assembly version 6.0.0.0, but directly, as an assembly, not as a NuGet package. Sadly, the latest packaged version is 2.2.0.0.
How can I make HeaderNames.XFrameOptions work in both my projects at the same time? Is installing an irrelevant package like AspNetCore.HealthChecks.UI.Client the only solution? And is there a more straightforward way to diagnose such assembly resolution issues?

ASP.NET Core 2.2 - How to retreive NuGet Package Dependencies from Assembly that a Project References

I have a project ("Project1") that has a reference to an Assembly that I created in another project ("Project2").
I just added a function to a class in that assembly, but that function required me to add the NuGet package System.Drawing.Common. Which isn't an issue.
Here's the issue:
When I build Project1, it does not bring System.Drawing.Common.DLL to the bin folder. So, at runtime, when I execute this new function, I get an error stating that it can't find this DLL.
Is there a way that I can have Project1 bring along all of the Assembly's dependencies? Or do I have to instead add all the same NuGet packages that Project2/Assembly uses to Project1?
I'd prefer the former. I'm using ASP.NET Core 2.2 and Visual Studio 2017.
Thank you.
The build command with .NET Core 2.2 does not bring in the dependencies by default this is for testing only. If you are wanting all the dependency .dll to be included for deployment you should use the publish command to get all the .dll from your NuGet packages. There are other switches to include the framework as well so you don't need to install the framework, on the host.
As a friendly reminder, Dot Net Core 2.2 support will end on December 23, 2019

Version conflict occur in Microsoft.Entity.FrameworkCore

I am working on a .net core web application with razor pages.
My application structure is like :
DemoCoreApplication with class Libraries "BLL.Data" and "BLL".
In BLL.Data, I have made the DB context then I have added the reference of "BLL. Data" project into my main project i.e: DemoCoreApplication.
Refrences in BLL.Data class library:
After building the main project (DemoCoreApplication), I got some strange version conflicting errors.
I believe the following steps will solve your problem
Install .Net Core 2.2 SDK (if you don't have)
Convert all projects in the solution to use .Net Core 2.2
Rebuild, Start the project

Netcore 2.0 publish is not copying SDK dlls

I am trying to publish a Netcore 2.0 application with MSBUILD.
The published output is missing all the SDK dlls such as Microsoft.AspNetCore.Hosting.dll (there are a lot) so when I try to run the application from command line with dotnet appname.dll I get the error saying that it can't find the referenced dll...
I have published other applications before (not created by me) and the publish copies the dlls and also has a "refs" folder which mine does not have.
So I am pretty sure it's a project configuration issue but I have been searching for hours and there is no information on what I should change.
Turns out there was a nuget reference on my project called Microsoft.AspNetCore.All...
For some reason uninstalling this reference solved the issue and now the publish output contains all the SDK dlls
The ASP.NET Core dlls are part of the runtime store that is included in the runtime & hosting bundle installations so they don't need to be included in the publish output. (note that this is going to change in the 2.1 timeframe)
To disable the use of the ASP.NET Core runtime package store, you can set this in your project file:
<PropertyGroup>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>

Reference third-party class libraries

I am working with .Net Core 1.0 (running under the .Net Framework 4.6.1, non-portable).
I need to include some DLLs that are from a locally-built GitHub project. When I build those projects, and then attempt to "Add Reference" to the resulting DLLs, I get a message saying I can't add them to a Core project directly.
After more research, I found a lot of information regarding "private" NuGet packages. However, those seem overly complex / overly engineered.
Is there any way I can do the following:
Without having to go through the headache of creating a private NuGet repository, can I just "add reference" to the built assemblies that are sitting in the bin folder of the NuGet projects I pulled?
I really don't want to have to build a local-only NuGet package. Mostly because I've already wasted too much time on this issue, and because I read this entire concept is about to be scrapped and turned into something else (sounds familiar by now)... such as the Roslyn-based build system on GitHub.
My current state:
Visual Studio Professional 2015
.Net Core 1.0.1
.Net Core 1.0.1 Tooling Preview 2
No, as for now you have to create a nuget package before and restore it via Nuget. You can use a simple folder as NuGet source, so if you put your compiled NuGet package in C:\packages, you can add this as a source to NuGet (while in the NuGet UI, click the settings Icon and add the folder as new source).
This may change with the next release of ASP.NET Core (1.1), as the .NET/ASP.NET Core team is working to move from *.xproj to *.csproj files.
One of the reasons why you need to use nuget is because it can contain multiple targets and project.json allows you to target multiple platforms (i.e. net452 and netcoreapp1.0).