在 .NET Framework 控制台应用程序中使用 .NET Standard 2.0 DLL 库时如何设置依赖项? [英] How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application?

查看:93
本文介绍了在 .NET Framework 控制台应用程序中使用 .NET Standard 2.0 DLL 库时如何设置依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道在这种情况下我应该如何设置依赖项(在何处添加 EntityFramework nuget 包):

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

  1. Core.Persistence 编译为 .NET Standard 2.0 DLL 库的项目.我有实体框架 6、EF、DbContext 等的数据库实体类.它应该只依赖于 EntityFrameworkCore.

  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.

Core.Domain 项目,它也编译为 .NET Standard 2.0 DLL 库.我想把我的业务对象 POCO 类放在这里.这应该没有依赖项.

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.

Core.Application 项目,这是 .NET Standard 2.0 DLL 库.我这里有所有应用程序逻辑.它依赖于 Core.Persistence 因为它进行数据库查询和 Core.Domain 因为它从查询结果中产生业务对象.

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.

Client.ConsoleClient 项目.它使 .NET Framework 4.7.2 可执行.它应该只依赖于 Core.Application但我这里有问题.

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.

Client.WindowsClient 项目,我不想关注这个问题.

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

所以,这就是我所做的:

So, this is what I have done:

问题是,当我尝试从 Core.Application 调用方法时,我收到 System.IO.FileLoadException.

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

它说它找不到System.Interactive.Async 文件(它是EntityFrameworkCore 的依赖项).在我将此文件添加为依赖项后 - 还有其他 System.IO.FileLoadException 错误.

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.

所以,我暂时将 EF6 Core NuGet 包添加到了我的 Client.ConsoleClient,并且 System.IO.FileLoadException 的问题消失了,但我觉得我是做错事了.

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.

此时我发现,Visual Studio 没有将 Core.xxx 项目输出中的 DLL 文件复制到 Client.ConsoleClient 项目输出中,这就是为什么我我收到错误.

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?

推荐答案

这是一个众所周知的老伤病,登录 GitHub 网址:
依赖项不会通过项目引用从新的 NET Standard 项目流到旧的桌面项目 链接

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

一个可能的解决方案是将 NuGet 依赖项添加到 Full NET Framework 项目中,就像您所做的那样.

A possible solution is to add the NuGet dependency to the Full NET Framework project, as you did.

将以下内容包含在 Full NET Framework 项目.csproj 项目文件中的另一个建议也适用于我.

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>

请注意,我在 NET Standard 项目中使用了包引用.

Note that I am using package references in the NET Standard projects.

就目前而言,NET Standard 项目最好作为 NuGet 包使用,因为这些将包含任何依赖引用作为 NuGet 打包到目标项目中.

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 引用 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 引用 Core.Persistence

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <ProjectReference Include="..Core.PersistenceCore.Persistence.csproj" />
    </ItemGroup>
</Project>

<小时>

ConsoleClient.csproj 引用 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.ApplicationCore.Application.csproj">
            <Project>{067b3201-3f65-4654-a0fb-e8fae521bf29}</Project>
            <Name>Core.Application</Name>
        </ProjectReference>
    </ItemGroup>
</Project>

这篇关于在 .NET Framework 控制台应用程序中使用 .NET Standard 2.0 DLL 库时如何设置依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆