Search code examples
c#servicestackrequestfiltering

How do I access ServiceStack's Service.Db instance from within a RequestFilter?


I have a C# ServiceStack RequestFilter...

public class VerifyRequestedSystemsAttribute : Attribute, IHasRequestFilter
{
    IHasRequestFilter IHasRequestFilter.Copy()
    {
        return this;
    }


    public void RequestFilter(IRequest req, IResponse res, object requestDto)
    {
        var systemIds = RetrieveSystemIds(requestDto);

        Db.Select...? //How do I make Db available for use?
    }

    private List<int> RetrieveSystemIds(object requestDto)
    {
        // Find the properties on the requestDto having the RequestedSystemIds attribute
        var type = requestDto.GetType();
        var properties = type.GetPublicProperties();
        foreach (var systemIds in
                    from p in properties
                    where p.HasAttribute<RequestedSystemsAttribute>() && p.PropertyType == typeof(List<int>) && p.CanRead && p.CanWrite
                    select p.GetValue(requestDto, null)
                        as List<int>)
            return systemIds;

        return new List<int>();
    }


    // The lowest priority means it will run first, before other custom attributes
    public int Priority { get { return 0; } }
}

Are you can see - in the RequestFilter method I'd like to query the database but I don't have access to the Service.Db instance.

What is the correct way to get the Service.Db instance? Should I be using a ServiceRunner instead?


Solution

  • IOC Dependencies are also injected in Filter Attributes so you can add public properties of the dependencies your Request Filter Attribute needs, e.g:

    public class VerifyRequestedSystemsAttribute : Attribute, IHasRequestFilter
    {
        public IDbConnectionFactory DbFactory { get; set; }
    
        private List<int> RetrieveSystemIds(object requestDto)
        {
            using (var db = DbFactory.OpenConnection())
            {
                //...
            }
        }
    }