Master_T
Master_T

Reputation: 7923

Why do I sometimes need to to add Nuget dependencies even if they're not used directly by my project?

Sorry for the vague title, but I didn't really know how to phrase it concisely.

Here's my problem: I have a .NET Framework C# solution that has 2 projects inside:

In Project A, I have a method like this:

public class FooClass {
    public static void Foo(){
        //does something with a class from referenced Nuget package "NP"
        NP.BarClass.Bar();
        return;
    }
}

In Project B, I call the above mentioned method, like this:

public static void main(){
    FooClass.Foo();
}

however, this gives the following compiler error:

The type 'BarClass' is defined in an assembly that is not referenced. You must add a reference to assembly 'NP, Version=X.X.X.X, Culture=neutral, PublicKeyToken=xxxxxxxxxxx'

The solution, as suggested by the compiler, is to also add a reference to Nuget package NP in Project B. When I do that, everything works as expected.

My questions are:

EDIT: I've managed to reproduce the problem in a self-contained example, see here

Upvotes: 2

Views: 1525

Answers (2)

Master_T
Master_T

Reputation: 7923

EDIT: I've reproduced the problem here

Well, after further tiral-and-error, I finally found the source of the problem, and let me tell your: it's pretty weird, at least in my opinion.

So, the reason why this only seemed to happen in certain cases is because there seems to be a further requirement for this to happen. In particular, you have to have an overloaded method with the same number of parameters that either returns or accepts as parameter a type from the nuget package in question.

In my case, the FooClass in ProjectA was something like this:

public class FooClass
{
    public static void Foo(string x)
    {
        //Do something
        return;
    }

    public static void Foo(ClassFromNugetPackage np)
    {
        //Do something
        return;
    }
}

Now, in ProjectB I was only calling the first method, the one with the string parameter, but that was giving me the error, even if the project didn't directly reference any class from the nuget package.

However, if I rename the second method, so it is no longer an overload, like this:

public class FooClass
{
    public static void Foo(string x)
    {
        //Do something
        return;
    }

    public static void Bar(ClassFromNugetPackage np) //Renamed method
    {
        //Do something
        return;
    }
}

the problem disappeared!

If anyone can tell me the logic behind this, I would be happy. I mean, method overloading is resolved at compile time, right? The compiler should know I'm calling the version of the method that doesn't return/accept stuff from the nuget package... why the error then?

Upvotes: 1

Mr Qian
Mr Qian

Reputation: 23780

Why does this happen only with certain Nuget packages? I've been working with C# for a while now, and this doesn't happen with all packages, only some of them.

Since your issue is complex and we cannot test the whole in our side, maybe you can follow these:

Actually, it does have a possibility of conflict between your VS Framework version or package reference format and the specific nuget packages.

Some specific nuget packages are installed in the class library project, but when other projects refer to the class library, the content of some specific packages will conflict with the old framework version of the VS, so you still need to refer to the main The project can only be identified by adding this dependency. And this is just a problem with some special packages and framework version.

And another is just like zivkan said, you should keep package management format consistent(Tools-->Options-->Nuget Package Manager-->General-->Package Management) to prevent conflicts due to different management formats. You should make sure that project A's output files including NP are in the Porject B's output files.

Why does this happen at all? Since my project (Project B) is not directly referencing any types or methods from package NP, why do I have to include it explicitly? Isn't it enough that it's included in Project A, the one that uses it?

I assume you used an old VS version(<=VS2017) to create such projects.

In general, you really don't need to include these in the main project(Project B).

In fact, In VS2017 or earlier, in the form of Project Reference, there is indeed an issue that the dependent nuget and its files in the class library project will be lost in the project which references it. Fortunately, this problem has been fixed in vs2019.

So if your situation is what I said, I suggest you could use the latest VS2019.

Or in older VS version, use your solution--> add the nuget in Project B.

In my side with VS2019, I did not face the same error.

Upvotes: 1

Related Questions