[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sending objects containing objects from WCF Server

Posted on 2009-12-23
2
Medium Priority
?
445 Views
Last Modified: 2013-12-17

Hello,
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)
{
r_type.Responsebj=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.
thanks






[DataContract]
 public class ResponseType
    {
        private object responseObj;    
        private string additionalData;

        [DataMember]
        public object ResponseObj
        {
            get { return responseObj; }
            set { responseObj = value; }
        }
        [DataMember]
        public string AdditionalData
        {
            get { return additionalData; }
            set { additionalData = value; }
        }
}

Open in new window

0
Comment
Question by:parabellum
  • 2
2 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 26112542

1. Add one more class as follows:


    [DataContract]
    public class MyFaultException
    {
        private string _reason;
 
       [DataMember]
        public string Reason
        {
            get { return _reason; }
            set { _reason = value; }
        }
    }


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

  [ServiceContract()]
    public interface IService
    {
        [OperationContract]
        [FaultContract(typeof(MyFaultException))]
        ResponseType YourFunction();
    }

add exception handling to your wcf service class:
public class Service : IService
    {
         public string YourFunction()
        {
            try
            {
//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 = "";
            try
            {
ServiceProxy.ServiceClient client = new ServiceProxy.ServiceClient();
                ();

                result = client.YourFunction();

            }

            catch (FaultException<ServiceProxy.MyFaultException> ee)

            {

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

            }
0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 26112561
u need to modify your web.config:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <system.serviceModel>
   
    <behaviors>

      <serviceBehaviors>

        <behavior name="MyServiceTypeBehaviors">

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

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>
</configuration>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

873 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