.NET Core、.NET Standard 和跨解决方案的传递依赖项 [英] .NET Core, .NET Standard and Transitive Dependencies across Solutions

查看:32
本文介绍了.NET Core、.NET Standard 和跨解决方案的传递依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与此类似

My question is similar to this one, although it doesn't really address my issue.

我正在研究一些新的 AWS Lambda 函数,我希望将它们的实现保留在单独的类库中以供重用.我正在使用两种解决方案测试这个概念:

I am working on some new AWS Lambda functions, and I would like to keep their implementation in separate class libraries for reuse. I'm testing this concept using two solutions:

  1. 具有单个 .NET Standard 类库项目的解决方案.该类库引用了 HTML Agility Pack.
  2. 具有单个 .NET Core 2.0 控制台应用程序项目的解决方案.

类库:

using System;
using HtmlAgilityPack;

namespace ClassLibrary1
{
    public class Class1
    {
        public static bool FoundDotNet(string html)
        {
            bool foundDotNet = false;

            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(html);
            var titleNode = document.DocumentNode.SelectSingleNode("//title");
            if (titleNode != null)
            {
                string titleText = titleNode.InnerText;
                if (titleText.ToLower().Contains(".net"))
                {
                    foundDotNet = true;
                }
            }

            return foundDotNet;
        }
    }
}

控制台应用程序:

using System;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var foundDotNet = ClassLibrary1.Class1.FoundDotNet("<html><head><title>.NET WTF Buddy</title></head><body>You're doin' me a confuse.</body></html>");
            Console.WriteLine(foundDotNet);
        }
    }
}

两个项目都没有问题.但是,HTML Agility Pack 程序集未复制到任一项目的 Debug 目录中,并且当我尝试运行控制台应用程序时,我收到 Unhandled Exception: System.IO.FileNotFoundException: 无法加载文件或汇编'HtmlAgilityPack'

Both projects build without issue. However, the HTML Agility Pack assembly isn't copied into the Debug directory for either of the projects, and when I try to run the console application, I get Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'HtmlAgilityPack'

我将两个项目的包管理格式设置为PackageReference",我认为这可以正确处理传递依赖.HTML Agility Pack 列在 json.deps 文件中,所以我不确定是什么问题.

I have the package management format set to "PackageReference" for both projects, which I thought would handle the transitive dependency correctly. HTML Agility Pack is listed in the json.deps file, so I'm not sure what the problem is.

"HtmlAgilityPack/1.7.1": {
        "dependencies": {
          "System.Net.Http": "4.3.2",
          "System.Xml.XPath": "4.3.0",
          "System.Xml.XPath.XmlDocument": "4.3.0",
          "System.Xml.XmlDocument": "4.3.0"
        }

如果我将类库项目移动到与控制台应用程序相同的解决方案中,它就可以正常工作.是什么阻止我将代码分成单独的解决方案?

If I move the the class library project into the same solution as the console application, it works fine. What's preventing me from separating my code into separate solutions?

推荐答案

我在多个解决方案中使用了一个大型、复杂的库,并且该库具有许多可传递依赖项.

I'm using a large, complicated library in several solutions and the library has many transitive dependencies.

首先,设置您的库.右键单击库的项目名称并选择属性.大约一半时,您会看到一个标签为 Packages 的选项卡.每次重建项目时,您都可以使用它来自动生成 NuGet 包.只需增加版本号.我使用四个位置版本编号——前三个是 semver-style(主要版本、次要版本、补丁版本),第四个是我为每个新构建手动递增.

First, set up your library. Right click on the library's project name and choose Properties. About halfway down you'll see a tab labeled Packages. You can use that to auto-generate the NuGet package every time you rebuild the project. Just increment the version number. I use four position version numbering -- the first three are semver-style (major release, minor release, patch release), and the fourth one I increment manually for each new build.

我建议在您的驱动器或网络上专门为您的本地 NuGet 包创建一个文件夹.您可以为每个项目在该文件夹下创建文件夹.然后将调试和发布构建输出指向该项目文件夹,NuGet 包也会在那里生成.

I recommend creating a folder on your drive or network specifically for your local NuGet packages. You can create folders under that for each project. Then you point your debug and release build output to that project folder, and the NuGet package will be generated there, too.

最后,回到 Visual Studio,转到工具 -> 选项 -> NuGet 包管理器 -> 包源并将该顶级文件夹添加为包源.

Finally, back in Visual Studio, go to Tools -> Options -> NuGet Package Manager -> Package Sources and add that top-level folder as a package source.

从那里开始很简单 - 在您的消费应用程序中打开您的 NuGet 依赖项.右上角有一个下拉菜单,您可以在其中选择包来源.它会自动搜索所有子文件夹并找到您创建的任何包.现在,当您调整库时,只需单击一下即可更新客户端应用.

From there it's simple -- open your NuGet dependencies in your consuming app. There's a drop-down at the top right where you can choose the package source. It will automatically search all the child folders and find whatever packages you've created. Now when you tweak your library, it's just a single click to update the client apps.

这篇关于.NET Core、.NET Standard 和跨解决方案的传递依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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