ServiceStack将服务中的值传递给响应属性

时间:2015-06-27 06:12:26

标签: c# servicestack servicestack-bsd

我有服务:

[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?
        }
}

现在,如果我需要在响应属性中对CacheMemory执行某些操作,然后再将其发回。我如何获得它?谢谢。

1 个答案:

答案 0 :(得分:2)

过滤器属性无法访问Service实例,您可以使用IRequest.Items字典将对象传递给ServiceStack's Request Pipeline中的不同处理程序,例如:

[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"];
    }
}