我已经编写了自定义 ServiceRunner 并覆盖了 HandleException 方法。正如我所看到的,如果服务中出现未处理的异常,HandleException 方法返回的对象将成为服务响应。我希望返回与服务在成功的情况下返回的相同的 responseDTO 类型。有什么方法可以在 HandleException 内部访问遇到异常的服务的 responseDTO 类型?
编辑:根据收到的答案,我使用定义如下的 DTO/服务类型实现了服务(在 F# 中):
type CS()=
interface IReturn<Op<list<Y>,list<E>>>
member val key = List<int>() with get,set
type CSService() =
interface IService
member this.Get(request: CS)=
//service code here returning Op<list<Y>,seq<E>> type
因此,我希望在 HandleException 中收到的类型是Op<list<Y>,list<E>>
(Y 和 E 是在 bin 文件夹中可用的 dll 中定义的已知类型)
根据建议,我尝试了以下两个选项,因为 WebRequestUtils 在两个命名空间中都可用:
Type test1 = ServiceStack.WebRequestUtils.GetErrorResponseDtoType(request.GetType());//error
Type test2 = ServiceStack.ServiceClient.Web.WebRequestUtils.GetErrorResponseDtoType(request.GetType());
对于 test1 我收到以下异常:
Could not load type 'ServiceStack.ErrorResponse' from assembly 'ServiceStack.Interfaces, Version=3.9.71.0, Culture=neutral, PublicKeyToken=null'.
堆栈跟踪:
at ServiceStack.WebRequestUtils.GetErrorResponseDtoType(Type requestType)
at M.ServiceStackWebAPP.MyCustomServiceRunner`1.HandleException(IRequestContext requestContext, TRequest request, Exception ex) in d:\Projects\ServiceStack\M.WebService\MyCustomServiceRunner.cs:line 167
at ServiceStack.ServiceHost.ServiceRunner`1.Execute(IRequestContext requestContext, Object instance, TRequest request)
at ServiceStack.ServiceHost.ServiceRunner`1.Process(IRequestContext requestContext, Object instance, Object request)
at ServiceStack.ServiceHost.NServiceExec`1.Execute(IRequestContext requestContext, Object instance, Object request, String requestName)
at ServiceStack.ServiceHost.NServiceRequestExec`2.Execute(IRequestContext requestContext, Object instance, Object request)
at ServiceStack.ServiceHost.ServiceController.<>c__DisplayClass15.<>c__DisplayClass17.<RegisterNServiceExecutor>b__14(IRequestContext reqCtx, Object req)
at ServiceStack.ServiceHost.ServiceController.ManagedServiceExec(ServiceExecFn serviceExec, Object service, IRequestContext requestContext, Object dto)
at ServiceStack.ServiceHost.ServiceController.<>c__DisplayClass15.<RegisterNServiceExecutor>b__13(IRequestContext requestContext, Object dto)
at ServiceStack.ServiceHost.ServiceController.Execute(Object request, IRequestContext requestContext)
at ServiceStack.WebHost.Endpoints.EndpointHost.ExecuteService(Object request, EndpointAttributes endpointAttributes, IHttpRequest httpReq, IHttpResponse httpRes)
at ServiceStack.WebHost.Endpoints.Support.EndpointHandlerBase.ExecuteService(Object request, EndpointAttributes endpointAttributes, IHttpRequest httpReq, IHttpResponse httpRes)
at ServiceStack.WebHost.Endpoints.RestHandler.GetResponse(IHttpRequest httpReq, IHttpResponse httpRes, Object request)
at ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName)
调用 test2 成功,但我收到ErrorResponse
type 作为返回值而不是 expected Op<list<Y>,list<E>>
。
我还缺少什么吗?