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

ServiceStack passing values in service to response attribute

我有一个服务:

[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 做一些事情。我如何访问它?谢谢。

过滤器属性无权访问服务实例,您可以使用 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"];
    }
}