Thursday, 12 July 2012

WCF-Lesson 1

This is the basic application to interact with wcf service.
for any query you can mail me at
you can also mail me your wcf problem which you face in your project

Basic Application with the help of WCF
Wcf stands for windows communication foundation. It is introduced in .net framework 3.0.
Wcf is network-distributed service oriented technology. It is combination Web Service, Remoting, MSMQ and Com+.
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.

Note:-all endpoints mentioned in the web.config file.
Following is the basic example of wcf service. This is example is made to only interact with you and describe you what the wcf service is
In wcf service we have to 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 Servi ce)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:-

1.       Open visual studio 2010. Go to the file->New->Website.
Following window will be opened.
Select wcf service form this window.

2.       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

3.       As we deleted inbuilt interface and class and created our own interface Itechnology and try to add [ServiceContract] but its not showing becoz we didn’t use the namespace System.ServiceModal.

4.       In this step we will add the system.ServiceModal namespace and then add the [ServiceContract]

5.       In this step we add the method which takes the 2 parameters and returns its sum. We also add class name TechnologyImplementation.

Itechnology Interface

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

public interface Itechnology

       int sum(int x, int y);


TechnologyImplementation.cs Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for TechnologyImplementation
/// </summary>
public class TechnologyImplementation: Itechnology
    public int sum(int x, int y)

        return x + y;

6.       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 simle binding without any security and will run on http protocol.

<?xml version="1.0"?>
              <compilation debug="true" targetFramework="4.0"/>



      <service name="TechnologyImplementation">

        <endpoint address="" contract="Itechnology" binding="basicHttpBinding"></endpoint>
                                  <!-- 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"/>
              <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
              <modules runAllManagedModulesForAllRequests="true"/>

7.       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" %>


8.       Copy this url and create new website where we will call this service. And click on the add service reference.


9.       Paste this url here and click on the go Button and after that click on ok to add the service
10.   Create web page

11.   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();

12.   And finally run the application

In this way this is the basic application to interact with wcf service.

No comments:

Post a Comment