Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application?

I can't figure out how should I set up dependencies (where to add EntityFramework nuget packages) in this scenario:

  1. Core.Persistence project which compiles to .NET Standard 2.0 DLL library. I have Entity Framework 6, database entity classes for EF, DbContext etc. It is supposed to depend just on EntityFrameworkCore.

  2. Core.Domain project which also compiles to .NET Standard 2.0 DLL library. I want to put my business object POCO classes here. This is supposed to have no dependencies.

  3. Core.Application project, this is .NET Standard 2.0 DLL library. I have all application logic here. It depends on Core.Persistence because it makes database queries and Core.Domain because it produces bussiness objects from query results.

  4. Client.ConsoleClient project. It makes .NET Framework 4.7.2 executable. It is supposed to depend only on Core.Application, but I have a problem here.

  5. Client.WindowsClient project which I don't want to focus in this question.

So, this is what I have done:

The problem is, that I'm getting System.IO.FileLoadException when I try to call method from Core.Application.

It says that it cannot find System.Interactive.Async file (which is dependency of EntityFrameworkCore). After I add this file as dependency - there are other System.IO.FileLoadException errors.

So, temporarily I have added EF6 Core NuGet package to my Client.ConsoleClient, and problems with System.IO.FileLoadException are gone, but I feel I'm doing something wrong.

At this moment I figured out, that Visual Studio is not copying DLL files from Core.xxx projects outputs into Client.ConsoleClient project output, and that's why I'm getting errors.


How to fix this properly?

like image 393
Kamil Avatar asked Jul 12 '19 11:07

Kamil


People also ask

Is .NET standard compatible with .NET Framework?

NET Standard 1.3 will be compatible with apps that target . NET Framework 4.6, . NET Core 1.0, Universal Windows Platform 10.0, and any other platform that supports .

How does .NET determine DLL dependencies?

Load your DLL into it, right click, and chose 'Analyze' - you'll then see a "Depends On" item which will show you all the other dll's (and methods inside those dll's) that it needs.

How do I use .NET standard library in .NET core?

Open Visual Studio 2015 Go to menu click File> New> Project then choose . net Core from left menu by selecting ASP.NET Core Web Application like below image. I have chosen an ASP.NET Core sample Template, like the below image. Let's create another project as a simple class library.

How do I add a DotNet dependency to a console application?

The following example uses the default console application project that's created by dotnet new console: To add a dependency, run the dotnet add package command, as shown in the following example:

Which version of the NET Standard should I be using?

NET Core 3.0 as well as upcoming versions of Xamarin, Mono, and Unity will be updated to implement .NET Standard 2.1. Library authors who need to support .NET Framework customers should stay on .NET Standard 2.0. In fact, most libraries should be able to stay on .NET Standard 2.0, as the API additions are largely for advanced scenarios.

How do I add dependencies to a library?

The primary way of adding dependencies to a .NET library is referencing NuGet packages. NuGet package references allow you to quickly reuse and leverage already written functionality, but they're a common source of friction for .NET developers.

How do I add a dependency to a class in NET Core?

For most common .NET Core applications, you don't need to do this. Add a dependency by editing the project file. To add a dependency, add a <PackageReference> element inside an <ItemGroup> element. You can add to an existing <ItemGroup> or create a new one.


2 Answers

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>
like image 101
pfx Avatar answered Oct 16 '22 16:10

pfx


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:

  1. Delete Properties\AssemblyInfo.cs from Client.ConsoleClient. We won't be needing this any more as the contents of that file now go into the project file.

  2. Delete packages.config - again, Nuget references will be stored in the project file - that's if you need any Nuget references after we reference Core.Application later.

  3. 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>
    
  4. Reload the project in Visual Studio.

  5. Add a reference to Core.Application and add any Nuget packages you need.

  6. 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:

enter image description here

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.

like image 42
Stephen Kennedy Avatar answered Oct 16 '22 17:10

Stephen Kennedy