Chapter 2. Enhancements to WCF 4.5

In this chapter, we will cover the following recipes:

  • Understanding Windows Communication Foundation (WCF) by creating a basic application
  • Hosting a WCF service in different hosting environments
  • Streaming data over a network using a WCF service
  • Writing a REST-based WCF service
  • Creating a WCF service using the Open Data Protocol standards
  • Extending a WCF service (service extensibility)

Introduction

In today's world, enterprise applications are the key to any enterprise. We are in the age of devices. People use one or multiple devices to be connected with the external world. Business processes need shared services, which can be consumed by these devices. Basically, an application can either consume data directly from the database tier or use loosely coupling models and get data from a hosted service. Most enterprises prefer to have a loose coupling on data so that their apps are maintained and also to sustain a clear separation of the application and business logic. Most of the big giants expose their data using standard compliant web-based services and the clients use them to consume data. Some of the services are also hosted behind the secure socket layer and require a secure encrypted connection between the host and the client to perform a data transfer.

Modern enterprises rely on standardized data points so heavily that many of them are now sharing public content through a common gateway enterprise in a format that is globally supported such that third-party clients can connect to their endpoint and consume their data services to represent information. The hype of ATOM, RSS, or other syndication standards is one of the reasons to support the most standard format. An enterprise exposes shared data in the form of RSS feeds, which can be consumed by a number of RSS-feed readers such that the data easily reaches the common mass.

Service-oriented architecture is a design principle that people follow to outline well-defined services that can be consumed by the external world using a common set of contracts, and each of the services can be individually modified independent of each other. They also need to provide a message-based communication approach based on open standards. As a technology giant, Microsoft has also implemented the SOA design principle and provided an easy way of implementation. Microsoft has named it Windows Communication Foundation (WCF). WCF makes it very easy to create and consume service-oriented business processes similar to point-to-point architectures. It is an architecture that has been moving forward in the .NET environment to address service-oriented architecture by providing a standard way to write services. WCF combines the existing technologies, such as TCP, named pipes, web services, sockets, and MSMQ and creates an easy way to implement a standard SOA representation. WCF has been widely accepted as a standard format to write web services with the support of multiple protocols and endpoints.

Note

Simple Object Access Protocol (SOAP) is one of the preferred SOA models, where communication between the two parties is made using an XML representation of data. SOAP is an extensible message standard supported by W3C, which generally runs on the HTTP protocol under the request-response model, and communicates between two endpoints using standard messages with a predefined header information for each envelope.

Each service provides a WSDL that defines the definition of the public interfaces including the metadata, such as protocol bindings and message formats. Generally, WSDL is defined in an XML format such that it is clearly understood and can be used by devices on different platforms to invoke functionalities of the service. WSDL is similar to Interface definition language (IDL) for web services. The definition of WSDL is that it is typically used during design time to build proxy classes, which will be later used to send and receive responses from the server. The main advantage of the WSDL standard is that it is platform independent and easy to implement. Let's look into the architecture of WCF, as shown in the following figure:

Introduction

It is important to note that all distributed technologies are based on the same concept. However, each of them provides specific services unique to the product. For instance, ASMX is a traditional web service that provides a standard SOAP-based communication protocol. Remoting is used to provide remote access of two .NET-based applications. As a programmer, you are forced to learn each of these technologies one by one to make use of them. WCF provides a unified solution to all the distributed technologies. It provides a single programming model to leverage the features of any of the preceding distributed technologies. This means you can write one service once and expose different endpoints just by configuring WCF, and everything will work in the same way as it did for specific technologies.

In WCF, the three most important things that you need to remember (popularly termed as the ABCs of WCF) are as follows:

  • Address (where): This represents a unique network URL that specifies the service and where messages can be sent. Generally, it is made up of the protocol, IP address of the network terminal, and service location.
  • Binding (how): This represents how the client should communicate with the service. WCF supports a number of binding protocols that you can use to define the service, for example, BasicHttpBinding, WSHttpBinding, MsmqIntegrationBinding, NetTcpBinding, NetNamedPipeBinding, and so on.
  • Contract (what): This defines what the messages should contain. The contract is generally a set of interfaces that the client needs to take care of to request the service.

Now, let's create the simplest WCF service to get a basic idea of how it works.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.72.74