Web Services and WCF
What are Web Services?
- A web service is any piece of software that makes itself available over the internet and uses a standardized XML messaging system. XML is used to encode all communications to a web service. For example, a client invokes a web service by sending an XML message, then waits for a corresponding XML response. As all communication is in XML, web services are not tied to any one operating system or programming language—Java can talk with Perl; Windows applications can talk with Unix applications.
- Web services are self-contained, modular, distributed, dynamic applications that can be described, published, located, or invoked over the network to create products, processes, and supply chains. These applications can be local, distributed, or web-based. Web services are built on top of open standards such as TCP/IP, HTTP, Java, HTML, and XML.
- Web services are XML-based information exchange systems that use the Internet for direct application-to-application interaction. These systems can include programs, objects, messages, or documents.
- A web service is a collection of open protocols and standards used for exchanging data between applications or systems. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Java and Python, or Windows and Linux applications) is due to the use of open standards.
To summarize, a complete web service is, therefore, any service that −
- Is available over the Internet or private (intranet) networks
- Uses a standardized XML messaging system
- Is not tied to any one operating system or programming language
- Is self-describing via a common XML grammar
- Is discoverable via a simple find mechanism
Components of Web Services
The basic web services platform is XML + HTTP. All the standard web services work using the following components −
- SOAP (Simple Object Access Protocol)
- UDDI (Universal Description, Discovery and Integration)
- WSDL (Web Services Description Language)
All these components have been discussed in the Web Services Architecture chapter.
How Does a Web Service Work?
A web service enables communication among various applications by using open standards such as HTML, XML, WSDL, and SOAP. A web service takes the help of −
- XML to tag the data
- SOAP to transfer a message
- WSDL to describe the availability of service.
You can build a Java-based web service on Solaris that is accessible from your Visual Basic program that runs on Windows.
You can also use C# to build new web services on Windows that can be invoked from your web application that is based on JavaServer Pages (JSP) and runs on Linux.
Example
Consider a simple account-management and order processing system. The accounting personnel use a client application built with Visual Basic or JSP to create new accounts and enter new customer orders.
The processing logic for this system is written in Java and resides on a Solaris machine, which also interacts with a database to store information.
The steps to perform this operation are as follows −
- The client program bundles the account registration information into a SOAP message.
- This SOAP message is sent to the web service as the body of an HTTP POST request.
- The web service unpacks the SOAP request and converts it into a command that the application can understand.
- The application processes the information as required and responds with a new unique account number for that customer.
- Next, the web service packages the response into another SOAP message, which it sends back to the client program in response to its HTTP request.
- The client program unpacks the SOAP message to obtain the results of the account registration process.
What is WCF?
WCF stands for Windows Communication Foundation. It is basically used to create a distributed and interoperable Application. WCF Applications came into the picture in .Net 3.0 Framework. This is a framework, which is used for creating Service oriented Applications. You can send the data asynchronously from one end point to another. I think you all know about Web Service and are thinking, if we already have Web Services and accessing on a remote basis then why did WCF come into picture?
Distributed Application
It means those Applications, which do not run only on single system but can run on multiple systems, which are connected over the network. For example, a Web Service that can consume by different clients.
Interoperable
Example
Web Service is interoperable because it can be consume from any client, either it is from Java or .NET but on a remote basis, it is not interoperable because if we want to consume any remote service from the client, client Application must be developed in .NET.Why we need WCF ApplicationsIn this, I am explaining why we need WCF Application, if we already have Web Service.
Suppose, you have two clients- one wants to use a Web Service, which sends data over the network, using Http protocol and want reply in XML format, so we will create a Web Service.The other wants to send the data, using Web Service over the network, using TCP protocol and replying in binary format, then we need to implement a remote Web service with TCP protocol.
Problem
The problem is in the example, shown above, we need to create two different Services for two different clients .WCF is solving this problem and one single service can be consumed by two different clients- either they want same protocol or a different protocol. We specify the protocol name in an endpoint attribute of the Web Service.
WCF
There are three parts of WCF Application or we can say it consists of three things, which are-
- WCF Service : What is the service and what it is providing.
- WCF Service host: Where is the Service hosted.
- Service Client: Who is the client of the Service.
Fundamentals of WCF
Message
- Header
- Body
By default Header and fault are disabled but Body is responsible for the data transmission or data exchanging.
Header is useful to send some data from client to Server. Suppose we want to send user name from each request but don’t want to send it by an argument, we can easily add it into message header.
Endpoint
Endpoint is a very essential part of WCF Application, as it describes the address of Web Service from where a user can receive and send the message. It also specifies the communication mechanism of how the message will be sent or received.
End point consists of three things, which are A,B,C and each of them have a question mark.
- Address (Where?)
- Binding (how?)
- Contract (What?)
Endpoint = A + B+ C
Address is the address of WCF Service, where the Service is hosted? It gives the exact URL of Web Service, where the Service hosts the pattern of URL, which is-
Scheme://domain/[:port]/path
net.tcp://localhost:1234/MyService
http://localhost:1234/MyService
Binding
It describes the way or mechanism by which the user will communicate with Web Service. It constitutes some binding element, which creates the structure of communication such as some transport protocols like HTTP, TCP etc. Message format or security techniques etc.
Contract
Contract is the third important question. What functionality and operation is being provided by the service is called contract. It specifies what functionality and operations are need to be exposed to the client. It is the interface name which has all operation that need to be exposed.
Hosting
Hosting is the important thing in WCF Application and it makes WCF Application different from other distributed Applications. WCF supports following types of hosting-
- IIS Hosting
- Self hosting
- WAS hosting
SOAP
SOAP stands for Simple Object Access Protocol. It is not a transport protocol but an XML based message protocol.
Difference between WCF and Web Service
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. |