Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

servicestack - ServiceExceptionHandler usage on RestServiceBase<T>

I'm trying to use the ServiceExceptionHandler on my Serivce which extends RestServiceBase<TViewModel>

I can use the AppHost.ServiceExceptionHandler, that's working fine. I need the user info from the HttpRequest, thats not available at AppHost level.

So I'm trying to use the ServiceExceptionHandler on Service level. Though I set the delegate on service ctor, it's null when exception thrown on OnGet method

public class StudentService : RestServiceBase<Student>
{ 
    public StudentService()
    {
        ServiceExceptionHandler = (request, exception) =>
        {
            logger.Error(string.Format("{0} - {1} 
 Request : {2}
", HttpRequest.UserName(), exception.Message, request.Dump()), exception);
            var errors = new ValidationErrorField[] { new ValidationErrorField("System Error", "TODO", "System Error") };
            return DtoUtils.CreateErrorResponse("System Error", "System Error", errors);
        };
    }
}

I'm not sure of what is the issue with this code. Any help will be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Register Global AppHost.ServiceExceptionHandler

In your AppHost.Configure() you can register a global Exception handler with:

this.ServiceExceptionHandler = (request, ex) => {
   ... //handle exception and generate your own ErrorResponse
};

For finer-grained Exception handlers you can override the following custom service event hooks:

Handling Exceptions with the New API

If you're using the New API you can override the Exception by providing a custom runner, e.g:

public class AppHost { 
  ...
    public virtual IServiceRunner<TRequest> CreateServiceRunner<TRequest>(
        ActionContext actionContext)
    {           
        //Cached per Service Action
        return new ServiceRunner<TRequest>(this, actionContext); 
    }
}

public class MyServiceRunner<T> : ServiceRunner<T> {
    public override object HandleException(
        IRequestContext requestContext, TRequest request, Exception ex) {
      // Called whenever an exception is thrown in your Services Action
    }
}

Handling Exceptions with the Old API

RestServiceBase<T> is uses the old API in which you can handle errors by overriding the HandleException method, e.g:

public class StudentService : RestServiceBase<Student>
{ 
    ...

    protected override object HandleException(T request, Exception ex)
    {
        LogException(ex);

        return base.HandleException(request, ex);
    }
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...