【问题标题】:MissingMethodException with Suave & Fable.Remoting带有 Suave 和 Fable.Remoting 的 MissingMethodException
【发布时间】:2021-02-15 17:11:40
【问题描述】:

Here 是一个可以重现问题的最小示例。

尝试使用 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\_work\1\s\src\fsharp\FSharp.Core\async.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\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 577
   at Program.greetFun@12-1.Invoke(AsyncActivation`1 ctxt) in C:\Users\username\code\test\suavetest\Program.fs:line 12
   at Microsoft.FSharp.Control.AsyncPrimitives.unitAsync@577.Invoke(AsyncActivation`1 ctxt) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 577
   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 105

我已经为此烦恼了几天,但没有任何进展。使用 .Net Core 3.1 和 .Net 5 都会出现同样的错误。

任何人都知道可能导致这种情况的原因吗?我没有看到关于 Fable.Remoting 或 Suave 的任何未解决问题,所以我不得不想象这是我做错了什么?

【问题讨论】:

    标签: f# suave


    【解决方案1】:

    这看起来像是 FSharp.Core 不匹配问题。您是否依赖低于 4.7.2 的特定 FSharp.Core 包?

    一般来说,除非您正在编写一个用于在 NuGet 上分发的库,否则您根本不应该将您的依赖项固定在 FSharp.Core 上。对于应用程序,请始终使用 .NET SDK 提供的任何内容,您几乎不会遇到这些问题。

    对于需要担心其包与哪些 F# 版本兼容的库作者来说,明确地 FSharp.Core 包引用是一个额外的问题。如果这不是您的要求,那么最好不要选择额外的复杂性。

    在全新的 .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
    

    在幕后,这使用了 SDK 中的 FSharp.Core 版本 4.7.0 或更高版本,这使其与您正在使用的库兼容,并且在运行时不会出现异常.

    【讨论】:

    • 啊,我想就是这样。我的 fsproj 文件同时引用了 Fable.Remoting.Suave 和 Suave 本身。删除 Suave 引用后,它确实按预期工作!
    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多