This article provides an example using WCF. After going through this snippet, the reader will have a clear and basic understanding of the wsHttpBinding and how to consume it in a web application.
- Visual Studio 2010
- SQL Server 2008 SSMS
Step 1
Open Visual Studio 2010 and add a new project named PincodeService. Then add a WCF Service file named DataService.svc as in the following:
Step 2
In that, you will see [ServiceContract]. Below this your interface name is declared. Thereafter you'll see [OperationContract]; your function contract should be defined here.
Step 3
Implementation of interface methods.
DataService.cs contains the following code.
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
-
- namespace PincodeService
- {
-
- public class DataService : IDataService
- {
- List<PinCode> IDataService.getPincodeDetails(string pinCode)
- {
- List<PinCode> lstPinCode = new List<PinCode>();
- DataTable dsData = new DataTable();
- dsData = getPincode(pinCode);
-
- if (dsData != null)
- {
- foreach (DataRow row in dsData.Rows)
- {
- PinCode objPincode = new PinCode();
- objPincode.Pincode = Convert.ToString(row["Pincode"]);
- objPincode.PostOfficeName = Convert.ToString(row["Post_Office_Name"]);
- objPincode.DistrictName = Convert.ToString(row["Districts_Name"]);
- objPincode.CityName = Convert.ToString(row["City_Name"]);
- objPincode.State = Convert.ToString(row["State"]);
- lstPinCode.Add(objPincode);
- }
- }
-
- return lstPinCode.ToList();
- }
-
-
- #region " [ Private Function ] "
- private DataTable getPincode(string pinCode)
- {
- DataSet dsData = new DataSet();
- SqlConnection sqlCon = null;
- SqlDataAdapter sqlCmd = null;
-
- try
- {
- using (sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))
- {
- sqlCmd = new SqlDataAdapter("USP_PINCODECRUD", sqlCon);
- sqlCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
- sqlCmd.SelectCommand.Parameters.AddWithValue("@PINCODE", pinCode);
-
- sqlCon.Open();
- sqlCmd.Fill(dsData);
-
- sqlCon.Close();
- }
- }
- catch
- {
- throw;
- }
- return dsData.Tables[0];
- }
- #endregion
- }
- }
Step 4
The following describes the DataTable and Stored Procedure used.
The following is the database script included in the download:
- CREATE PROCEDURE [USP_PINCODECRUD]
- @PINCODE VARCHAR(6)
- AS
- BEGIN
- SELECT Pincode, Post_Office_Name, Districts_Name, City_Name, [State]
- FROM tbl_PincodeMaster
- WHERE Pincode LIKE '%' + @PICODE + '%'
- END
- GO
Step 5
Change the web config in the system service model as in the following:
- <?xml version="1.0"?>
- <configuration>
-
- <system.web>
- <compilation debug="true" targetFramework="4.0" />
- </system.web>
-
- <connectionStrings >
- <add name="connectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
- </connectionStrings>
-
- <system.serviceModel>
-
- <services>
-
- <service name="PincodeService.DataService" >
- <endpoint address="PincodeService" binding="wsHttpBinding" contract="PincodeService.IDataService"/>
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
- <host>
- <baseAddresses >
- <add baseAddress="http://localhost:8080/"/>
- </baseAddresses>
- </host>
- </service>
- </services>
-
- <behaviors>
- <serviceBehaviors>
- <behavior>
- <serviceMetadata httpGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
- </system.serviceModel>
-
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- </system.webServer>
- </configuration>
Step 6
Compile and run the WCF application.
Step 7
Consuming the Service and using it in the web application.
Open another Visual Studio 2010 instance and add an empty web project as WcfClientUI.
The Service will be consumed and used in this application.
Right-click on the project and service reference and provide the address of the service. Add the discovered service to the project.
Step 8
I have added one aspx web page.
The following is the Service proxy created and used to access the exposed methods.
- #region " [ namespaces ] "
- using System;
- using System.Collections.Generic;
- using System.Linq;
- #endregion
-
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
-
- #region " [ Button Events ] "
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- PincodeService.DataServiceClient objService = new PincodeService.DataServiceClient();
- List<PincodeService.PinCode> lstPincode = new List<PincodeService.PinCode>();
-
- lstPincode = objService.getPincodeDetails(txtPincode.Text.Trim()).ToList();
-
- gvData.DataSource = lstPincode;
- gvData.DataBind();
-
- }
- #endregion
- }
- <%@ 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>Pincode Client</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table width="40%">
- <tr>
- <td colspan="3" align="center">
- <asp:Label ID="lblMsg" runat="server" ForeColor="Red"></asp:Label>
- </td>
- </tr>
- <tr>
- <td>
- Pincode
- </td>
- <td>
- <asp:TextBox ID="txtPincode" runat="server" MaxLength="6" AutoCompleteType="Disabled"></asp:TextBox>
- </td>
- <td align="left">
- <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
- </td>
- </tr>
- <tr>
- <td>
- <br /> <br /> <br />
- </td>
- </tr>
- <tr>
- <td colspan="3">
- <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="true" EmptyDataText="No data found."
- EmptyDataRowStyle-ForeColor="Red">
- </asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Step 9
Compile and run the client application.