Introduction to WCF - CodeProject

:

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

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

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

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.