Navigation

Sunday 17 March 2013

WCF Serviece : How to create and consume in C#

STEP 1: Create a New WCF Service in your Visual Studio

               =>File =>New => Web Site => WCF Service

STEP 2:Write these code in your interface file(IService.cs)



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

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name //"IService" in both code and config file together.
[ServiceContract]
public interface IService
{
    [OperationContract]
    [FaultContract(typeof(MyException))]
    string search(int x);
}

[DataContract]
public class MyException
{
    private string Reason;

    [DataMember]
    public string MyReason
    {
        get
        {
            return Reason;
        }
        set
        {
            Reason = value;
        }
    }
}


STEP 3:Write these code on your Class file(Service.cs)



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
{
    string r = null;
    public string search(int rno)
    {
        try
        {
            SqlConnection con = new SqlConnection("Data Source=VCS;Initial Catalog=COLLEGITES_22_FEB;User ID=sa;Password=vcs@1234;  integrated security=yes");
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT COUNTRY_NAME  FROM MASTER_COUNTRY WHERE SNO=" + rno + ";", con);
            SqlDataReader rd = cmd.ExecuteReader();
            bool b = rd.Read();
            if (b == false)
            {

            }
            else
            {
                r = rd["COUNTRY_NAME"].ToString();

            }
            return r;
        }
        catch (Exception r)
        {
            MyException m = new MyException();
            m.MyReason = "Some internal prob. in database server";
            throw new FaultException<MyException>(m);
         
        }
    }
}

STEP 4: Run your WCF Service that you Created

    Press Run Button on your toolbar and Copy Url  that u Get
    Example: http://localhost:49200/WCFService1/Service.svc

STEP 5: Create  a new website in your Visual Studio(Website1)

 =>File =>New => Web Site => ASP.NET WebSite
this website is client that use WCF Serviece by adding Service Reference in your website


STEP 6: "Add Service Reference" 

By Right Clicking on your Solution Explorer Or Click "WebSite" on Menu Bar
when Add Service Reference Window Open ,Past URL of  your WCF Service
on Address : http://localhost:49200/WCFService1/Service.svc 
and Press "Go" button  and then "OK" button

STEP 7:Write these code on  your "Default.aspx" Page

using System.ServiceModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            int r = Convert.ToInt32(TextBox1.Text);
            ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
            string s1 = s.search(r);
            if (s1 == null)
            {

                Label1.Text = "Record Not Found";

            }
            else
            {
                Label1.Text = s1;
            }
        }
        catch (FaultException<ServiceReference1.MyException> m)
        {

            Label1.Text = m.Detail.MyReason;

        }
    }
}


STEP 8: Run your page Default.aspx Page and check your Code




No comments:

Post a Comment