Search code examples
c#servicestackservicestack-bsd

ServiceStack passing values in service to response attribute


I have a service:

[SomeResponse]
public class SomeService : ServiceBase {
    public string[] CacheMemory{ get; set; }
    //....
}

public class SomeResposeAttribute : ResponseFilterAttribute {
    public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto) {
            //I want to access SomeService->CacheMemory here?? How?
        }
}

Now, if I need to do something to CacheMemory in response attribute before I send it back. How do I get access to it? Thank you.


Solution

  • The Filter Attributes don't have access to the Service instance, you'd use the IRequest.Items dictionary in to pass objects to different handlers throughout ServiceStack's Request Pipeline, e.g:

    [MyResponseFilter]
    public class SomeService : Service 
    {
        public string[] CacheMemory { get; set; }
    
        public object Any(Request request)
        {
            base.Request.Items["CacheMemory"] = CacheMemory;
            //...
            return response;
        }
    }
    
    
    public class MyResponseFilterAttribute : ResponseFilterAttribute 
    {
        public override void Execute(IRequest req, IResponse res, object dto) 
        {
            var cacheMemory = (string[])req.Items["CacheMemory"];
        }
    }