Written By:- Isha Malhotra
Wcf stands for windows communication foundation. It is introduced in .net framework 3.0.
Wcf is network-distributed service oriented technology. It is used to create the distributed architecture.
It is the single platform where we can create a single service which will be interact with different client having different protocols by adding endpoints.
System.ServiceModal is namespace for wcf service.
Wcf consists of many endpoints. Following is the component of Endpoint.
->Address: - it describes where the service resides in the network.
->Binding:-it describes how the service interacts with application.
->Contracts:-it describes what service will do.
Following is the example to show how to create wcf service with HTTP protocol. In wcf service we work with Interface and Class. In interface we declare the method which will be implemented in the class.
Every Wcf Service should have at least one Service Contract (as contract defines what the service will do). It defines using [ServiceContract] (same like [WebService] in Web Service)and [OperationContract](same like [WebMethod] in Web Service) Attribute.
Following steps describe the wcf service. By using this steps you can create the basic application for wcf:-
Open visual studio 2010. Go to the file->New->Website. Following window will be opened. Select wcf service forms this window.
As we can see one interface IService and one Class Service.cs. When we open the wcf service all configuration made with this interface and class. If we will work with the same interface and service we need not to change any configuration. But if we want to make our own interface and class we have to make some changes in configuration file.
As we deleted inbuilt interface and class and created our own interface Itechnology and try to add [ServiceContract] but it's not showing because we didn't use the namespace System.ServiceModal.
In this step we will add the system.ServiceModal namespace and then add the [ServiceContract]
. In this step we add the method which takes the 2 parameters and returns its sum. We also add class name Technology Implementation.
Itechnology Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
[ServiceContract]
public interface Itechnology
{
[OperationContract]
int sum(int x, int y);
}
TechnologyImplementation.cs Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class TechnologyImplementation: Itechnology
{
public int sum(int x, int y)
{
return x + y;
}
}
Make changes in Web.Config file.
a) In <system.serviceModel> tag include new tag <services>
b) Add service name. this name should be the name which is the name of your class.
c) Add your endpoint in <services> tag.
d) Leave the address blank as this is the basic application and running on local host so no need to give the address. We will learn this attribute in next lesson.
e) Add the contract name. Contract name should be the Interface name which you added.
f) Give the binding name basicHttpBinding as its simple binding without any security and will run on http protocol.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<services>
<service name="TechnologyImplementation">
<endpoint address="" contract="Itechnology" binding="basicHttpBinding"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Make changes in Service.svc file.
a) Give the service name same the name of your class.
b) Add class name in codebehind.
c) And after that run the application
<%@ ServiceHost Language="C#" Debug="true" Service="TechnologyImplementation" CodeBehind="~/App_Code/TechnologyImplementation.cs" %>
Copy this url and create new website where we will call this service. And click on the add service reference.
Paste this url here and click on the go Button and after that click on ok to add the service
Create web page
Do following coding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(TextBox1.Text);
int y = Convert.ToInt32(TextBox2.Text);
//create the object of class declared in wcf
ServiceReference1.ItechnologyClient ic = new ServiceReference1.ItechnologyClient();
//call the method which defined in the wcf service
int z = ic.sum(x, y);
Label1.Text = z.ToString();
}
}
And finally run the application In this way this is the basic application to interact with wcf service.