servicestackrequestfiltering

Accessing Db in RequestFilter


I'm using ServiceStack 4 and currently access the database (Db) in a service without any issue. However, in a RequestFilter or a ResponseFilter, I do not have access to the Db property.

Is there any other way to access the database than resolving an <IDbConnection> instance in the Execute method of the filter? I tried resolving from a registered IDbConnection, but it is not as seemless as in a Service and the connection is closed.

I'm trying to keep it DRY and avoid breaking the Request chain.

Thank you for your help :)


Solution

  • You can resolve a IDbConnection from the IDbConnectionFactory that's registered in your AppHost IOC (i.e. you can only resolve registered dependencies), e.g:

    using (var db = HostContext.Resolve<IDbConnectionFactory>().Open()) 
    {
        //...
    }
    

    This is also how the base Service class resolves its IDbConnection.