5

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?

LandonC
  • 889
  • 1
  • 16
  • 28

1 Answers1

3

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.

McGuireV10
  • 9,572
  • 5
  • 48
  • 64
  • Thanks for the suggestion, but do you know if it's applicable to the specific situation I outlined, that is .net Standard/Core cross-solution issues? – LandonC Mar 26 '18 at 01:46
  • Yes, this is how I solved the transitive dependency issue. My case is Azure and I'm not using this HTML Agility Pack, but I have several `netstandard20` libraries with various dependencies (sometimes several layers deep) referenced by .NET Core and ASP.NET Core applications. VS won't pick up the transitive dependencies but a NuGet package will. – McGuireV10 Mar 26 '18 at 10:19
  • This solves the problem, but doesn't actually answer the question asked. – Ian Kemp Aug 30 '19 at 11:32
  • @IanKemp because the direct "answer" is trivial and obvious -- that's just how VS packaging works. – McGuireV10 Sep 04 '19 at 09:01