Search code examples
asynchronousserviceservicestack

ServiceStack: Will there be an OnBeforeExecuteAsync?


I have a ServiceStack Service with a baseclass, and I was hoping to execute some code "OnBeforeExecute", like below. However, I am mostly encountering async methods that needs to be awaited, and generally, this is something I try to follow everywhere (async everywhere). This becomes a problem though, because the OnBeforeExecute does not exist as an awaitable:

public override void OnBeforeExecute(object requestDto)
{
    base.OnBeforeExecute(requestDto);
    await Redis.GetAsync<Test>("asd"); // <-- async example call
}

Is there plans to introduce an virtual Task OnBeforeExecute(object requestDto) to remedy this, or do you have any other suggestions?


Solution

  • You can use an async Global Request Filter or async Filter attribute.

    From v5.10.4+ your Services can implement IServiceBeforeFilterAsync to execute OnBeforeExecuteAsync() before each request, e.g:

    public class MyServices : Service, IServiceBeforeFilterAsync 
    {
        public Task OnBeforeExecuteAsync(object requestDto)
        {
            //...
        }
    }