Click here to Skip to main content
15,890,690 members
Articles / Web Development
Tip/Trick

Introduction to WCF

Rate me:
Please Sign up or sign in to vote.
4.43/5 (4 votes)
6 Apr 2015CPOL2 min read 17.8K   22   2
This tip explains WCF and helps understand the difference between WCF and Web Services.

Introduction

WCF is a platform for building distributed businesses and deploying services among various endpoints in Windows. WCF was initially called name “Indigo” and we could build service-oriented applications and provide interoperability.

Microsoft first released the WCF bundle with .NET Framework 3.0 in Visual Studio .NET 2008. It provides many useful features for developing services like hosting service instance management, asynchronous calls, reliability, transaction management, disconnected queued calls and security.

Microsoft was the second release of the WCF bundle with .NET Framework 3.5 in Visual Studio .NET 2008. This version supported additional tools and communication options.

Microsoft's third release of the WCF bundle came with .NET Framework 4.0 in Visual Studio.NET 2010. This version released a couple of features, like configuration changes, a few extensions, discovery and routers.

WCF also supports the Windows Azure Platform and supported operating systems such as Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7 and later versions.

WCF is a combination of multiple existing .NET Technologies like ASMX, .NET Remoting, Enterprise Services, WSE and MSMQ.

Advantages of WCF

  • WCF provides better reliability and security compared to ASMX Web services.
  • In WCF, there is no need to make much of a change to code to use the security model and alter the binding.
  • Small changes in the configuration file will match your requirements.
  • WCF provides interoperability between services.

Disadvantage of WCF

  • WCF does not support method overloading functions.

Using the Code

Web Services (ASMX)

Web Services are used to send/receive messages using the Simple Object Access Protocol (SOAP) via HTTP only.

It is available in the namespace “System.Web.Services. WebService Class” with a constructor, methods, properties and events.

Code

C#
using System;    
using System.Collections.Generic;    
using System.Web;    
using System.Web.Services;    
    
namespace HelloWorldServices    
{    
    /// <summary>    
    /// Summary description for Service1    
    /// </summary>    
    [WebService(Namespace = "http://tempuri.org/")]    
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    
    [System.ComponentModel.ToolboxItem(false)]    
    public class Service1 : System.Web.Services.WebService    
    {    
    
        [WebMethod]    
        public string HelloWorld()    
        {    
            return "Hello World";    
        }    
    }    
}

WCF

WCF is used to exchange messages using any format via any transport protocol like HTTP, TCP/IP, MSMQ, Named Pipes and and so on. Its default format is SOAP.

Note: Microsoft Message Queuing (MSMQ) is a messaging queue services developed by Microsoft.

Simple Object Access Protocol (SOAP) is a messaging protocol developed by W3C.

Code: IService1.cs

C#
using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Runtime.Serialization;    
using System.ServiceModel;    
using System.ServiceModel.Web;    
using System.Text;    
    
namespace HelloWorldWCFService    
{    
    // NOTE: You can use the "Rename" command on the "Refactor" 
    menu to change the interface name "IService1" in both code and config file together.    
    [ServiceContract]    
    public interface IService1    
    {      
        [OperationContract]    
        string GetData(int value);    
    
        [OperationContract]    
        CompositeType GetDataUsingDataContract(CompositeType composite);    
    
        // TODO: Add your service operations here    
    }       
    
    // Use a data contract as illustrated in the sample below 
    // to add composite types to service operations.    
    [DataContract]    
    public class CompositeType    
    {    
        bool boolValue = true;    
        string stringValue = "Hello ";    
    
        [DataMember]    
        public bool BoolValue    
        {    
            get { return boolValue; }    
            set { boolValue = value; }    
        }    
    
        [DataMember]    
        public string StringValue    
        {    
            get { return stringValue; }    
            set { stringValue = value; }    
        }    
    }    
} 

Service1.svc

C#
using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Runtime.Serialization;    
using System.ServiceModel;    
using System.ServiceModel.Web;    
using System.Text;    
    
namespace HelloWorldWCFService    
{    
    // NOTE: You can use the "Rename" command on the "Refactor" 
    // menu to change the class name "Service1" in code, svc and config file together.    
    public class Service1 : IService1    
    {    
        public string GetData(int value)    
        {    
            return string.Format("You entered: {0}", value);    
        }    
    
        public CompositeType GetDataUsingDataContract(CompositeType composite)    
        {    
            if (composite == null)    
            {    
                throw new ArgumentNullException("composite");    
            }    
            if (composite.BoolValue)    
            {    
                composite.StringValue += "Suffix";    
            }    
            return composite;    
        }    
    }    
} 

Differences between Web Services and WCF Table

S.Number Functions WCF
1 It is available in the namespace "System.Web. Services.WebService Class" It is available in the namespace "System.ServiceModel"
2 It is supported hosting only IIS Hosting like IIS, Self Hosting (Console hosting), Windows Activation Services, Windows Services is supported.
3 It is used for the XML Serializer only. It is used to DataContractSerializer only
4 One-way communication and Request-Response is supported One-way, two-way (Duplex) communication and Request- Response is supported.
5 Binding like XML 1.0, Message Transmission Optimization Mechanism (MTOM), Custom is supported. Binding like XML 1.0, Message Transmission Optimization Mechanism (MTOM), Binary and Custom is supported.
6 Transport protocol like HTTP, TCP and custom is supported. Transport protocol like HTTP, TCP, Named Pipes, MSMQ, P2P and custom is supported.
7 Protocol security is supported. Protocol security, transaction and reliable message is supported.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionDated Pin
codefabricator10-Apr-15 2:56
codefabricator10-Apr-15 2:56 
AnswerRe: Dated Pin
Santhakumar Munuswamy @ Chennai12-Sep-15 0:07
professionalSanthakumar Munuswamy @ Chennai12-Sep-15 0:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.