Step 1:  Creating a WCF service


♣    Open visual studio.net

♣    Select language as C# and select WCF service application template. Rename it ad “MYWCFAPP”

♣    Write the Below highlighted code with in IService1.cs

___________________________________________________________________________

namespace SimpleWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
        // TODO: Add your service operations here
       [OperationContract]
        int add(int a, int b);
        [OperationContract]
        int sub(int a, int b);
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
___________________________________________________________________________

♣    Write the below code with in Service1.svc.cs

___________________________________________________________________________

namespace SimpleWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        public int add(int a, int b)
        {
            return a + b;
        }
        public int sub(int a, int b)
        {
            return a - b;
        }
    }
}
___________________________________________________________________________

♣    Build the Solution and One WCF service is Created.

Step 2 : Creating the Client Application


♣    Open a ASP.net web application , rename it as WCFCLIENT

♣    Design webform1.aspx like below

wcf4


Step 3 : Adding WCF service reference to the Client application


♣    Open solution explorer of WCFclient , select WCFclient, right click, Select “Add Service Reference”. It will open Add service reference window.

♣    Here we have to go back to “MYWCFAPP” and we have to run the “MYWCFAPP”. With this process it will open WCF test client window, here copy the url of the WCF service an return back to WCF client and paste that url with in the address box and click "GO” and “OK” button.

♣    With this process WCF service reference will add to the WCF client solution explorer. That reference name is “service reference1”.

Step 4 : Consuming the WCF service form WCF client


♣ Write the below code with in Webform1.aspx

___________________________________________________________________________

namespace SimpleWCFClient
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        ServiceReference1.Service1Client objserv = new ServiceReference1.Service1Client();
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(TextBox1.Text);
            int y = Convert.ToInt32(TextBox2.Text);
            int addres = objserv.add(x, y);
            TextBox3.Text = addres.ToString();
        }
        protected void btnSubtract_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(TextBox1.Text);
            int y = Convert.ToInt32(TextBox2.Text);
            int subres = objserv.sub(x, y);
            TextBox3.Text = subres.ToString();
        }
    }
}
___________________________________________________________________________