使用 ServiceStack 的 AsyncServiceBase 开火即忘 [英] Fire and forget with ServiceStack's AsyncServiceBase

查看:18
本文介绍了使用 ServiceStack 的 AsyncServiceBase 开火即忘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下服务

public class AppService : AsyncServiceBase<EvaluateStock>
{
    public IBus Bus { get; set; }

    public override object ExecuteAsync(EvaluateStock request)
    {
        // this will block the incoming http request 
        // unitl task is completed

        // long computation
        // Bus.Publish(result)
    }
}

被不同的消费者按照以下方式调用

which gets called by different consumers following way

POST
http://srv1/app/json/asynconeway/EvaluateStock

使用 asynconeway 我假设它可以让我像 WCF 使用 IsOneWay 那样实现火灾和遗忘.但似乎并非如此.

Using asynconeway I was assuming that it will allow me to achieve fire and forget as WCF does with IsOneWay. But seems is not the case.

我错过了什么吗?

推荐答案

AsyncServiceBase 已被弃用,因为 ExecuteAsync 是现在在 ServiceBase 中,当向 /asynconeway/XXX 预定义端点发出请求时,它会被调用.

AsyncServiceBase has been deprecated as ExecuteAsync is now in ServiceBase which is what gets called when a request is made to /asynconeway/XXX pre-defined endpoint.

推荐的方法是实现 IMessageFactory 如果 IMessageFactory 已在 AppHost IOC 中注册,则会调用它.如果 IMessageFactory 未注册,则它只是被执行同步 - 此时如果您仍然希望它非阻塞,您将覆盖它.ExecuteAsync 的实现位于:

Rather than overriding ExecuteAsync the recommended approach is to implement IMessageFactory which is what gets called if an IMessageFactory has been registered in the AppHost IOC. If an IMessageFactory wasn't registered than it just gets executed Sync - at which point if you still wanted it non-blocking you would override it. The impl for ExecuteAsync is at:

// Persists the request into the registered message queue if configured, 
// otherwise calls Execute() to handle the request immediately.
// 
// IAsyncService.ExecuteAsync() will be used instead of IService.Execute() for 
// EndpointAttributes.AsyncOneWay requests
public virtual object ExecuteAsync(TRequest request)
{
    if (MessageFactory == null)
    {
        return Execute(request);
    }

    BeforeEachRequest(request);

    //Capture and persist this async request on this Services 'In Queue' 
    //for execution after this request has been completed
    using (var producer = MessageFactory.CreateMessageProducer()) {
        producer.Publish(request);
    }

    return ServiceUtils.CreateResponseDto(request);
}

IMessageFactory(客户端)/IMessageService(服务器)是 ServiceStack 的 Messaging API 的一部分,它允许您发布消息以便稍后延迟执行.请参阅 Redis 和消息传递 wiki,了解使用内置Redis IMessageService.还有 InMemoryRCon IMesssageService 可用,也应该很容易创建自己的.

IMessageFactory (client)/IMessageService (server) is apart of ServiceStack's Messaging API which allows you to publish messages for deferred execution later. See the Redis and Messaging wiki for an example of an end-to-end solution that uses the built-in Redis IMessageService. There are also InMemory and RCon IMesssageService's available and it should be easy to create your own as well.

还有一个 异步分支,它在 IHttpAsyncHandler 上运行了 ServiceStack,并且已经有一个功能性 alpha 版本可供您尝试:ServiceStack-v4.00-alpha.压缩包

There is also an async branch that has ServiceStack running on IHttpAsyncHandler and already has a functional alpha build available for you to try at: ServiceStack-v4.00-alpha.zip

通过此更改,ServiceStack 支持将 Task<> 作为服务的返回类型.您只需要注册Task<>插件.要查看完整示例,请查看此集成测试.

With this change ServiceStack supports Task<> as a return type on services. You only need to register the Task<> plugin. To see a full example look at this integration test.

这篇关于使用 ServiceStack 的 AsyncServiceBase 开火即忘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆