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

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

问题描述

我的问题与此一个,尽管它并不能真正解决我的问题。

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.

首先,设置您的库。右键单击库的项目名称,然后选择属性。在大约一半的位置,您会看到一个标签为包裹的标签。您可以在每次重建项目时使用它来自动生成NuGet软件包。只需增加版本号即可。我使用四个位置版本编号-前三个是 semver样式(主要版本,次要版本,补丁程序版本) ,第四个,我为每个新版本手动增加。

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标准和跨解决方案的传递依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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