-->

Why do I get a missing method exception runtime wh

2019-06-24 08:25发布

问题:

I have the following code:

open FSharp.Data

[<Literal>]
let connectionString = @"Data Source=(local)\SQLExpress;Integrated Security=SSPI;Database=SfagStage"

type InsertEnhet = SqlCommandProvider<"Sql\InsertEnhet.sql", connectionString>

let insertEnhet (enhet:Enhet) = 
    let cmd = new InsertEnhet() // <<--- This generates an error runtime
    cmd.Execute(enhet.Navn, enhet.Enhetsnummer, enhet.LEIKode, enhet.Kommune, DateTime.Now)

The row where I create the command is what causing the missing method I think. The part that of the exception that I think matters is:

System.MissingMethodException: Method not found: 'Void FSharp.Data.ISqlCommand Implementation..ctor(FSharp.Data.Connection, Int32, System.String, Boolean, System.Data.SqlClient.SqlParameter[], FSharp.Data.ResultType, FSharp.Data.ResultRank, Microsoft.FSharp.Core.FSharpFunc`2, System.String)'.

回答1:

This is the kind of exception that you may get when you don't have correct bindingRedirect for FSharp.Core.dll. Check out this article by Mark Seemann. In principle, I think that adding app.config with bindingRedirect to your application should solve the problem.

This typically happens when using a library that is compiled against older version of FSharp.Core in an application that uses newer version of FSharp.Core. The .NET runtime loads the new version of FSharp.Core but it does not know that types from the older version (like FSharpFunc) should be mapped to corresponding types in the new version - and so you get MethodMissing, because .NET thinks that FSharpFunc is a different type than the loaded one. (Though things get a bit more complicated with type providers.)