Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# function in module showing "Method Not Found" when called from a namespace

I have a function defined in a module called DataAccess like this

module DataAccess
let getData() = 
    ["abc";"def"]

I'm then using an F# MVC controller to pull the data out like this

[<HandleError>]
type FitnessController() = 
    inherit Controller()

    member x.Index() =
        let data = DataAccess.getData() |> List.map(fun p -> p) |> List.toArray
        x.View(data) :> ActionResult

I get intellisense and all builds well, but when the web page pops up it says that the method does not exist

Method not found: 'Microsoft.FSharp.Collections.FSharpList`1<Models.Entity> DataAccess.getData()'.

When I take a look at the assembly in dotPeek it does show up as a static method that returns an FSharp list. Am I missing something obvious here?

(Ignore the fact that getData and the map function do nothing i've omitted the code for brevity, getData just contains record types that are marked as serializable, but I still get the error even when using strings as in the code example here) I should also say that this is MVC 3 with Razor C# pages.

like image 946
Dylan Avatar asked Oct 30 '11 19:10

Dylan


1 Answers

Do you have DataAccess and FitnessController defined in the same assembly or different assemblies?

If they are defined in different assemblies then this error is almost certainly caused by the assembly that contains FitnessController being compiled against one version of the assembly that contains DataAccess but at runtime a different version is loaded.

This could happen for a number of reasons, the two that I can think of off the top of my head are:

  • a version of the assembly containing DataAccess is deployed to the gac. The .NET assembly loader always uses the version deployed in the GAC if it exists and it's fairly easy for the version in the GAC to get out of sync with the latest development version.
  • the version of the assembly containing DataAccess isn't being copied into the bin directory of the asp.net project when your code is compiled.

Is this happen in your development environment or in production? If it's in your development environment it should be fairly easy to debug, simple run the project in debug mode then use the Debug > Windows > Modules window to see where the two assemblies are being loaded from. Once you know that it should be fairly easy to see where the problem comes from.

Alternatively it may just be easier DataAccess and FitnessController into the same assembly.

like image 71
Robert Avatar answered Nov 07 '22 09:11

Robert