Sending objects containing objects from WCF Server

Posted on 2009-12-23
Last Modified: 2013-12-17

I have a WCF Server that returns a ResponseType class object that i have created. The class is shown in the code section.

When the WCF Client  invokes a WCF Server Function and the   server operation is a success
(for example  WCF server succesfully connects to the database)  ResponseObj will be  some
string data. In this scenario everything is working fine. (r_type.ResponseObj="Connection is OK")

But  when   a exception occurs at the WCF server  i catch that exception and want to inform the client about the exception. And i want to directly send the exception object to the WCF client. So i use something like this:
catch(Exception ex)
return r_type;

But  at the WCF client i get the exception : Server is in the faulted state.

In short  when the object type ResponseType is a complex object like Exception class object
instead of int or string, the system does not work.
Is there a workaround to overcome this issue ?

Note : If i add  another DataMember  like Exception ex to the ResponseType Class then i can
succesfully send the exception to the client. But I do not want to add a DataMember  for
each type of exception. By using reflection at the client (.GetType())  i can already understand
the type of the object.

I am sorry and i hope i have made myself clear.


 public class ResponseType


        private object responseObj;    

        private string additionalData;


        public object ResponseObj


            get { return responseObj; }

            set { responseObj = value; }



        public string AdditionalData


            get { return additionalData; }

            set { additionalData = value; }



Open in new window

Question by:parabellum
    LVL 42

    Expert Comment


    1. Add one more class as follows:

        public class MyFaultException
            private string _reason;
            public string Reason
                get { return _reason; }
                set { _reason = value; }

    add FaultContract atteibute to your service contract interface which exposes API:

        public interface IService
            ResponseType YourFunction();

    add exception handling to your wcf service class:
    public class Service : IService
             public string YourFunction()
    //code here
                catch (Exception exp)
                    MyFaultException theFault = new MyFaultException();
                    theFault.Reason = "Some Error " + exp.Message.ToString();
                    throw new FaultException<MyFaultException>(theFault);

                return "No Error";

    now, u can catch your fault exception to get a more details of the exception:

     static void Main(string[] args)


                string result = "";
    ServiceProxy.ServiceClient client = new ServiceProxy.ServiceClient();

                    result = client.YourFunction();


                catch (FaultException<ServiceProxy.MyFaultException> ee)


                   result = "Exception: " + ee.Detail.Reason;

    LVL 42

    Accepted Solution

    u need to modify your web.config:
    <?xml version="1.0" encoding="utf-8" ?>




            <behavior name="MyServiceTypeBehaviors">

                  <serviceDebug includeExceptionDetailInFaults="true" /> -->THIS ONE !!!!





    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now