简体   繁体   中英

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?

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:

  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:

在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM