Flurl icon indicating copy to clipboard operation
Flurl copied to clipboard

4.6.1 class library unit test "MissingMethodException"

Open kmusick opened this issue 9 years ago • 24 comments

To recreate:

  1. Create a new .NetStandard library with a method using Flurl (EDIT: Not necessary, skip to step 2).
  2. Create a class library targeting 4.6.1
  3. Create project.json with the following
{
  "dependencies": {
    "FluentAssertions": "4.17.0",
    "Flurl": "2.2.1",
    "Flurl.Http": "1.1.1",
    "NSubstitute": "1.10.0",
    "xunit": "2.1.0"
  },
  "frameworks": {
    "net461": {}
  },
  "runtimes": {
    "win": {}
  }
}
  1. Reload project
  2. Try to run the following
[Fact]
public void Test()
{
    using (var x = new HttpTest())
    {
        "G".GetJsonAsync();
    }
}

Exception :

System.MissingMethodException Method not found: 'System.Threading.Tasks.Task`1<System.Object> Flurl.Http.HttpExtensions.GetJsonAsync(System.String, System.Threading.CancellationToken, System.Net.Http.HttpCompletionOption)'. at Tests.Class1.Test()

kmusick avatar Jan 20 '17 23:01 kmusick

This can be worked around by using a class library WITHOUT project.json as your Unit Test Project. If you are testing classes from a different project, this solution does not work (only tested with a netstandard library). However, you can add those classes as linked items in the Unit Test Project and test them that way. Not perfect, but it is functional

kmusick avatar Jan 25 '17 01:01 kmusick

I can repro, but I'm not sure off hand how to fix. I tried adding "System.Threading.Tasks" and a couple others to your dependencies and that didn't fix it, but I'm not sure that would be a proper fix anyway.

I'll call in the expert at this point. @kroniak any suggestions?

tmenier avatar Jan 25 '17 03:01 tmenier

@tmenier @kmusick In project.json library test can runs with "dotnet-test-xunit" lib.

I think you must use non project.json test library.

kroniak avatar Jan 25 '17 08:01 kroniak

@kroniak switching to dotnet-test-xunit did not work for me. This is where I found documentation on it and it doesn't seem like a valid solution -> this isn't a dotnet core project, it is a 4.6.1 class library. Here is some documentation ons witching to project.json for projects which seems like it should work. The section titled "NuGet v3 and Project.json to the rescue" is probably relevant to this issue

kmusick avatar Jan 25 '17 14:01 kmusick

@kmusick there are a runner which can runs this test. What runner do you use?

kroniak avatar Jan 25 '17 16:01 kroniak

@kroniak I normally use the Resharper test runner, but used Visual Studio runner for testing dotnet-test-xunit library. I tried multiple beta versions of xunit as well after failing with the suggested version from their site. Note, I am above their recommended dotnet --version (I am at 1.0.0-preview4-004233) so that may be causing an issue there. Can you post a working project.json?

kmusick avatar Jan 25 '17 16:01 kmusick

@kmusick i have no problem with xunit test runner and resharper runner.

Look https://gist.github.com/kroniak/0d7782249ad3ad2c7efedb5d353dc87e

kroniak avatar Jan 31 '17 08:01 kroniak

That looks like a NetCore project while I am attempting to run the tests in a 4.6.1 Framework Class Library Project. Using a NetCore project may be a valid workaround but I'm not sure if that means there is a problem with 4.6.1 and Flurl or something on my end.

On Tue, Jan 31, 2017, 2:12 AM Collin [email protected] wrote:

@kmusick https://github.com/kmusick i have no problem with xunit test runner and resharper runner.

Look https://gist.github.com/kroniak/0d7782249ad3ad2c7efedb5d353dc87e

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tmenier/Flurl/issues/166#issuecomment-276300343, or mute the thread https://github.com/notifications/unsubscribe-auth/AGH-KEUigi_Y9vmy7mAY8nlKcQkdrhggks5rXuzTgaJpZM4Lp3lm .

kmusick avatar Jan 31 '17 19:01 kmusick

Look to gist. I'm not using net core, only net 461. It works. But xunit required runner. How resharper runs tests in project.json project I don't know. If you want use runner only for net461 use non project json class library.

kroniak avatar Jan 31 '17 19:01 kroniak

Was it determined this is a test runner issue and not a Flurl issue? Can we close this?

tmenier avatar Feb 22 '17 03:02 tmenier

I have the same issue. I have migrated to .csproj.

 <ItemGroup>    
    <PackageReference Include="Flurl.Http" Version="1.1.2" />
    <PackageReference Include="Microsoft.NETCore.Platforms" Version="1.1.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
    <PackageReference Include="dotnet-test-xunit" Version="2.2.0-preview2-build1029" />
  </ItemGroup>

I have tried with and without dotnet-test-xunit and get the same exception: System.MissingMethodException : Method not found: 'System.Threading.Tasks.Task`1<System.Net.Http.HttpResponseMessage> Flurl.Http.HttpExtensions.PostAsync(Flurl.Http.IFlurlClient, System.Net.Http.HttpContent, System.Threading.CancellationToken, System.Net.Http.HttpCompletionOption)

My code is along the lines of (redacted for privacy):

[Fact]
public async Task MyTest()
{
    using (var httpTest = new HttpTest())
    {
        httpTest.RespondWith("myhtml");
        var result = await _myService.TestMethod();
     }
}
public async Task<string> TestMethod()
{          
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("foo", "bar")
    });

    using (var flurl = new FlurlClient("myurl"))
    {
        var response = await flurl.PostAsync(content);
        // Do work here  
        return "";              
     }
}

Replacing the FlurlClient in TestMethod with an HttpClient allows the test to run. Commenting out the HttpTest code in the test does not help, which suggests it is the FlurlClient that is failing and isn't affected by the presence of an HttpTest.

michaelmcneilnet avatar Mar 29 '17 18:03 michaelmcneilnet

@michaelmsm89 do you use vs 2017??

kroniak avatar Mar 29 '17 19:03 kroniak

@kroniak Yes, I do.

michaelmcneilnet avatar Mar 29 '17 19:03 michaelmcneilnet

@michaelmsm89 I dont have vs2017 eat. I will try it and submit feedback.

kroniak avatar Mar 31 '17 10:03 kroniak

@michaelmsm89 Sorry, I cant reproduce this issue. I have install vs 2017 and create xunit template project.

kroniak avatar Apr 05 '17 17:04 kroniak

I encountered the same problem when trying to build a powershell module that consumes a library which uses Flurl. My library targets netstandard1.3 and my powershell module targets net46. The module seems to load fine but as soon as I try to invoke a command that uses flurl I get a similar exception:

Method not found: 'System.Threading.Tasks.Task`1<System.Net.Http.HttpResponseMessage> Flurl.Http.GeneratedExtensions.PostUrlEncodedAsync(Flurl.Http.IFlurlRequest, System.Object,
System.Threading.CancellationToken, System.Net.Http.HttpCompletionOption)'.

I can however use the library perfectly fine from a net46 console application. I'm able to get it working in powershell if I force my library that consumes flurl to target net45.

nlowe avatar Nov 22 '17 19:11 nlowe

@nlowe I'm afraid that sounds a little out of my wheelhouse. I'm not even certain whether it's a problem with Flurl. Maybe post it on Stack Overflow and see if any PS gurus can point you/us in the right direction?

tmenier avatar Nov 23 '17 00:11 tmenier

@tmenier I'm having this same issue in a completely different scenario:

I have a Fake script that uses Noobot which depends on SlackConnector which depends on Flurl and Flurl.Http.

Could it somehow be related to System.Net.Http versions? I imagine that if an executable loads a different version of that assembly (not the one your library uses), then it is feasible that this exception will occur due to the CLR not matching types from different versions of the assembly.

dotfede avatar Nov 30 '17 19:11 dotfede

@tmenier Effectively the issue is related to the lack of Binding Redirects for System.Net.Http

I added the following to fake.exe.config and it worked:

<dependentAssembly>
  <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>

I believe that 5.2.3.0 is the version of the assembly used by .NET 4.6, whereas 4.5 had an older version, which might explain why @nlowe 's case worked correctly under NET45

dotfede avatar Nov 30 '17 20:11 dotfede

@tmenier I have same issue with net core 2.0, VS 2017, version 15.0.27004.2010

With this package references all work fine:

    <PackageReference Include="Flurl" Version="2.5.0" />
    <PackageReference Include="Flurl.Http" Version="2.0.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0-preview-20171211-02" />
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

after update flurl nugets to

    <PackageReference Include="Flurl" Version="2.5.2" />
    <PackageReference Include="Flurl.Http" Version="2.1.0" />

Tests throw exception: [xUnit.net 00:00:01.8470390] System.MissingMethodException : Method not found: 'Flurl.Url Flurl.Url.AppendPathSegment(System.Object)'.

rdcm avatar Dec 13 '17 11:12 rdcm