ABC of WCF

Pavan_Kumar
Posted by in WCF category on for Beginner level | Points: 250 | Views : 19505 red flag
Rating: 4 out of 5  
 4 vote(s)

In this article we will revisit the ABCs of WCF and see some practical scenario.

Introduction

WCF stands for Windows Communication Foundation. Basically WCF is a platform for building, configuring and deploying distributed services. In fact we can say wcf is a service layer that will allows us to create and communicate using verities of ways.

Objective


To understand about the ABC's of WCF.


Description

Before WCF: Previously we used to use web services. The major problem with web service is that only http binding is allowed with it (No interoperability), exception handling mechanism was not robust and few more disadvantages.Because of all these drawbacks WCF took birth and came into picture by overcoming these drawbacks. Therefore we can say WCF as next generation of web services.

At the highest level, we can say WCF for the following:

  • Web services
  • .NET to .NET communication (via HTTP, TCP)
  • Distributed transactions
  • WS  specifications
  • Queued messaging

Now let’s try to understand what are all the building blocks of wcf .ABC is the three building blocks of WCF.Let’s say you are travelling to India for your project work. You are going to land in India using air-lines. Let’s analyze more

We are going to India-Address

We are going via air-lines-Transport medium

We are going for project work- Implementation

Similarly for WCF

            A - Address (Where): is the location where the service will be hosted(India is the address for your project).
            B - Bindings (How)    : is the mode of transmission (airline is the mode of transport).       

           C - Contacts (What): is an agreement between two parties

            (you are paying money for air-lines to drop you at India and there you are going do your project).

Now let’s dig more about ABC of wcf.

 <endpoint address="http://localhost/WCFDataService/Service1.svc"

            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_DataService1"

            contract="ServiceReference1.DataService1" name="BasicHttpBinding_DataService1" />

In the above snippet you can see address, binding, and contract.

Address is nothing but a location of the service. It would be like http://ServerName/ServiceName. This location will be used to communicate with clients.

Binding are the communication channels between wcf service and client (http, TCP etc)

Below list shows the default binding that comes with WCF.

·              BasicHttpBinding

·         WSHttpBinding

·         WSDualHttpBinding

·         WSFederationHttpBinding

·         NetTcpBinding

·         NetNamedPipeBinding

·         NetMsmqBinding

·         NetPeerTcpBinding

Based on your needs you can select any of the binding modes.

Let’s move to contract. You are probably thinking, “Oh great, contracts…another new term to use!!!” Well, not exactly.

Contract –This is the heart of our service. It defines the operations that a service a perform. Usually contract will be implemented as an Interface decorated by [ServiceContract] attribute.

Types of contracts:

  •     Service Contract
  •     Operation Contract
  •     Data Contract 

Service Contract: It is an interface or class defines the service contract in Windows Communication Foundation  (WCF) application.

         The Service Contract defined as follows:

      [ServiceContract]

         public interface DataService1

     {

          // OperationContact

        Define the OperationContact here….

     }

          Operation ContractOperation contract is defines the methods of the service can be accessible by the  external systems. Operation Contract attributes needs to apply for all these methods, these are like web methods in   web service. The operation contracts are defined as follows

    [ServiceContract]

    public interface DataService1

    {

        [OperationContract]

        List<Ticket> GetDataUsingDataContract();

 

        [OperationContract]

        List<Ticket2> GetDataUsingDataContract2(string Key, string Stat, string Prio, string App);

        [OperationContract]

        Dictionary<string,string> GetTotalOpenPending();

        [OperationContract]

        Dictionary<string, string> GetLastMonthsTickets();
    }

 

Data Contract: Data Contract attribute to the class to serialize the class by a serializer and apply Data Member attribute to the fields in the class that must be serialized.

  [DataContract]

    [Serializable]

    public class MemYearRep

    {

        Define the Datamembers here….

    }

Data Members: Data member specifies the type which is part of a Data Contract used as composite type members of the contract. To define a Data Member apply Data Member attribute to the fields that must be serialized. Data Member attribute can be applied to private properties, but it will be serialized and desterilized and can be accessible to the user or process. The code below shows how to define Data Member in a Data Contract.

  [DataContract]

    [Serializable]

    public class MemYearRep

    {

        [DataMember]

        public string FullName { get; set; }

        [DataMember]

        public int Year { get; set; }

        [DataMember]

        public int Count { get; set; }

    }

Let’s take a look all these contracts into one example 

 

[ServiceContract]

    public interface DataService1

    {

 

        [OperationContract]

        Members GetMemberDetails(int MemberID);

    }

[DataContract]

    [Serializable]

    public class Members

    {

        [DataMember]

        public int MemId { set; get; }

        [DataMember]

        public string MemName { set; get; }

        [DataMember]

        public string Password { set; get; }

        [DataMember]

        public string MemMailId { set; get; }

        [DataMember]

        public bool IsManager { set; get; }

        [DataMember]

        public string Team { set; get; }

        [DataMember]

        public bool IsNew { set; get; }

    }

Now, note that in the contract I showed above, I am using a return type called " Members ".

The  " Members " is a class I wrote, that represents a business object which lets me conveniently hold the details of the data I am accepting or sending.  

 Address, Binding and Contracts together constitutes an end point.

        <endpoint address="http://ServerName/ServiceName" binding="wsHttpBinding" contract="WCFDataService.IService2">

 

Conclusion


We understood about ABC's of WCF. In the next article I will try to explain exception handling and types of hosting in wcf. Please provide feedback below.

Reference


MSDN

Page copy protected against web site content infringement by Copyscape

About the Author

Pavan_Kumar
Full Name: Pavan Kumar
Member Level: Starter
Member Status: Member
Member Since: 2/6/2012 7:41:05 AM
Country: India


From past two year working with CGI in Dot net technology.

Login to vote for this post.

Comments or Responses

Posted by: Shantanupatel on: 3/14/2012 | Points: 25
Hi Pavan,
Nice Approach!

Posted by: Pavan_Kumar on: 3/14/2012 | Points: 25
Thanks Shantanu.
Posted by: Savariya on: 3/22/2012 | Points: 25
Good article

ThANKS
Posted by: Pavan_Kumar on: 3/22/2012 | Points: 25
Thanks Savariya.
Posted by: Jmckamal on: 3/27/2012 | Points: 25
Good explanation
Posted by: Pavan_Kumar on: 3/28/2012 | Points: 25
Thank you !!

Login to post response

Comment using Facebook(Author doesn't get notification)