Basic Concepts of WCF Service With simple Example.

Hiremath
Posted by in WCF category on for Beginner level | Points: 250 | Views : 165286 red flag
Rating: 4.57 out of 5  
 7 vote(s)

Hi friends, In this Article u r going to learn basic concepts of Windows communication Foundation with a Simple example of web application calculator.


 Download source code for Basic Concepts of WCF Service With simple Example.

Introduction

What is WCF?

WCF is a service Oriented programming in .net framework 3.0 which provide unique platform for all type of Communications in .net for distributed Applications.

It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF.

Basic Bare Bones of WCF

1>Endpoints (ABC terms).

>Address

>Binding

>Contract

All the WCF communications are take place through end point. End point consists of three components.(i.e ABC)

Address:

   Basically URL, specifies where this WCF service is hosted .Client will use this url to connect to the service.

         e.g http://localhost:8071/MyService/Calculator.svc.

Binding:

   Binding will describes how client will communicate with service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.

In My service I used Basic Http Service protocol for communication.i.e wsHttpBinding

Contract:

   Collection of operation that specifies what the endpoint will communicate with outside world. Usually name of the Interface will be mentioned in the Contract, so the client application will be aware of the operations which are exposed to the client. Each operation is a simple exchange pattern such as one-way, duplex and request/reply.

In My Service Contract is” Service1 “.

Exchange pattern is” request/reply”;

2> Service Contract: Service contracts describe the operation that service can provide

3>Operation Contract: Attribute Which is used to define the Method as part of Service.


Demo of the Calculator Example.

Step By Step Procedure

1>    Creating WCF Service

2>    Consuming WCF Service in Web Application using VS 2008.

3>    Creating WCF Client.

Creating WCF Service.

Steps:

1>    Open  VS 2008 > Projects > Select C#  >  WCF  > WCF Service Library.

2>    Type name of  the service file as u need. In this ex.MyWCFSolution. then click OK.

3>    New Window will open with one Class.cs and Interface .cs File which can be seen in Solution Explore.

4>    Add the Namespace System.ServiceModel and  also add  Reference in Solution Explore.

5>    Type the Code in Interface file and class file as shown below.

Code in Interface(EX: IService1.cs)

[ServiceContract]

    public interface IService1

    {

        [OperationContract]

       int add(int a,int b);

 

        [OperationContract]

       int Sub(int a, int b);

 

        [OperationContract]

        int Mul(int a, int b);

 

        [OperationContract]

        int Div(int a, int b);

 

        // TODO: Add your service operations here

    }

 

Code in Class file(EX: Service1.cs)

public class Service1 : IService1

    {

       

        int IService1.add(int a, int b)

        {

            return (a + b);

        }

 

        int IService1.Sub(int a, int b)

        {

            if (a >= b)

            {

                return (a - b);

            }

            else

            {

                return (b - a);

            }

        }

       

       

        public int Mul(int a, int b)

        {

            return (a * b);

        }

 

        public int Div(int a, int b)

        {

            if(a>=b)

                try { return (a / b); }

                catch

                { return 0; }

            else

                return (b / a);

        }

    }

Consuming WCF Service in Web Application.

Steps:

1>Create web Application as shown in attachment please download the File.

 

2> After Creating Desing of the Calculator web application the add reference System.ServiceModel namespace into web application in Solution Explore.

3>Add the WCF Service in visual studio by right click on Reference and select Add service Reference

4>Give the Service hosted Address and click on Go button and select the service which u created I mean in Ex service1.

5>Give the namespace: Name as Myservice and  the click OK .  

1>    Creating WCF Client.

In Order to call the Methods (services from WCFService) we need to Create the porxy WCF Client.

Once service is consumed in Application, WCF Client is Created, So Using the name of the Client Name we can call the methods in out applications

EX. In  Our WCF Service we have 4 methods (add, sub, mul, div).

2>      TO get the WCF client name Double click on Service i.e Myservice and take the copy of the Client name.

The fwindow will open in that u take the copy.

3>    Paste the Client Name in Default.cs File get the following code then create the instance of the client as show in below code. Using this porxy client u can call the services of the WCF service as shown in below code.

4>    On paste u get MY.Myservice.Service1Client

5>  Code to be Right in Default.cs in web application(EX:Calculator)

namespace Calculator

{

    public partial class _Default : System.Web.UI.Page

    {

       

        Calculator.Myservice.Service1Client Client = new Calculator.Myservice.Service1Client();

//Creating proxy client instance “Client”

        int a, b;

        protected void Page_Load(object sender, EventArgs e)

        {

           

            Response.Write("MY Calculator");

 

        }

 

        protected void btnAdd_Click(object sender, EventArgs e)

        {

a = Convert.ToInt32(txt1st.Text);

            b = Convert.ToInt32(txt2nd.Text);

 

            int Addition = Client.add(10,20);

            txtResult.Text = Addition.ToString();

        }

 

        protected void btnsub_Click(object sender, EventArgs e)

        {

            a = Convert.ToInt32(txt1st.Text);

            b = Convert.ToInt32(txt2nd.Text);

int Addition = Client.Sub(10, 20);

            txtResult.Text = Addition.ToString();

 

        }

 

        protected void btnmul_Click(object sender, EventArgs e)

        {  

a = Convert.ToInt32(txt1st.Text);

            b = Convert.ToInt32(txt2nd.Text);

            int Addition = Client.Mul(10, 20);

            txtResult.Text = Addition.ToString();

        }

 

        protected void btndiv_Click(object sender, EventArgs e)

        {

            a = Convert.ToInt32(txt1st.Text);

            b = Convert.ToInt32(txt2nd.Text);

int Addition = Client.Div(10, 20);

            txtResult.Text = Addition.ToString();

 

        }

    }

}

Testing the application and using WCF services.

1>  Run the WCF service First.(Ex:MyWCFsolution)

2>  Run the wev Application(EX: Calculator)(download the attachements)


Conclusion

Hope this article makes Sense of  basic Idea about WCF. Thank you.

Page copy protected against web site content infringement by Copyscape

About the Author

Hiremath
Full Name: hiremath V.K
Member Level: Starter
Member Status: Member
Member Since: 4/29/2011 3:36:46 AM
Country: India
Vappath Hiremth
http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Navalemanoj0405 on: 9/21/2011 | Points: 25
nice article for beginners ....
Posted by: Anilgenius on: 9/18/2012 | Points: 25
Very nice for article :)

Login to post response

Comment using Facebook(Author doesn't get notification)