MissingMethodException when running a unit test th

2019-06-17 17:05发布

问题:

I have a NUnit unit test that is written in a normal F# library but targets F# code in a Portable Class Library.

When I run this test (in Visual Studio 2013), I get the following exception:

Result Message: System.MissingMethodException : Method not found:
 'Microsoft.FSharp.Control.FSharpAsync`1<System.IO.TextReader> FSharp.Data.Runtime.IO.asyncReadTextAtRuntime(System.Boolean, System.String, System.String, System.String, System.String)'.

This is what I have in my app.config in the Portable Class Library:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.3.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

This is what I have in the app.config of my normal F# library:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.1.0" newVersion="4.3.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

回答1:

The MissingMethodException means exactly that (in terms of signature).

It sounds like your test code doesn't have a reference to the version of FSharp.Data DLL that your portable library is using.

The method signature for asyncReadTextAtRuntime was changed very recently, so you must reference the latest version in your test project.

See this GitHub commit, where the function was altered to take an additional parameter called formatName:

https://github.com/fsharp/FSharp.Data/commit/be3651f314b7a13b57a755a728287373adda775d#diff-a47e4306ce1338946e18435ee1e97c50R304



回答2:

Apparently, FSharp.Data does not support PCL libraries using profile 7. After changing my PCL project profile to 47 everything works as expected.



回答3:

I had this same issue, and it was nothing to do with PCLs that I was aware. Adding an explicit binding redirect in the (C#) testing project for FSharp.Core made it go away (actually I had the same issue in Linqpad also)

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-999.999.999.999" newVersion="4.4.0.0" />
  </dependentAssembly>
</assemblyBinding>

(The testing project in C# has no direct FSharp references itself, other than what it inherits from the F# projects it's testing)



回答4:

I updated my version of the DLL to an earlier version.

In my case, I was attempting to use Type Providers in the FSharp.Data DLL.

I updated FSharp.Data to an earlier version and the error went away.