Fire and forget with ServiceStack's AsyncServiceBase

后端 未结 1 517
星月不相逢
星月不相逢 2021-02-03 12:54

I have following service

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

    public override object ExecuteA         


        
相关标签:
1条回答
  • 2021-02-03 13:38

    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.

    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 (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.

    Future Async support

    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

    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.

    0 讨论(0)
提交回复
热议问题