Link to home
Start Free TrialLog in
Avatar of patd1
patd1Flag for United States of America

asked on

WCF question about sending result in xml format

I am new learning to create WCF web services. I created  this sample WCF service by taking help from different blogs online, it is working fine,  but I am not sure if it sends results in xml format or not. Please guide if I need to do something to send result in xml format.

Thanks.

GreetingService.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;


namespace Wcf.Samples.ServiceLibrary
{
    [DataContract]
    public class MyFaultException
    {
        private string _reason;
        [DataMember]
        public string Reason
        {
            get { return _reason; }
            set { _reason = value; }
        }
    }

    public class GreetingService : IGreeting
    {
        #region IGreeting Members

        public string Greet(string name)
        {
            return "Hello " + name;
        }

        public string Add(string num1, string num2)
        {
            int Result;
            try
            {
                int Number1 = Convert.ToInt32(num1);
                int Number2 = Convert.ToInt32(num2);
                Result = Number1 + Number2;               
            }
            catch (Exception exp)
            {
                MyFaultException theFault = new MyFaultException();
                theFault.Reason = "Error Occured: " + exp.Message.ToString();
                throw new FaultException<MyFaultException>(theFault);
            }
            return Result.ToString();
        }
        #endregion
    }
}

Open in new window


IGreeting.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;



namespace Wcf.Samples.ServiceLibrary
{
    [ServiceContract]   
    public interface IGreeting
    {
        [OperationContract]
        [FaultContract(typeof(MyFaultException))]
        string Greet(string name);

        [OperationContract]
        [FaultContract(typeof(MyFaultException))]
        string Add(string num1, string num2);
    }

}

Open in new window


Service.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Wcf.Samples.ServiceLibrary.GreetingService" %>

Open in new window


Code on the client that invokes the service:
 protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        GreetingClient client = new GreetingClient("WSHttpBinding_IGreeting");
        try
        {
            TextBoxResult.Text = client.Add(TextBoxNum1.Text, TextBoxNum2.Text);            
        }
        catch (FaultException<MyFaultException> ee) {TextBoxFault.Text = ee.Detail.Reason;}
        catch (InvalidOperationException ie) { TextBoxFault.Text = ie.Message; }
        catch (TimeoutException te) {TextBoxFault.Text = te.Message;}
        catch (FaultException fEx) {TextBoxFault.Text = fEx.Message;}
        catch (CommunicationException ce) { TextBoxFault.Text = ce.Message; }
        finally
        {
            client.Close();
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial