5

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)'.
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137

1 Answers1

5

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.)

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • 3
    I had the correct redirects, but I think it might have been due to an older version of FSharp.Core. Upgrading to the latest, 4.4.0.0, in nuget (instead of the one including in the VS project) fixed it for me. – Tomas Jansson Oct 13 '15 at 14:42
  • 3
    Yeah, whenever I get odd error that makes no sense, I blame FSharp.Core :-) – Tomas Petricek Oct 13 '15 at 14:44
  • Yes, the nuget worked perfectly. Its funny that the code will compile and runs fine from F# interactive, but dies at runtime when run from the actual application... makes you scratch your head for a moment.... – schmoopy Apr 29 '16 at 03:23