ServiceStack服务网关抛出AggregateException而不是WebServiceException

时间:2018-12-03 08:52:37

标签: exception-handling servicestack

我正在切换到使用ServiceGateway从ASP.net控制器中执行请求。之前,我只能将调用包装在带有catch的Try Catch块中(WebServiceException ex),现在将异常作为AggregateException抛出,并带有WebServiceException的innerException。

 try
  {
    var request = new GetRequest();
    var response = HostContext.AppHost.GetServiceGateway(HostContext.GetCurrentRequest()).Send(request);
    //within above call the validator throws exception
    ...
  }
  catch (WebServiceException ex)
  {
    // no longer reaches here

    if (ex.ResponseStatus.ErrorCode == "404")
      return HttpNotFound();
    throw;
  }
  catch (AggregateException ex)
  {
    // reaches here
  }

引发异常后立即进行异常堆栈跟踪

at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at ServiceStack.InProcessServiceGateway.ExecSync[TResponse](Object request)
   at ServiceStack.InProcessServiceGateway.Send[TResponse](Object requestDto)
   at ServiceStack.ServiceGatewayExtensions.Send[TResponse](IServiceGateway client, IReturn`1 request)
   at Web.MT.Controllers.User.UserController.List(String categoryHandle, Int32 pageNumber) in C:\Dev\...

在我不得不去重构大量代码之前……这是预期的行为吗?我只是做错了吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我无法重现此行为,我添加了一个TestGateway Service,它在经典的ASP.NET MVC控制器中引发了Not Found异常,但仍然仅引发了WebServiceException。 / p>

ServiceStack服务

public class TestGateway : IReturn<TestGatewayResponse>
{
    public string Name { get; set; }
}

public class TestGatewayResponse
{
    public string Result { get; set; }

    public ResponseStatus ResponseStatus { get; set; }
}

public class TestGatewayService : Service
{
    public object Any(TestGateway request) =>
        throw HttpError.NotFound("NotFound");
}

MVC控制器中的网关

public ActionResult Contact()
{
    try
    {
        var request = new TestGateway {Name = "MVC"};
        var gateway = HostContext.AppHost.GetServiceGateway(HostContext.GetCurrentRequest());
        var response = gateway.Send(request);
        //within above call the validator throws exception
    }
    catch (WebServiceException ex)
    {
        // no longer reaches here
        if (ex.ResponseStatus.ErrorCode == "NotFound")
            return HttpNotFound();
        throw;
    }

    return View(GetViewModel("Contact"));
}

哪个被WebServiceException处理程序捕获并返回HttpNotFound()

除了升级到最新的ServiceStack之外,我什么都没有推荐,否则,如果您可以在GitHub上发布指向独立repro的链接,我可以进行进一步调查。