【问题标题】:ServiceStack passing values in service to response attributeServiceStack 将服务中的值传递给响应属性
【发布时间】:2015-06-27 06:12:26
【问题描述】:

我有一个服务:

[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 执行某些操作,然后再将其发回。我如何访问它?谢谢。

【问题讨论】:

    标签: c# servicestack servicestack-bsd


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 2018-10-05
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多