How to find Service from ServiceStack RequestFilter

旧街凉风 提交于 2019-11-29 08:57:30

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 headers
res.EndServiceStackRequest();
return;

Or I can create an error response, or just return from the RequestFilter and let the service execute normally.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!