繁体   English   中英

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

[英]Why does Giraffe/AspNetCore + SignalR dependency injection fail to resolve a MailboxProcessor singleton?

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

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

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

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

嗯....事实证明我做了一件蠢事,偶然有两个Message定义。 我的JsonBlobHub正在使用一个定义,而agentFactoryconfigureServices正在使用另一个定义。 一旦我删除了Message的一个定义,DI容器就像你期望的那样解决了JsonBlobHub的激活问题。

我想说这最终只是浪费时间,但实际上它确实导致了一个很好的小型自包含的例子,即将F#,Giraffe,ASP.NET Core和SignalR结合使用,并证明所有部分都可以很好地协同工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM