none
Unable to Catch fault exception in restfull service RRS feed

  • Question

  • Hello all,

     I have created one RESTFull service, and I am trying to get fault exception,but along  with fault exception ( shown in yellow color) I am getting stacktrace also.

    can any body know  how to get rid of that

    plzz help.

    thanks.

    Wednesday, April 29, 2015 1:25 PM

Answers

  • If you just want to know if there is exception occurred other than see those information like stacktrace, then you can set includeExceptionDetailInFaults to false in ServiceDebugBehavior in App.config/Web.config,

    <serviceBehaviors>
            <behavior name="Default">
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>

    Or set IncludeExceptionDetailInFaults to false in ServiceBehaviorAttribute.

    [ServiceBehavior(IncludeExceptionDetailInFaults = false)] public class Service1 : IService1

    {

    }

    But doing this, you won't see the error message either.

    One way to handle exception is you can encapsulate the exception in ExceptionDetail, and throw WebFaultException<ExceptionDetail>, and in client side, you can use try/catch to process the exception.

    ExceptionDetail ed = new ExceptionDetail(new Exception("Balabala"));
    throw new WebFaultException<ExceptionDetail>(ed, System.Net.HttpStatusCode.BadRequest);

    If you want to use self-defined type to store the exception details other that ExceptionDetail, you need to use FaultContract, please refer to following as an example.

    https://msdn.microsoft.com/en-us/library/ms752208(v=vs.100).aspx

    http://blogs.msdn.com/b/endpoint/archive/2010/01/21/error-handling-in-wcf-webhttp-services-with-webfaultexception.aspx

    Friday, May 8, 2015 2:09 AM

All replies

  •  I have created one RESTFull service, and I am trying to get fault exception,but along with fault exception ( shown in yellow color) I am getting stacktrace also.

    What do you mean? Try to describe more about your question.

    I sale myself ONLY half CNY!

    Friday, May 1, 2015 8:50 AM
  • You can use a try/catch I would suspect, and throw an exception the way you want it.
    Friday, May 1, 2015 4:40 PM
  • If you just want to know if there is exception occurred other than see those information like stacktrace, then you can set includeExceptionDetailInFaults to false in ServiceDebugBehavior in App.config/Web.config,

    <serviceBehaviors>
            <behavior name="Default">
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>

    Or set IncludeExceptionDetailInFaults to false in ServiceBehaviorAttribute.

    [ServiceBehavior(IncludeExceptionDetailInFaults = false)] public class Service1 : IService1

    {

    }

    But doing this, you won't see the error message either.

    One way to handle exception is you can encapsulate the exception in ExceptionDetail, and throw WebFaultException<ExceptionDetail>, and in client side, you can use try/catch to process the exception.

    ExceptionDetail ed = new ExceptionDetail(new Exception("Balabala"));
    throw new WebFaultException<ExceptionDetail>(ed, System.Net.HttpStatusCode.BadRequest);

    If you want to use self-defined type to store the exception details other that ExceptionDetail, you need to use FaultContract, please refer to following as an example.

    https://msdn.microsoft.com/en-us/library/ms752208(v=vs.100).aspx

    http://blogs.msdn.com/b/endpoint/archive/2010/01/21/error-handling-in-wcf-webhttp-services-with-webfaultexception.aspx

    Friday, May 8, 2015 2:09 AM