Web Service vs. WCF Service

Web Service vs. WCF Service

1. Web service is there with .net framework from version 1.0. Whereas, WCF service got introduced with .NET 3.0.
2. ASP.NET Web services send and receive messages by using SOAP over HTTP or HTTPS.
WCF services use SOAP by default, but the messages can be in any format, and conveyed by using any transport protocol like HTTP,HTTPs, WS- HTTP, TCP, Named Pipes, MSMQ, P2P(Point to Point) etc.
3. Web services have “.asmx” extension, whereas Wcf services have “.svc” extension.
4. The asmx page uses “WebService” directive where as the svc page uses “ServiceHost” directive.
5. ASP.NET Web services rely on the XmlSerializer in System.XML.Serialization namespace for serialization (to translate data in .NET Framework types to XML and vice versa for transmission to or from a service). XmlSerializer has some limitations like:
  • Only public properties/fields can be serialized.
  • Only collection classes implementing IEnumerable or Icollection can be serialized.
  • Classes that implement IDictionary, such as HashTable cannot be serialized.
  • You can't explicitly indicate which fields or properties are to be serialized into XML and which are to be ignored by serializer.

ASP.NET WCF services use DataContractSerializer in System.RunTime.Serialization namespace for serialization, which overcomes all the limitations of XmlSerializer mentioned above.

6. To develop a web service using ASP.NET, [WebService] attribute has to be added to the service class, and the [WebMethod] attribute to the methods that are to be exposed by the service.
  1. [WebService]      
  2. public class MyService  
  3.    {      
  4.          [WebMethod]  
  5.         public SumClass SumOfNums(string JsonStr)  
  6.         {  
  7.             var ObjSerializer = new JavaScriptSerializer();  
  8.             var ObjSumClass = ObjSerializer.Deserialize<SumClass>(JsonStr);  
  9.             return new SumClass().GetSumClass(ObjSumClass.First, ObjSumClass.Second);  
  10.         }  
  11.    }  
  12.   
  13.     public class SumClass  
  14.     {  
  15.         public int First, Second, Sum;  
  16.   
  17.         public SumClass GetSumClass(int Num1, int Num2)  
  18.         {  
  19.             var ObjSum = new SumClass  
  20.             {  
  21.                 Sum = Num1 + Num2,  
  22.             };  
  23.             return ObjSum;  
  24.         }  
  25.     } 
Classes and structures to be exposed should be decorated with [DataContract] attribute and [DataMember] attribute should be added to fields or properties.[ServiceContract] attribute has to be added to the service class.[OperationContract]attribute should be added to the methods exposed by the service.
  1. [ServiceContract]     
  2. blic class MyService : WebService  
  3.  {    
  4.  [OperationContract]  
  5.       public SumClass SumOfNums(string JsonStr)  
  6.       {  
  7.           var ObjSerializer = new JavaScriptSerializer();  
  8.           var ObjSumClass = ObjSerializer.Deserialize<SumClass>(JsonStr);  
  9.           return new SumClass().GetSumClass(ObjSumClass.First, ObjSumClass.Second);  
  10.       }  
  11.  }  
  12.   [DataContract]      
  13.   public class SumClass  
  14.   {  
  15. [DataMember]  
  16.       public int First;  
  17.  [DataMember]  
  18. public int  Second;  
  19. [DataMember]  
  20. public int  Sum;  
  21.   
  22.       public SumClass GetSumClass(int Num1, int Num2)  
  23.       {  
  24.           var ObjSum = new SumClass  
  25.           {  
  26.               Sum = Num1 + Num2,  
  27.           };  
  28.           return ObjSum;  
  29.       }  
  30.   }  
7. ASP.net Web service can be hosted in IIS only.
WCF services can be hosted in IIS , WindowsActivation Services(WAS), Managed Windows services or self hosting etc.
 
8. Clients for ASP.NET Web services are generated using a command-line tool named WSDL.exe

WCF uses ServiceModelMetadata Utility tool,svcutil.exe for the same purpose.
 
9. The SOAP header can be customized. For a custom soap header, Create a class with required fields & derive it from SoapHeader to define the structure of the header, and then add the [SoapHeader] Attribute to the method to indicate the presence of a custom header & an instance of the custom SoapHeader class you created like:
    1. namespace MyWebServiceDemo  
    2.     {  
    3.         public class CustomSoapHeader : SoapHeader  
    4.         {  
    5.             public string CustomElement;  
    6.         }      
    7.  [WebService]  
    8.  public class MyService  
    9.  {  
    10.         CustomSoapHeader ObjCustomSoapHeader = new CustomSoapHeader();  
    11.         [WebMethod]  
    12.         [SoapHeader("ObjCustomSoapHeader")]  
    13.         public int SumOfNums(int First, int Second)  
    14.         {  
    15.             return First + Second;  
    16.         }  
    17.      }  
    18.     } 
    WCF provides[MessageContract], [MessageHeader] and [MessageBodyMember] attributes to describe the structure of SOAP messages.
    1. [MessageContract]  
    2.  public class CustomHeader  
    3.  {  
    4.      [MessageHeader]  
    5.      public string UserName;  
    6.      [MessageBodyMember]  
    7.      public int Id;  
    8.  } 
10. In ASP.NET Web services, unhandled exceptions are returned to clients as SOAP faults. Instances of the SoapException class can also be thrown.
In WCF services, unhandled exceptions are not returned to clients as SOAP faults. FaultContract can be used to exceptions to client.
  1. [ServiceContract]  
  2.  public class FaultDemo  
  3.  {  
  4.      [OperationContract]  
  5.      [FaultContract(typeof(FaultClass))]  
  6.      public int Divide(int Dividend, int Divider)  
  7.      {  
  8.          if (Divider == 0)  
  9.          {  
  10.              throw new FaultException<FaultClass>(new FaultClass  
  11.                  {  
  12.                      ExceptionReason = "Divider is 0",  
  13.                      ExceptionDetails = "Divider should be non-zero"  
  14.                  });  
  15.          }  
  16.          return Dividend / Divider;  
  17.      }  
  18.  } 

 11. If ASP.NET Web service class is derived from WebService, the WebService base class’ Context property can be used to access HttpContext object.Application property of HttpContext object can be used for application state and it's Session property can be used for session state.

WCF provides extensible objects for state management. ServiceHostBase is used to maintain state that all instances of all services on the same host can access, and InstanceContext maintains state that can be accessed within the same instance of a service.
 
12. Web services don’t support multi-threading. Whereas WCF services Support multi-threading by using ServiceBehaviour class.
 
13. Performance wise web services are slower than WCF service. This is because of the serializers they use.
 
14. The options for securing ASP.NET Web services are same as those for any IIS application.
WCF services also support all these securities provided by IIS and also behavior element of wcf config can be modified to provide some more security options.

So, WCF service is an advanced technology than Web service. Performance wise WCF is faster than web service. WCF provides more security, support various protocols & message formats. The only costly area of WCF for developer is it's configuration settings. However, this headache also got solved with WCF4.0 by providing default configuration settings. You can notice upto .NET3.5 visual studio is providing a direct template for web service. From .NET4.0, you will not get any direct template for web service so, you need to create a web application & add a web service to it. 

Up Next
    Ebook Download
    View all
    Learn
    View all