WCF How To Create and Consume Web Service using SQL Server Database


MVC 4 Gridview To Display Data Using SQL Server Database With Simple code "color: #b45f06;">tyle="text-align: left;">
WCF using Login TimeWeb Service How to Create And use using Win

"text-align: left;">

Introduction :

This Artical Explain How to create WCF Service and How to consume it using the windows form application it Can be also use in

web application Project But Here Describe with Windows Form Application.

Step 1 : Create  WCF Service Application :
















Step 2 :   Add Windows Form Application















Step 3: Rename the service Imaths.cs  and  maths.svc 
Step 4:  In Imaths.cs we can Create Service Interface for the use of webservice and Implement it in maths.svc file

Step 5: Create the Interface Class  Imaths.cs













using  System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace DemoWCF
{
   
    [ServiceContract]

    public interface Imaths
    {
        [OperationContract]
        Int32 Add(Int32 a,Int32 b);
        [OperationContract]
        Int32 Sub(Int32 a, Int32 b);
        [OperationContract]
        Int32 Mul(Int32 a, Int32 b);
        [OperationContract]
        Int32 Div(Int32 a, Int32 b);
    }
}
 Step 6 : And Implement the interface in    maths.svc 
  
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace DemoWCF
{
   
    public class maths : Imaths
    {
       public Int32 Add(Int32 a,Int32 b)
        {
            return a + b;

        }
       public Int32 Sub(Int32 a, Int32 b)
       {
           return a - b;

       }
       public Int32 Mul(Int32 a, Int32 b)
       {
           return a * b;

       }
       public Int32 Div(Int32 a, Int32 b)
       {
           return a / b;

       }
    }
Step 7 :  Now In WindowsFormsApplication1 we can Create Service Reference  by Right Click on References and Add Service Reference this service reference can be used for calling the service from the client .
Now, Create Windows Form With Three textbox and One ComboBox,and add Item in ComboBox In Following Image  for Select  Addition,Substraction, Multiplication and Division and Operation Are Display in Result Textbox


















Step 8 : Now We can use Reference As ServiceReference1.ImathsClient C = new ImathsClient()
in Form.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.ServiceReference1;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
       ServiceReference1.ImathsClient C = new ImathsClient();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Int32 x = Convert.ToInt32(txtfirst.Text);
            Int32 y = Convert.ToInt32(txtsecond.Text);
            if (comboBox1.Text == "Add")
            {
                txtresult.Text = C.Add(x, y).ToString();
            }
            else  if (comboBox1.Text == "Sub")
            {
                txtresult.Text = C.Sub(x, y).ToString();
            }
            else  if (comboBox1.Text == "Mul")
            {
                txtresult.Text = C.Mul(x, y).ToString();
            }
            else  if (comboBox1.Text == "Div")
            {
                txtresult.Text = C.Div(x, y).ToString();
               }
    }
}

Step 9: Run Program 





Previous
Next Post »