MissingMethodException with Suave & Fable.Remoting



下面是重现这个问题的最小示例。

尝试访问该端点,无论是使用Fable客户端还是导航到http://127.0.0.1:8080/ITestAPI/Test,都会导致服务器抛出未找到的方法:

[ERR] request failed
System.MissingMethodException: Method not found: 'Microsoft.FSharp.Collections.FSharpMap`2<System.String,System.Object> HttpContext.get_userState()'.
at Fable.Remoting.Suave.SuaveUtil.setBinaryResponseBody@26-1.Invoke(Unit unitVar)
at Microsoft.FSharp.Control.AsyncPrimitives.CallThenInvoke[T,TResult](AsyncActivation`1 ctxt, TResult result1, FSharpFunc`2 part2) in F:workspace_work1ssrcfsharpFSharp.Coreasync.fs:line 386
at Fable.Remoting.Server.Proxy.makeEndpointProxy@80-10.Invoke(AsyncActivation`1 ctxt)
at Microsoft.FSharp.Control.AsyncPrimitives.unitAsync@577.Invoke(AsyncActivation`1 ctxt) in F:workspace_work1ssrcfsharpFSharp.Coreasync.fs:line 577
at Program.greetFun@12-1.Invoke(AsyncActivation`1 ctxt) in C:UsersusernamecodetestsuavetestProgram.fs:line 12
at Microsoft.FSharp.Control.AsyncPrimitives.unitAsync@577.Invoke(AsyncActivation`1 ctxt) in F:workspace_work1ssrcfsharpFSharp.Coreasync.fs:line 577
at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in F:workspace_work1ssrcfsharpFSharp.Coreasync.fs:line 105

这几天我一直在想这个问题,没有任何进展。使用。net Core 3.1和。net 5都会出现同样的错误。

有人知道是什么原因导致的吗?我看不出和菲布有什么问题。对这件事冷淡或温和,所以我不得不想象是我做错了什么?

这看起来像一个FSharp。核心不匹配问题。你是否依赖于某个特定的FSharp ?核心软件包低于4.7.2?

一般来说,除非你要写一个在NuGet上发布的库,否则你应该而不是锁定你对FSharp的依赖。核心。对于应用程序,请始终使用。net SDK提供的任何内容,这样您几乎不会遇到这些问题。

明确FSharp。核心包引用对于库作者来说是一个额外的问题,他们需要担心自己的包与哪个f#版本兼容。如果这不是你所需要的,那么最好不要选择那些额外的复杂性。

使用您的示例代码在一个全新的。net 5控制台应用程序为我工作:

Test.fsproj

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="fable.remoting.suave" Version="4.13.0" />
</ItemGroup>
</Project>

Program.fs

open System
open Suave
open Fable.Remoting.Server
open Fable.Remoting.Suave
type ITestAPI = {
Test : Async<string>
}
let greetFun = 
async {
return "It works!"
}
let testAPI = {
Test = greetFun
}
let webApp  = 
Remoting.createApi()
|>Remoting.fromValue testAPI
|> Remoting.withDiagnosticsLogger (printfn "%s")
|>Remoting.buildWebPart
[<EntryPoint>]
let main argv =
startWebServer defaultConfig webApp
0 // return an integer exit code

在被子下面,使用FSharp。SDK中的内核版本为4.7.0或更高,这使得它与您正在使用的库兼容,并且在运行时不会产生异常。

我遇到了同样的问题。菲利普是对的,但他漏掉了一个重要的部分。问题是当前版本的fabile . remoting .Suave是基于Suave 2.5.6构建的。如果您引用这个特定版本的Suave,则可以在.fsproj文件中拥有这两个依赖项:

<ItemGroup>
<PackageReference Include="Fable.Remoting.Suave" Version="4.29.0" />
<PackageReference Include="Suave" Version="2.5.6" />
</ItemGroup>

最新更新