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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

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

0
patd1
Asked:
patd1
1 Solution
 
Miguel OzSoftware EngineerCommented:
The interaction will depend on the host binding settings of your configuration file:
For example if you have BasicHttpBinding, then your interaction will be encoded as text/XML
Check:
http://wcftutorial.net/WCF-Types-of-Binding.aspx
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now