所以我想通过某种方式让 RequestFilter 或 RequestFilterAttribute 了解它正在作用(或声明)的 Service) 然后想要在 Service 中查找一个方法,该方法将提供启用/禁用过滤器代码所需的逻辑.我不知道 IoC 容器的某些功能是否提供了这一点,或者是否有其他方法可以做到这一点.然后,根据过滤器的执行方式,它可能需要返回自己的数据,从而阻止服务实际执行.这可能吗?(是 this 答案是什么?) 解决方案 这似乎是关键:var serviceType = EndpointHost.Metadata.GetServiceTypeByRequest(requestDto.GetType());这将返回将处理请求的服务类型.它不是一个实例(但我怀疑是否已经创建了一个服务实例)所以为了做条件逻辑我必须定义一个静态方法然后查找它.我使用反射找到了一种声明特殊属性的方法,并且需要对其进行优化以获得最佳性能.从那里我可以有条件地确定是否运行一些逻辑.如果我愿意,我也可以绕过调用该服务并返回成功响应,如下所示:var successResponse = DtoUtils.CreateSuccessResponse(successMessage);var responseDto = DtoUtils.CreateResponseDto(requestDto, successResponse);var contentType = req.ResponseContentType;res.ContentType = contentType;res.WriteToResponse(req, responseDto);//这是关闭请求的正确方法;您可以使用 Close() 但它不会写出全局标头res.EndServiceStackRequest();返回;或者我可以创建一个错误响应,或者只是从 RequestFilter 返回并让服务正常执行.I'm trying to implement a RequestFilter that conditionally executes, based on information in the Service that would get invoked. I'd like to make the RequestFilter find the Service, look at it for a method/interface/attribute, and conditionally do its work based on that.I'm aware that you can declare a RequestFilterAttribute on the Service, but I couldn't figure out a good way to make it conditional. I wanted to pass a delegate/lambda into the attribute, but C# doesn't allow that. I could have plugged in a Type or type name into there, allowing the RequestFilterAttribute to find the Service class/method, but that seemed prone to copy/paste errors.So I'm left with wanting some way for a RequestFilter or RequestFilterAttribute to know about the Service it's acting on (or declared on) and then wanting to look up a method inside that Service that would provide the logic needed to enable/disable the filter's code. I couldn't tell if some feature of the IoC container offered this, or if there is some other way to do it, or not.And then, based on how the filter executes, it might need to return its own data, blocking the Service from actually executing. Is this possible? (Is this the answer for that?) 解决方案 This seems to be the key:var serviceType = EndpointHost.Metadata.GetServiceTypeByRequest(requestDto.GetType());This returns the type of the service that will handle the request. It's not an instance (but I am doubtful a service instance has even been created yet) so to do conditional logic I had to define a static method and then look it up. I used reflection to find a method declaring a special attribute, and will need to optimize that for best performance.From there I can conditionally determine whether to run some logic. I can also bypass calling the service if I like and return a success response as follows:var successResponse = DtoUtils.CreateSuccessResponse(successMessage);var responseDto = DtoUtils.CreateResponseDto(requestDto, successResponse);var contentType = req.ResponseContentType;res.ContentType = contentType;res.WriteToResponse(req, responseDto);// this is the proper way to close the request; you can use Close() but it won't write out global headersres.EndServiceStackRequest();return;Or I can create an error response, or just return from the RequestFilter and let the service execute normally. 这篇关于如何从 ServiceStack RequestFilter 中查找服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 23:09