How to pass back exceptions from WCF Service to client

+1 vote
asked Jun 12, 2013 in WCF by anonymous
edited Jun 13, 2013

I am learning WCF services and writing some sample services. So, first I created a WCF serviece with some operation contract. I have hosted that service in a console application. Then created proxy on another console application which is acting as client. When I called service operation, it threw some exception, but not able to see that in client application. It just gives me the below error

"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the...."

What should I do to get the exception back from the service to client?

Share

1 Answer

+1 vote
answered Jun 15, 2013 by anonymous
selected Jun 18, 2013 by administrator
 
Best answer

You are getting this generic error message back from your service because, you might not have enabled your service the include the exception detail. So, you did not enable this feature in your service, then the service will not send any information to the client because of security reason. Because sometime the exceptions may expose some important detail which client should not know. Otherwise, client may compromise your security layer.

But, if you are doing testing your service with the client and if you need know any exception detail then, you can simply enable IncludeExceptionDetailInFaults to true in ServiceBehaviors using serviceDebug. You can enable this either in Config file or in the code.

Below is the way you can do in Config File.

<behaviors>
  <ServiceBehaviors>
     <behavior>
       <serviceDebug includeExceptionDetailInFaults="ture"/>
     </behavior>
  </ServiceBehaviors>
</behaviors>

If you want to do the same in the code, then you can do it like below

[ServiceContract]
public interface ISampleService
{
    [OperationContract]
    void SampleContract();
}

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class SampleService
{
    public void SampleContract()
    {
        //do your code.
    }
}

But this approach is not available for production. If you want any exception suppose to be sent to client explicit then, instead of throwing normal exception, throw it as FaultException. If you throw FaultException, then the service dispatcher will sent the exception detail to the client.

If you throw FaultException, then you do not need to enable IncludeExceptionDetailInFaults. Because, when the service dispatch, see the FaultException, it does know that, we are intentionally sending the exception detail to the client.

Ex:

public class SampleService
{
    public void SampleContract()
    {
        //do your code.
      throw new FaultException("Something wrong with the process");
    }
}

In this way, client can implement try catch functionality and instead of catching normal exception, they can catch FaultException and see the error message send by the service.

Note: FaultException is the preferred way of sending exception from Service to client. Because, you can send exception with some very specific type. To send FaultException, you need to create FaultContract. T is nothing by the typeof FaultContract.

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...