Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 9961 |  Welcome, Guest!   Register  Login
Home > Articles > WCF > ABC of WCF

ABC of WCF

4 vote(s)
Rating: 4 out of 5
Article posted by Pavan_Kumar on 3/13/2012 | Views: 5778 | Category: WCF | Level: Beginner | Points: 250 red flag

Advertisements

Advertisements
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

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Pavan_Kumar

About Pavan Kumar

Experience:2 year(s)
Home page:
Member since:Monday, February 06, 2012
Level:Starter
Status: [Member]
Biography:From past two year working with CGI in Dot net technology.
 Responses
Posted by: Shantanupatel | Posted on: 14 Mar 2012 01:42:07 AM | Points: 25

Hi Pavan,
Nice Approach!


Posted by: Pavan_Kumar | Posted on: 14 Mar 2012 02:12:53 AM | Points: 25

Thanks Shantanu.

Posted by: Savariya | Posted on: 22 Mar 2012 03:20:03 AM | Points: 25

Good article

ThANKS

Posted by: Pavan_Kumar | Posted on: 22 Mar 2012 03:39:31 AM | Points: 25

Thanks Savariya.

Posted by: Jmckamal | Posted on: 27 Mar 2012 03:56:32 PM | Points: 25

Good explanation

Posted by: Pavan_Kumar | Posted on: 28 Mar 2012 06:09:46 AM | Points: 25

Thank you !!

>> Write Response - Respond to this post and get points
Related Posts

Many times we would like to monitor events of WCF application in production environment. We would like to monitor events like errors, security audits, performance etc. This can be achieved by extending the ASP.NET health monitoring system in WCF. The health monitoring system is also termed as instrumentation.

To Create and Host WCF Application in IIS Server.

In this article we will discuss how we can enable certificates on WCF service. WCF has two modes by which it transfers data one is the transport and the other is the message. This tutorial will concentrate on how we can enable certificates on message mode of data transfer.

In this article we will start with transport and message security understanding. We will then see simple code samples of how to implement transport and message security using WsHTTP bindings. We will also see what is the difference between ‘BasicHttpBinding’ and ‘WsHttpBinding’ with the help of a simple source code. WCF security is a huge topic by itself, but we are sure with this article you will get a quick start of how to go about WCF security.

WCF stands for Windows Communication Foundation, is the latest technology that enables communication between applications in the distributed environment. Lets learn how to create , host , consume a WCF service in a series of articles

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 3:25:46 PM