WCF (Windows Communication Foundation): WCF, as the name suggests, is a unified .NET framework that is used to develop service-oriented applications. It allows you to develop applications that can communicate using different communication mechanisms.
It is founded for other Microsoft Distributed Technologies and considered the future of distributed computing. Because of its flexibility, it makes the development of endpoints much easier. It supports various programming languages and platforms. It is SOAP-based and returns data in XML form. It can be hosted in different scenarios, and such scenarios include various services such as WAS, IIS, Managed Windows, etc. The following code will be used to build a service in WCF:
[ServiceContract] public interface ITest { [OperationContract] string ShowMessage(string strMsg); } public class Service: ITest { public string ShowMessage(string strMsg)
Web Service: Web Service, as the name suggests, is a client-server application that allows communication between client and server applications. It is basically a software module specially designed to execute a certain set of tasks. This service is specially used to make application platforms and technology independent. There are two types of web services include the SOAP web services and RESTful web services. Following code will be used to build a service in Web service:
[WebService] public class Service: System.Web.Services.WebService { [WebMethod] public string Test(string strMsg) { return strMsg; } }
WCF vs Web Service
WCF | Web Service |
---|---|
It is used for sending data asynchronous messages from one service endpoint to another. | It is used to exchanging data between applications or systems. |
Protocols used in this channel include HTTP, TCP, MSMQ, named Pipers. | Protocols used in the channel include HTTP and JMS. |
It is designed to offer a manageable approach for creating Web Services and Web service clients. | It is designed to perform or execute a certain set of tasks. |
It provides a runtime environment for service allowing you to expose CLR types as Services and to use other services as CLR types. | Service-oriented allows you to expose the functionality of existing code over the network so that other applications can consume the functionality of your program. |
Its features include service-oriented, reliable, and queued messages, multiple transports, and encoding that support multiple message patterns, interoperability, etc. | Its features include loosely coupled, supports document exchange, supports RCF (Remote Procedure Calls), ability to be Synchronous or Asynchronous, language-independent and interoperable, etc. |
It is more flexible as this service can be hosted in different types of applications. | It is less flexible as it can only be accessed over HTTP. |
Such service can be hosted on IIS, WAS, and Windows services. | Such a service can only be hosted on IIS server. |
It uses DataContractSerializer. | It uses XmlSerializer. |
Its extension is “.svcâ€. | Its extension is “.asmxâ€. |
It is more reliable, fast, and robust as compared to web services, and therefore it considered good for developing real-time applications. | It is not reliable and slow as compared to WCF. |
It supports duplex operations and multi-threading. | It does not support duplex operations and multi-threading. |
It also supports robust security, trustworthy messaging, transaction. | It only supports security services. |
There are some major differences that exist between WCF and a Web service which are listed below.
Attributes − WCF service is defined by ServiceContract and OperationContract attributes, whereas a web service is defined by WebService and WebMethod attributes.
Protocols − WCF supports a range of protocols, i.e., HTTP, Named Pipes, TCP, and MSMQ, whereas a web service only supports HTTP protocol.
Hosting Mechanisms − Various activation mechanisms are there for WCF hosting, i.e., IIS (Internet Information Service), WAS (Windows Activation Service), Self-hosting and Windows Service, but a web service is hosted only by IIS.
Services − WCF supports a robust security, trustworthy messaging, transaction and interoperability, while a web service only supports security services.
Serializer − WCF Supports DataContract serializer by employing System.Runtime.Serialization, whereas a web service supports XML serializer by making use of System.Xml.Serialization.
Tools − ServiceMetadata tool (svcutil.exe) is used for client generation for a WCF service, while WSDL.EXE tool is used for generating the same for a web service.
Exception Handling − In WCF, unhandled exceptions are handled in a better way by making use of FaultContract. They do not return to the client like in a web service as SOAP faults.
Hash Table − It is possible to serialize a Hash Table in WCF, but this is not the case in a web service.
Bindings − WCF supports several types of bindings like BasicHttpBinding, WSDualHttpBinding, WSHttpBinding, etc., while a web service supports only SOAP or XML.
Multithreading − WCF supports multithreading by using the ServiceBehavior Class, whereas this is not supported in a web service.
Duplex Service Operations − WCF supports duplex service operations apart from supporting one-way and request-response service operations, whereas a web service does not support duplex service operations.