加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET中的Akka.NET actor系统

发布时间:2020-12-15 23:14:57 所属栏目:asp.Net 来源:网络整理
导读:我在ASP.NET中创建了一个RESTful API的服务,它在IIS中托管.在这个服务中,我想用Akka.NET创建一个演员系统. 创建演员系统后: var actorSystem = ActorSystem.Create("myActorSystem"); 抛出以下异常: A first chance exception of type ‘System.InvalidOpe
我在ASP.NET中创建了一个RESTful API的服务,它在IIS中托管.在这个服务中,我想用Akka.NET创建一个演员系统.

创建演员系统后:

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.

演员系统固有地是一个并发系统,在演员之间交换异步消息.如here所述,这个actor系统在IIS取消AppDomain之后将无法生存,这可能是为什么抛出上述异常.

This article解释了如何在ASP.NET中运行后台任务.但是,由于我无法控制Akka.NET可能创建的后台任务的生命周期,所以我看不到我可以使用这个作为我的actor系统.

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

编辑:我也看到关于implementing a REST service using Akka的Stackoverflow有一个问题.关于类似于Spray toolkit的解决方案的任何建议,但对Akka.NET的工作将是受欢迎的.

解决方法

将ActorSystem作为一个静态类容器中的一个共享属性,这样您可以从其余的应用程序访问它.演员系统初始化/处理可以通过以下方式完成:

Global.asax – 在Global.asax Application_Start中使用ActorSystem.Create(…),并将其与Application_End上的system.Shutdown()配置.
> OWIN – 在OWIN的Startup.Configuration方法中创建actor系统,并通过绑定到host.OnAppDisposing事件(how-to link)来关闭它.

请记住,只有在第一次请求之后,IIS将启动您的Web应用程序,并在空闲时间后自动将其关闭.因此,如果您希望Akka演员系统连续运行,您的部署脚本将在发布后ping应用程序并设置空闲超时(link)足够长.

第二个选择

分离您的Actor System逻辑并将其部署,例如,作为Windows服务(或Linux deamon).打开Akka.Remoting并创建代理客户端,将所有应用程序长期运行的敏感任务转发到外部服务.当应用程序逻辑必须连续工作时,类似的解决方案通常用于调度程序或事件总线等事务.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读