Friday 3 August 2012

What is WCF?.

Introduction:-

Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. WCF provides a runtime environment for services, enabling you to expose CLR types as services, and to consume other services as CLR types.

WCF is part of .NET 3.0 and requires .NET 2.0, so it can only run on systems that support it.

A WCF service is made up of three parts: the service, one or more endpoints and a hosting environment.
A service is basically a class written in a .Net compliant language which contains some methods that are exposed through the WCF service. A service may have one or more endpoints – an endpoint is responsible for communication from the service to the client.
Endpoints also have three parts which are known as ‘ABC’: ‘A’ for Address, ‘B’ for Binding and ‘C’ for Contracts.
Address: Address specifies the info about where to find the service.
Binding: Binding specifies the info for how to interact with the service.
Contracts: Contracts specifies the info for how the service is implemented and what it offers.

Example:

Example for VS 2010:
open vs2010 >open new project> select wcf using c#> give the name of the project> click ok.
After that your will open.Now write some method.
For example you want to check the user name exist or not in the database.
First create a table registration which have two column user_id ,and user_name.
Fill the two column data.

when u open first time your service looks like below:
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;
// 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
{
 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;
 }
    
}
  
 after that you add method to retrieve data from database:
//this is used to get userid from database
    public DataSet GetUserId()
    {
        SqlConnection con = new SqlConnection("Data Source =SUSHIL-70F1F267; 
                Initial Catalog =sushil; Integrated Security=true");

        string cmd = "select user_name from registrations";

        SqlDataAdapter da = new SqlDataAdapter(cmd, con);

        con.Open();

        DataSet ds = new DataSet();

        da.Fill(ds);

        con.Close();

        return ds;
    }
 
Finally your Service.cs looks like:
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;
// 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
{
 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;
 }
    //this is used to get userid from database
    public DataSet GetUserId()
    {
        SqlConnection con = new SqlConnection("Data Source =SUSHIL-70F1F267; 
       Initial Catalog =sushil; Integrated Security=true");

        string cmd = "select user_name from registrations";

        SqlDataAdapter da = new SqlDataAdapter(cmd, con);

        con.Open();

        DataSet ds = new DataSet();

        da.Fill(ds);

        con.Close();

        return ds;
    }
}
 
Now add one web form in this project.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div><b>Enter User Name</b>
        <asp:TextBox ID="textUserId" runat="server"></asp:TextBox>
        <asp:Button ID="getUserId" runat="server" OnClick="getUserId_Click" 
          Text="Verify" />
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    Service wcfService = new Service();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void  getUserId_Click(object sender, EventArgs e)
   {
         DataSet ds = new DataSet();
        ds = (wcfService.GetUserId());
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string UserId = row["user_name"].ToString();
                if (UserId == textUserId.Text)
                {
                    lblMessage.Text = "Valid";
                    break;

                }
                else
                    lblMessage.Text = "InValid";
            }
        }
    }
    
}


Run the project and test it . If any problem regarding WCF you can ask a 
question or reply me on sushil.kumar86@outlook.com.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.