WCF-Windows Comunication Foundation- windows Communication foundation is based on message based system.
here client communicate with services through message exchange .
wcf follows soap Architecture and soap is based on messaging
,in wcf all communication will be in xml form exchange ,
the main diffrence between webservices and wcf are
web service- 1.this will communicate only in soap format using http.request.
2. web services uses webservice attribute and uses webmethod attribute
wcf- 1.while wcf can communicate in both ways in soap using http request as well as in Binary Format using Tcp .
2.while wcf uses service contract attribute in place of web service attribute and datacontract attribute in place of webmethod attribute.
3. wcf uses Endpoint To communicate with services. EndPoint is a place where client and services can communicate using messaging exchange system.
Wcf having 5 contract objects
1. service contract
2. data contract
3. message contract
4.Fault Contract
5.operational Contract
details about these contracts i will give you by giving example.
Wcf using Ado.net
Iservice.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
// NOTE: If you change the interface name "IService" here, you must also update the reference to "IService" in Web.config.
[ServiceContract]
public interface IService
{
[OperationContract]
DataSet getdata();
}
[DataContract]
class customer
{
[DataMember]
int x;
}
now comes to, default.aspx .cs,
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
GridView1.DataSource = s.getdata();
GridView1.DataBind();
}
}
now add reference of service and copy url then service will attached in your application
then output look like this
button1
Notes: in Wcf all mehods Reurn Something Except Void so this things We should kepp in Mind , generally this Question Asked By the interview.
now Another Example 2. Wcf Via Linq
open project chose wcf service application then we find in solution explorer here we find two file one is iterface and another is .svc file open interfacepages and following code
which i am going to implement below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.Linq.Mapping; // [ this should be required for linq for matching with database]
using System.Data.Linq; // [for linq datacontext]
namespace WcfService1
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
List<emp> mukesh_show();
}
[DataContract]
[Table(Name="Emp1")]
public class emp
{
[DataMember]
[Column(Name="id",IsPrimaryKey=true)]
public int id { get; set; }
[DataMember]
[Column(Name = "name")]
public string name { get; set; }
}
public class dal : DataContext
{
public dal(string x): base(x)
{
}
public Table<emp> t1;
}
}
continue ....
here client communicate with services through message exchange .
wcf follows soap Architecture and soap is based on messaging
,in wcf all communication will be in xml form exchange ,
the main diffrence between webservices and wcf are
web service- 1.this will communicate only in soap format using http.request.
2. web services uses webservice attribute and uses webmethod attribute
wcf- 1.while wcf can communicate in both ways in soap using http request as well as in Binary Format using Tcp .
2.while wcf uses service contract attribute in place of web service attribute and datacontract attribute in place of webmethod attribute.
3. wcf uses Endpoint To communicate with services. EndPoint is a place where client and services can communicate using messaging exchange system.
Wcf having 5 contract objects
1. service contract
2. data contract
3. message contract
4.Fault Contract
5.operational Contract
details about these contracts i will give you by giving example.
Wcf using Ado.net
Iservice.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
// NOTE: If you change the interface name "IService" here, you must also update the reference to "IService" in Web.config.
[ServiceContract]
public interface IService
{
[OperationContract]
DataSet getdata();
}
[DataContract]
class customer
{
[DataMember]
int x;
}
now comes on ,Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;
// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config and in the associated .svc file.
public class Service : IService
{
public DataSet getdata()
{
SqlConnection con = new SqlConnection("database=yogi;data source=FAMILY-A990589E\\SQLEXPRESS;integrated security=true");
SqlDataAdapter da;
da = new SqlDataAdapter("select* from emp", con);
DataSet ds = new DataSet();
//da.SelectCommand = cmd;
da.Fill(ds, "emp");
return ds;
}
}
now build the application then we get url and copy url, which is the End point where client and services communicate using Xml Exchange messaging System.
End point May me many ,This Is based on Request done by the Client.
one major things to know about wcf, Wcf uses Serialization Technique so , in Ado.net Only one class Is there Which follows
Serialization i.e, dataset Class,thats why we should not use datatable ,datareader and any other class. thats whys i am using
dataset in my example.
now open new website,take gridview and a button, so html code file looks like ,
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Empid" HeaderText="Empid" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
GridView1.DataSource = s.getdata();
GridView1.DataBind();
}
}
now add reference of service and copy url then service will attached in your application
then output look like this
| Empid | Name |
|---|---|
| 1100001 | Yogendra Singh |
| 1100002 | Satendra Singh |
| 1100004 | Satendra Singh |
| 1100005 | Deepak Kumar |
| 1100007 | Payal |
| Pratham | |
| Jyoti |
button1
Notes: in Wcf all mehods Reurn Something Except Void so this things We should kepp in Mind , generally this Question Asked By the interview.
now Another Example 2. Wcf Via Linq
open project chose wcf service application then we find in solution explorer here we find two file one is iterface and another is .svc file open interfacepages and following code
which i am going to implement below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.Linq.Mapping; // [ this should be required for linq for matching with database]
using System.Data.Linq; // [for linq datacontext]
namespace WcfService1
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
List<emp> mukesh_show();
}
[DataContract]
[Table(Name="Emp1")]
public class emp
{
[DataMember]
[Column(Name="id",IsPrimaryKey=true)]
public int id { get; set; }
[DataMember]
[Column(Name = "name")]
public string name { get; set; }
}
public class dal : DataContext
{
public dal(string x): base(x)
{
}
public Table<emp> t1;
}
}
now open .svc and do following code,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService1
{
public List<emp> mukesh_show()
{
dal d=new dal ("database=yogi;data source=FAMILY-A990589E\\SQLEXPRESS;integrated security=true");
IEnumerable<emp> E = d.t1;
return E.ToList();
}
}
}
now build the project ,and copy the url
it looks like this,
Service1 Service
You have created a service.
To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:
svcutil.exe http://localhost:1993/Service1.svc?wsdl
This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:
C#
class Test { static void Main() { Service1Client client = new Service1Client(); // Use the 'client' variable to call operations on the service. // Always close the client. client.Close(); } }
Visual Basic
Class Test Shared Sub Main() Dim client As Service1Client = New Service1Client() ' Use the 'client' variable to call operations on the service. ' Always close the client. client.Close() End Sub End Class
now open a website , and take a gridview and a button
html code would be like this,
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="Empid" HeaderText="Empid" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</form>
</body>
</html>
now comes to .cs file , and add reference or uin command prompt run this command
c:\\ svcutil.exe http://localhost:1993/Service1.svc?wsdl then proxy will generate which will generate two files one is .cs file and another is webconfig file in command promptand do the following code,
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client s = new ServiceReference1.Service1Client();
GridView1.DataSource = s.show_mukesh();
GridView1.DataBind();
}
now run the code and press button
then output look like this,
continue ....
No comments:
Post a Comment