简体   繁体   中英

Overloaded method not found

The setup is as follows:

  • a .NET Standard 2.0 library
  • a NUnit test project for it, targeting .NETFramework 4.6.1

Latest VS 2017, latest NUnit.

I've been working on the project on weekend from home, uploaded my work to git and today started working from work (I've already worked from both places before). Only now I found that something was wrong with the project (I don't remember very well what was wrong at the start, but it seems the problem was the same as I have now, described later).

After fiddling around to unrepairable state with it, I wholly deleted it and cloned the git repo anew.

The project compiles fine , but at runtime tests throw "Method not found" exception. A bit of poking around showed that the problem only manifests on one overload of the following method:

    public static YNABClient GetInstance(HttpMessageHandler _handler)
    {
        if (instance is null)
        {
            instance = new YNABClient(_handler);
        }

        return instance;
    }

    public static YNABClient GetInstance() => GetInstance(new HttpClientHandler());

The one without parameters is fine, the one with is not. Deleting and adding library as a reference to tests, deleting and adding both test and library project. Other solutions for similar situations I found on the internet all pertain to ASP.NET MVC, which is not my case, though this question did lead me to checking overloads and finding that one of them actually works.

At home everything still works fine, though I have yet to try to delete and reinstall the project as I did at work. This leads to 2 possible sources for problems: environment, though I haven't managed to find a meaningful difference, or git, though I use a "stock" git ignore for VS ( this one ), so there shouldn't be problems there. The basic setup for my projects didn't change during weekend and worked before, so something broke from recent fiddling.

Also, if I add a console application(.Net Framework 4.6.1) to solution and try calling the problematic method from it, it actually works fine.

If that would help, my github for the project is here

I've been asked for calling examples in the comments. Basically, I have 2 Test Fixture classes with different setups - one for real API calling for ease of debugging actual use, and one with faking it, as per good test practices. Works:

    [OneTimeSetUp]
    public void Setup()
    {
        ynabClient = YNABClient.GetInstance();
        ynabClient.RefreshAccessToken(ApiKeys.AccessToken);
    }

Throws exception:

    [OneTimeSetUp]
    public void Setup()
    {
        handler = new StubHandler();
        ynabClient = YNABClient.GetInstance(handler);
    }

Some poking around shows that my problem is quite likely related to System.Net.Http versioning discrepancy with .NET Framework and .NET Standard, that is quite a widespread problem if you google it. However none of the examples I dug up exhibit my particular symptoms, and I'm not yet sure what to do. And why everything works fine on my home PC.

The issue that you have is that your GetInstance method accepts HttpMessageHandler as parameter, and in your test you are passing HttpClientHandler object. So, you have declared one parameter, but provide different object when you call the method.

I got this error in my environment:

Severity Code Description Project File Line Suppression State Error CS0433 The type 'HttpMessageHandler' exists in both 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Your YNABConnector is .NET Standard 2 but Unit test is 4.6.1, they are using different signature for same assembly.

Here is official document to test: https://github.com/nunit/docs/wiki/.NET-Core-and-.NET-Standard

  1. created a .NET Core Library(be aware that: cannot be .NET Standard library by above link),
  2. copy your test code there,
  3. follow the official document to add references,

your code works then, no errors.

But in your base code, I still prefer this code:

public class YNABClient
{
    private static YNABClient instance;

    private HttpClient client;

    private HttpMessageHandler handler;

    private YNABClient(HttpMessageHandler _handler = null)
    {
        handler = _handler ?? new HttpClientHandler();
        client = new HttpClient(_handler);
    }

    public static YNABClient GetInstance(HttpMessageHandler _handler = null)
    {
        return instance ?? (instance = new YNABClient(_handler));
    }

    ......
}

So, it turns out, as DongDong suggested, the problem is indeed with interfacing between .NET Framework and .NET Standard. The problem is not really that they're incompatible, or that Test project needed some additional dependencies, but that they're shipped with different versions of System.Net.Http.

The diagnostics were hindered by showing no visible errors. However, changing parameter types showed that indeed, the problem is only with classes from that namespace.

Another problem with diagnosing the issue was that the project works fine on some machines (my home PC and Daisy Shipton's from comments to the question).

However after determining the source of the problem I was able to google what problems exist with the library in the first place, and eventually found a trove of uncompatibility issues on .NET github .

I tried the solution used in those cases and added a "binding redirect" to a concrete version of the library, and after that it works fine. I added the redirect to app.config of my Tests project. For reference it looks like this:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>

I still have no understanding of why the project worked fine on some machines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM