使用ServiceStack Async ServiceBase解决问题

时间:2012-06-20 16:35:22

标签: servicestack

我有以下服务

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)
    }
}

由不同的消费者按照方式调用

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

使用asynconeway我假设它可以让我实现火力而忘记WCF对IsOneWay的影响。但似乎并非如此。

我错过了什么吗?

1 个答案:

答案 0 :(得分:8)

AsyncServiceBase已弃用,因为ExecuteAsync现在位于ServiceBase中,这是在向 / asynconeway / XXX 预定义端点发出请求时调用的内容。

推荐的方法不是覆盖 ExecuteAsync ,而是实现IMessageFactory,如果在AppHost IOC中注册IMessageFactory,则会调用该Redis and Messaging wiki。如果IMessageFactory没有注册,那么它只是执行同步 - 此时如果你仍然想要它非阻塞你将覆盖它。 ExecuteAsync的impl位于:

// 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 IMessageService的端到端解决方案的示例,请参阅InMemory。还有RConasync branch IMesssageService可用,也很容易创建自己的。

未来异步支持

还有一个ServiceStack-v4.00-alpha.zipIHttpAsyncHandler上运行ServiceStack,并且已经有一个功能性的alpha版本可供您试用:Task<> plugin

通过此更改,ServiceStack支持Task<>作为服务的返回类型。您只需注册this integration test即可。要查看完整示例,请查看{{3}}。