Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core, .NET Standard and Transitive Dependencies across Solutions

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

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. A solution with a single .NET Standard class library project. This class library has a reference to HTML Agility Pack.
  2. A solution with a single .NET Core 2.0 console application project.

Class library:

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;
        }
    }
}

Console application:

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);
        }
    }
}

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'

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?

like image 327
LandonC Avatar asked Oct 17 '22 20:10

LandonC


1 Answers

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

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.

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.

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

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.

like image 57
McGuireV10 Avatar answered Oct 23 '22 15:10

McGuireV10