Skip to main content
 首页 » 编程设计

c#之从 C# 调用 Freebase Provider 时 FSharp.Data 'System.MissingMethodException'

2024年02月13日17lautakyan007

您好,我在 F# 上有这段代码,如果我从 F# Interactive Editor 测试它,isPalindrome 和 Extract 方法都运行良好:

namespace Portable3 
open FSharp 
open FSharp.Data 
open Microsoft.FSharp.Linq  
open FSharp.Data.FreebaseOperators 
open MyTrip.Model.MyTrip 
open MyTrip.Model.FreeBase 
open System.Runtime 
open System.Linq 
 
module math =  
let isPalindrome (str : string) =  
 let rec check(s : int, e : int) = 
    if s = e then true 
    elif str.[s] <> str.[e] then false 
    else check(s + 1, e - 1) 
 check(0, str.Length - 1) 
 
 [<AutoOpen>] 
 module Extractor = 
 
[<Literal>]  
let FreebaseApiKey = "AIzaSyCO31Ls" 
type FreebaseDataWithKey = FreebaseDataProvider<Key=FreebaseApiKey> 
 
let Extract mid = let dataWithKey = FreebaseDataWithKey.GetDataContext() 
                  let place = dataWithKey.Commons.Travel.``Travel destinations``.Where( fun x-> x.MachineId = mid) |> Seq.toList                            
                  let result = new Place()    
                  let firstPlace = place.Head 
                  result.Name <- firstPlace.Name                       
                  result 

我从 C# 控制台应用程序调用此代码,如下所示:

 class Program 
{ 
    static void Main(string[] args) 
    { 
        //Works well 
        var isPalin = math.isPalindrome("ABsBA"); 
        //fails 
        var res = Extractor.Extract("/m/04jpl"); 
        Console.WriteLine(res); 
        Console.Read(); 
 
    } 
} 

控制台C#项目是.net Framework 4.5.1版本,我在这个项目上也下载了FSharp.Data和FSharp.Core。执行 isPalindrome 时效果很好,但是当我要执行 Extract 方法时出现此错误:

An unhandled exception of type 'System.MissingMethodException' occurred in FsharpConsoleTest.exe 
 
Additional information: Method not found: 'FSharp.Data.Runtime.Freebase.FreebaseDataContext   FSharp.Data.Runtime.Freebase.FreebaseDataContext._Create(System.String, System.String, System.Boolean, System.String, System.Boolean, System.Boolean)'. 

知道发生了什么事吗?我在互联网上搜索但没有找到任何相关内容。谢谢!

请您参考如下方法:

最后的问题是使用带有 FSharp.Data 的可移植库。我尝试在普通的 F# 库中使用它,没有发现任何问题,我拥有所有 debuggin 功能,并且没有发生 c# 与 f# 集成的错误!