WCF Data Service Service Operation Error Handling RRS feed

  • Question

  • Hi folks,

    I have created a WCF data service with a service operation.

    I want to be generate a kind of Business exception. I try to generate WebFaultException but I don't see how to catch this error at the client side when this error is throwing by a service operation.


    Here is my service operation to simulate an exception:






    public void GenerateException()




    throw new DataServiceException(403,"Custom Message" );



    Here is my client:



    WebClient wc = new WebClient();



    new Uri("http://localhost:27820/WcfDataService1.svc/GenerateException"));

    DownloadString is throwing an exception but it's onyl "Interal Server Error" I can't see my "Custom Message".


    Any Idea ?


    Many Thanks.



    Tuesday, March 1, 2011 5:38 PM

All replies

  • I created a similar error-raising service operation, and it seems that what is happening (at least on my machine) is that the DataServiceException that is raised in the service operation is getting wrapped by a TargetInvocationException (probably because of how the operation is called). However, this messes up how the data service returns the exception--and your nice message is being hidden in an inner exception which is not included in the response. You need to override the DataService<T>.HandleException method to get the TargetInvocationException and unpack the DataServiceException to return it directly.

    I've put up a blog post with details on how to do this.



    Glenn Gailey

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Sunday, March 6, 2011 9:37 AM