Difference Between Web Service and WCF

Overview

 
My previous article provided an Introduction to WCF (Windows Communication Framework). Now we will discuss the differences between Web Services and WCF concepts.
 

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, prosperities and events.
 
Code
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Web;    
  4. using System.Web.Services;    
  5.     
  6. namespace HelloWorldServices    
  7. {    
  8.     /// <summary>    
  9.     /// Summary description for Service1    
  10.     /// </summary>    
  11.     [WebService(Namespace = "http://tempuri.org/")]    
  12.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    
  13.     [System.ComponentModel.ToolboxItem(false)]    
  14.     public class Service1 : System.Web.Services.WebService    
  15.     {    
  16.     
  17.         [WebMethod]    
  18.         public string HelloWorld()    
  19.         {    
  20.             return "Hello World";    
  21.         }    
  22.     }    
  23. }  

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
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Runtime.Serialization;    
  5. using System.ServiceModel;    
  6. using System.ServiceModel.Web;    
  7. using System.Text;    
  8.     
  9. namespace HelloWorldWCFService    
  10. {    
  11.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.    
  12.     [ServiceContract]    
  13.     public interface IService1    
  14.     {    
  15.     
  16.         [OperationContract]    
  17.         string GetData(int value);    
  18.     
  19.         [OperationContract]    
  20.         CompositeType GetDataUsingDataContract(CompositeType composite);    
  21.     
  22.         // TODO: Add your service operations here    
  23.     }    
  24.     
  25.     
  26.     // Use a data contract as illustrated in the sample below to add composite types to service operations.    
  27.     [DataContract]    
  28.     public class CompositeType    
  29.     {    
  30.         bool boolValue = true;    
  31.         string stringValue = "Hello ";    
  32.     
  33.         [DataMember]    
  34.         public bool BoolValue    
  35.         {    
  36.             get { return boolValue; }    
  37.             set { boolValue = value; }    
  38.         }    
  39.     
  40.         [DataMember]    
  41.         public string StringValue    
  42.         {    
  43.             get { return stringValue; }    
  44.             set { stringValue = value; }    
  45.         }    
  46.     }    
  47. }   
Service1.svc
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Runtime.Serialization;    
  5. using System.ServiceModel;    
  6. using System.ServiceModel.Web;    
  7. using System.Text;    
  8.     
  9. namespace HelloWorldWCFService    
  10. {    
  11.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.    
  12.     public class Service1 : IService1    
  13.     {    
  14.         public string GetData(int value)    
  15.         {    
  16.             return string.Format("You entered: {0}", value);    
  17.         }    
  18.     
  19.         public CompositeType GetDataUsingDataContract(CompositeType composite)    
  20.         {    
  21.             if (composite == null)    
  22.             {    
  23.                 throw new ArgumentNullException("composite");    
  24.             }    
  25.             if (composite.BoolValue)    
  26.             {    
  27.                 composite.StringValue += "Suffix";    
  28.             }    
  29.             return composite;    
  30.         }    
  31.     }    

Differences between Web Services and WCF

 
S.No. 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 It is supported hosting like IIS, Self Hosting (Console hosting), Windows Activation Services, Windows Services
3 It is used for the XML Serializer only It is used to DataContractSerializer only
4 It is supported one-way communication and Request-Response It is supported one-way, two-way (Duplex) communication and Request- Response
5 It is supported binding like XML 1.0, Message Transmission Optimization Mechanism (MTOM), Custom It is supported binding like XML 1.0, Message Transmission Optimization Mechanism (MTOM), Binary and Custom
6 It is supported transport protocol like HTTP, TCP and custom It is the supported transport protocol like HTTP, TCP, Named Pipes, MSMQ, P2P and custom
7 It is supported protocol security It is the supported protocol security, transaction and reliable message
 

Conclusion

 
This article will help to understand the differences between Web Services and WCF in .NET.


Similar Articles