asp.net - ASP.NET 中的 Akka.NET Actor 系统

标签 asp.net spray hangfire akka.net

我在 ASP.NET 中创建了一个带有 RESTful API 的服务,托管在 IIS 中。在这个服务中,我想用 Akka.NET 创建一个 Actor 系统。

创建 Actor 系统后:

var actorSystem = ActorSystem.Create("myActorSystem");

抛出以下异常:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.dll Additional information: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.



Actor 系统本质上是一个并发系统,在 Actor 之间交换异步消息。如解释 here ,这个 Actor 系统将无法在 IIS 关闭 AppDomain 后继续存在,这可能就是引发上述异常的原因。

This article解释了如何在 ASP.NET 中运行后台任务。但是,我不知道如何将它用于我的 Actor 系统,因为我无法控制可能由 Akka.NET 创建的后台任务的生命周期。

有没有办法使这项工作,或者我应该放弃在 ASP.NET 应用程序中拥有 Actor 系统的想法?

编辑:我还在 Stackoverflow 上看到了一个关于 implementing a REST service using Akka 的问题。 .关于类似于 Spray toolkit 的解决方案的任何建议,但欢迎为 Akka.NET 工作。

最佳答案

我在 ASP.NET MVC 应用程序中使用了 Akka.NET 和 Akka.Remote,它们在 EC2 上每秒执行多达 1000 个请求 - 所以我将分享一些我用来成功启动和运行它的技巧和窍门。有一个原型(prototype)版本,它甚至使用了 Akka.Cluster,但最终没有发布该版本。

  • 最佳通话地点ActorSystem.Create在 Global.asax Application_Start() 内.
  • 保留对 ActorSystem 的静态引用Global.asax 本身内部的对象,使用静态字段或属性。有助于确保 ActorSystem本身不会在长时间运行的应用程序中进行垃圾收集。
  • 创建一个单独的静态帮助器类来初始化您的应用程序需要的任何顶级参与者 - 即 /user/ 顶部的参与者等级制度。此类还应提供您的 ASP.MVC Controller 和操作方法可用于 Tell 的参与者路径和 Ask操作。

  • 创建 ActorSystem这是一个有点昂贵的操作,因为很多系统级的东西会立即启动。最好在应用程序启动时执行一次,然后将结果缓存在 Application 中。类(class)。
    创建单独的actor实例很便宜——您应该能够在 ASP.NET MVC 操作方法中毫无问题地做到这一点。如果您再次看到此错误,请让我们知道此错误发生在请求处理过程的哪个部分以及哪个版本的 ASP.NET。
    编辑:添加了一些关于如何在 ASP.NET Core 上执行此操作的更新指南
    https://petabridge.com/blog/akkadotnet-aspnetcore/

    关于asp.net - ASP.NET 中的 Akka.NET Actor 系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27634843/

    相关文章:

    ASP.NET 从我的代码后面设置 javascript、jQuery 设置

    asp.net - OWIN 自托管 CookieAuthentication 和旧版 .NET 4.0 应用程序/FormsAuthenticationTicket

    asp.net - 当我将 'Enable 32-Bit Applications' 更改为 False 时,为什么我的 IIS 7 拒绝提供 css 或 js

    scala - 使用分块响应通过 Spray 从 Play 枚举器流式传输数据

    spray - 指令如何在 Spray 中工作?

    c# - hangfire 中的重复工作未被解雇

    Hangfire 正在停止,日志捕获停止信号

    c# - ASP.NET MVC 中的面向方面编程

    scala - SSLException :Unrecognized SSL message, 明文连接

    c# - 无法在 ABP 中的 Hangfire 循环作业上访问处置对象(在 DbContext 上)错误