How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application?
This is a wellknown and quite old hurt logged on GitHub at:
dependencies don't flow from new NET Standard
project to old desktop projects through project references link
A possible solution is to add the NuGet
dependency to the Full NET Framework
project, as you did.
The other suggestion to include the following to the .csproj
project file of the Full NET Framework project
is also working for me.
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
Note that I am using package references in the NET Standard
projects.
As for now, it looks like NET Standard
projects are best to be consumed as NuGet
packages, as these would include any dependent references as NuGet
packages into the target project.
Core.Persistence.csproj referencing Entity Framework
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
</ItemGroup>
</Project>
Core.Application.csproj referencing Core.Persistence
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core.Persistence\Core.Persistence.csproj" />
</ItemGroup>
</Project>
ConsoleClient.csproj referencing Core.Application
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<!-- ... -->
</PropertyGroup>
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<!-- ... --->
<ItemGroup>
<ProjectReference Include="..\Core.Application\Core.Application.csproj">
<Project>{067b3201-3f65-4654-a0fb-e8fae521bf29}</Project>
<Name>Core.Application</Name>
</ProjectReference>
</ItemGroup>
</Project>
The new SDK format csproj
files don't play terribly well with the legacy format project files.
But fear not as .NET Framework console apps can use the SDK format!
Make sure you have your work committed to source control, or make a copy of the folder, and then do the following:
Delete
Properties\AssemblyInfo.cs
fromClient.ConsoleClient
. We won't be needing this any more as the contents of that file now go into the project file.Delete
packages.config
- again, Nuget references will be stored in the project file - that's if you need any Nuget references after we referenceCore.Application
later.Open
Client.ConsoleClient.csproj
in a text editor and change the contents to:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net472</TargetFramework> </PropertyGroup> </Project>
Reload the project in Visual Studio.
Add a reference to
Core.Application
and add any Nuget packages you need.If you had any content in
Properties\AssemblyInfo.cs
other than versions at 1.0.0.0, right click the project in Solution Explorer and click Package. Add the details you need and then save:
That's it, although there are 2 others things you might need to do depending on your circumstances:
If there are files which should be excluded, you'll need to exclude them, as the new project format includes all relevant file types by default.
You might have to set the language version. In my Visual Studio 2019 Preview,
latest
(latest minor version of C#) is the default so I don't need to do this.