为什么ServiceStack不能从返回Guid的方法处理异常?

时间:2015-09-29 15:29:44

标签: c# exception-handling servicestack

注意:我确认ServiceStack 3.9.71.0和4.0.46.0会出现此问题。

这是我非常基本的服务代码:

namespace MyServiceStackApplication.Services
{
    [Route("/hello")]
    [Route("/hello/{Name}")]
    public class Hello
    {
        public string Name { get; set; }
    }

public class HelloService : Service
{
    public Guid Any(Hello request) // Note that this method returns a Guid.
    {
        throw new Exception("My test exception."); // Note that I am throwing an exception here.
    }
}
}

这是我的Global.asax.cs代码:

public class MvcApplication : HttpApplication
{
    public class AppHost : AppHostBase
    {
        public AppHost() : base("Hello Web Services", typeof(HelloService).Assembly) { }

    public override void Configure(Container container)
    {
        //register any dependencies your services use, e.g:
        //container.Register<ICacheClient>(new MemoryCacheClient());
    }

    public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
    {
        return new MyServiceRunner<TRequest>(this, actionContext);
    }

    public class MyServiceRunner<T> : ServiceRunner<T>
    {
        public MyServiceRunner(IAppHost appHost, ActionContext actionContext) : base(appHost, actionContext)
        {
        }

        public override object HandleException(IRequest request, T requestDto, Exception ex)
        {
            Console.WriteLine(ex);
            return base.HandleException(request, requestDto, ex);
        }
    }
}

protected void Application_Start()
{
    //AreaRegistration.RegisterAllAreas();
    //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    //RouteConfig.RegisterRoutes(RouteTable.Routes);
    //BundleConfig.RegisterBundles(BundleTable.Bundles);

    new AppHost().Init();
}
}

我的问题:

  • 当我致电HandleException时,为什么我的http://localhost:xxxxx/hello方法在堆栈跟踪顶部没有收到抛出的异常?enter image description here

1 个答案:

答案 0 :(得分:1)

您只能从ServiceStack服务返回一个参考类型,该服务理想情况下应该是一个响应DTO,但也可以是原始类型,如stringbyte[]Stream等。< / p>