f# - Suave 和 Fable.Remoting 中的 MissingMethodException

标签 f# suave

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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81c7e0e3ede4afd3e4eceef5e8efe6afd2f4e0f7e4afd2f4e0f7e4d4f5e8edaff2e4f5c3e8efe0f3f8d3e4f2f1eeeff2e4c3eee5f8c1b3b7acb0afc8eff7eeeae4" rel="noreferrer noopener nofollow">[email protected]</a>(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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e082f2c222b601c2b23213a272029601d2b3c382b3c601e3c21363760232f252b0b202a3e2127203a1e3c2136370e767e637f7e6007203821252b" rel="noreferrer noopener nofollow">[email protected]</a>(AsyncActivation`1 ctxt)
   at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d00242e3f223e222b39630b1e252c3f3d630e2223393f2221630c3e34232e1d3f24202439243b283e63382324390c3e34232e0d787a7a6304233b222628" rel="noreferrer noopener nofollow">[email protected]</a>(AsyncActivation`1 ctxt) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 577
   at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e0e2c31392c3f3370392c3b3b2a182b301e6f6c736f7017302831353b" rel="noreferrer noopener nofollow">[email protected]</a>(AsyncActivation`1 ctxt) in C:\Users\username\code\test\suavetest\Program.fs:line 12
   at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35785c56475a465a53411b73665d5447451b765a5b41475a591b74464c5b5665475c585c415c4350461b405b5c4174464c5b56750002021b7c5b435a5e50" rel="noreferrer noopener nofollow">[email protected]</a>(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 与此相关的任何 Unresolved 问题,所以我必须想象这是我做错的事情?

最佳答案

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

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

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

在全新的 .NET 5 控制台应用程序中使用示例代码对我来说很有效:

测试.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 中 4.7.0 版本或更高版本的 FSharp.Core,这使得它与您正在使用的库兼容,并且在运行时不会出现异常.

关于f# - Suave 和 Fable.Remoting 中的 MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66212402/

相关文章:

F# 如何返回具有元组或空值的值

f# - 从联合案例中获取值(value)

f# - 如何确定 System.Type 值的大小?

f# - 使用组合器清理 Suave.io 中的 mapJsonAsync

f# - Suave - 控制何时响应为 'cached' 或重新计算

f# - 使用 Suave 发送具有正确名称的 (zip) 文件

f# - 元组函数组合

f# - 在没有 SQL 类型提供程序的情况下连接到 SQL 数据库?

linux - 风流与netcoreapp2.0

rest - Suave with f# - 如何在 f# 聊天应用程序中使用 rest api 和 websocket 端口?