为什么 Giraffe/AspNetCore + SignalR 依赖注入无法解析 MailboxProcessor 单例?

Ben*_*ins 5 f# dependency-injection asp.net-core f#-giraffe

我正在设置一个简单的Giraffe应用程序,其中包含一个或两个端点和一个 SignalR 集线器。我所拥有的是这样的:

type JsonBlob = JsonProvider<"Blob.json">
type Message = 
    | GetBlobs of AsyncReplyChannel<JsonBlob.Root list>
    | PostBlob of JsonBlob.Root

type JsonBlobHub(agent : MailboxProcessor<Message>) =
    inherit Hub()
    member self.RespondToClient() =
        let blobs = agent.PostAndReply(GetBlobs)
        self.Clients.All.SendAsync("ReceiveBlobList", blobs)

let agentFactory(serviceProvider : IServiceProvider) =
    let thing = serviceProvider.GetService<Thing>()
    MailboxProcessor.Start(fun (inbox : MailboxProcessor<Message>) ->
        /* loop implementation */
    )

// other stuff
let configureApp (app : IApplicationBuilder) =
    app.UseSignalR(fun routes -> routes.MapHub<JsonBlobHub>(PathString "/blobhub")) |> ignore
    app.UseGiraffe webApp // webApp defined elsewhere, not important

let configureServices (services : IServiceCollection) =
    services.AddSingleton<MailboxProcessor<Message>>(agentFactory) |> ignore
    services.AddGiraffe() |> ignore
    services.AddSignalR() |> ignore

let main argv =
    WebHostBuilder() =
        .UseKestrel()
        .UseWebRoot("WebRoot")
        .Configure(Action<IApplicationBuilder> configureApp)
        .ConfigureServices(configureServices)
        .ConfigureLogging(configureLogging)
        .Build()
        .Run
    0
Run Code Online (Sandbox Code Playgroud)

当 SignalR 客户端连接到 时/blobhub,连接意外关闭,因为应用程序MailboxProcessor<Message>在尝试激活BlobHub类时无法解析。

然而,我有点难住了,因为我已经清楚地MailboxProcessor<Message>configureServices函数的容器中注册了类型。有没有人看到这段代码有问题?或者,也许我假设这些东西应该起作用,但有一些我不知道它们不应该起作用的原因?

Ben*_*ins 2

好吧......事实证明我做了一件愚蠢的事情并且不小心有了两个定义Message。我JsonBlobHub在使用一种定义的同时agentFactory,也在configureServices使用另一种定义。一旦我删除了 DI 容器的定义之一,就解决了您所期望的Message激活问题。JsonBlobHub

我想说这最终是浪费时间,但它确实导致了一个很好的独立小示例,将 F#、Giraffe、ASP.NET Core 和 SignalR 一起使用,并证明所有部分可以很好地协同工作。