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.
- [WebService]
- public class MyService
- {
- [WebMethod]
- public SumClass SumOfNums(string JsonStr)
- {
- var ObjSerializer = new JavaScriptSerializer();
- var ObjSumClass = ObjSerializer.Deserialize<SumClass>(JsonStr);
- return new SumClass().GetSumClass(ObjSumClass.First, ObjSumClass.Second);
- }
- }
-
- public class SumClass
- {
- public int First, Second, Sum;
-
- public SumClass GetSumClass(int Num1, int Num2)
- {
- var ObjSum = new SumClass
- {
- Sum = Num1 + Num2,
- };
- return ObjSum;
- }
- }
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.
- [ServiceContract]
- blic class MyService : WebService
- {
- [OperationContract]
- public SumClass SumOfNums(string JsonStr)
- {
- var ObjSerializer = new JavaScriptSerializer();
- var ObjSumClass = ObjSerializer.Deserialize<SumClass>(JsonStr);
- return new SumClass().GetSumClass(ObjSumClass.First, ObjSumClass.Second);
- }
- }
- [DataContract]
- public class SumClass
- {
- [DataMember]
- public int First;
- [DataMember]
- public int Second;
- [DataMember]
- public int Sum;
-
- public SumClass GetSumClass(int Num1, int Num2)
- {
- var ObjSum = new SumClass
- {
- Sum = Num1 + Num2,
- };
- return ObjSum;
- }
- }
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:
-
- namespace MyWebServiceDemo
- {
- public class CustomSoapHeader : SoapHeader
- {
- public string CustomElement;
- }
- [WebService]
- public class MyService
- {
- CustomSoapHeader ObjCustomSoapHeader = new CustomSoapHeader();
- [WebMethod]
- [SoapHeader("ObjCustomSoapHeader")]
- public int SumOfNums(int First, int Second)
- {
- return First + Second;
- }
- }
- }
WCF provides[MessageContract], [MessageHeader] and [MessageBodyMember] attributes to describe the structure of SOAP messages.
- [MessageContract]
- public class CustomHeader
- {
- [MessageHeader]
- public string UserName;
- [MessageBodyMember]
- public int Id;
- }
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.
- [ServiceContract]
- public class FaultDemo
- {
- [OperationContract]
- [FaultContract(typeof(FaultClass))]
- public int Divide(int Dividend, int Divider)
- {
- if (Divider == 0)
- {
- throw new FaultException<FaultClass>(new FaultClass
- {
- ExceptionReason = "Divider is 0",
- ExceptionDetails = "Divider should be non-zero"
- });
- }
- return Dividend / Divider;
- }
- }
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.