Introduction to Windows Communication Foundation

12 juli 2021 om 10:00 by ParTech Media - Post a comment

The programming world is moving towards distributed systems. Here’s what we mean by that- any activity carried out by a user is accomplished by two or more applications in the backend. However, this is achieved with a better user experience and lesser latency.

This approach comes up with a lot of benefits from a user perspective as well as from a system architecture side. Since the systems are distributed, it is easier to maintain and develop the system.

Also, it gives the advantage of creating individual applications in different programming languages. So, based on the need and complexity, developers and architects can choose the programming language of their choice.

Though we see a lot of advantages with the distributed system, it also comes with one complex challenge to handle. Let us explain this with an example -

Imagine a backend system that is being consumed by multiple clients. A real-time example would be the backend of an e-commerce system that provides user address details to a UI. This is being used by the end-user. Also, the same data is being sent to a conveyor belt to segregate/group parcels based on the zip codes.

Here, the UI expects the data to be in JSON format and the conveyor expects the data to be in binary format. Though the same data is being used, two different forms of the data are being required.

In such cases, repeating the business logic or writing the code to transpose the required format will be a cumbersome process. Also, there are chances that the applications might expect the data to be sent over different transportation protocols, say one with HTTP and another with TCP. To overcome this difficulty and provide interoperability, Microsoft has provided WCF.

Let's see what is WCF and how it is useful in this blog.

Table of contents

  1. What is WCF?
  2. Components in WCF,
  3. How to create a WCF service, and
  4. Conclusion.

What is WCF?

WCF stands for Windows Communication Foundation. It is a framework introduced by Microsoft from .Net framework version 3.0. It combines the functionality of ASP. Net web services, enterprise services, Messaging Queue, and .Net Remoting.

It provides an important advantage for developers - It helps them concentrate on the business logic rather than on the daunting task of creating communication and transport protocols. Similar to WebAPIs, WCF also has endpoints through which the clients pass on the input data and get the output data.

Components in WCF

To access the web services, endpoints are typically used. Endpoints are configured to call a particular method in a Service class, which, in turn, provides the type of data that will be communicated through the endpoint.

WCF provides three basic components (referred colloquially to as ABC of WCF) which cover its complete functionality. Those are -

  • Address,
  • Binding
  • Contract

Address

The address provides the exact location of your endpoint. Below are a few examples of an address -

  • http://partech.com/MyService
  • net.tcp://partech.com/MyService

Syntax for the address is - Transportschema:serverlocation:port/path/subpath

Here, the transport schema can be any one of the transport protocols that come with WCF. Server location is the IP or the masked address of the webpage. Port is optional. Path and subpath denote the method in the service which needs to be called.

Binding

Binding holds the information about how the data is being handled at the server-side and client-side i.e it defines the transportation and encoding protocols.

Some basic binding types are

  1. Basic Binding
  2. TCP Binding
  3. Web Service (WS) Binding
  4. Duplex WS Binding
  5. MSMQ Binding

Contract

As the name suggests, it is an agreement between client and server about the content and structure of the message that will be exchanged. Data contract defines the structure of the message and message contract defines the content of the message.

How to create a WCF service?

To create a WCF-based service, users are required to install Visual Studio 2019 along with the component - Windows Communication Foundation (which is present under the Individual Component page of the Installation wizard).

Open Visual Studio and look for the project template - WCF Service Application. Provide a valid name for the solution. Here we are creating the application under the .Net Framework 4.7.2 version.

A project with the below structure gets created -

Let's go through them one by one. First, let's check out the IService.cs file. This contains the Service Contract (the interface that holds the endpoints) and Operation Contracts (Endpoints that will be used for communication).

On the same file, below the Service Contract, Data Contract is present. This provides the data member that will be used for communication.

In the Service1.svc.cs file, the class implements the interface of the Service contract. Also, the definition of the Operational contracts is created in the class that implements the Service contract. Here, we have two operational contracts provided in the service contract, the same is being implemented in the Service1 class.

Now the important part - web.config file. Here, along with the standard settings of a .Net application, WCF configurations are also included. This includes binding of the application that will be used for communication and will be configured here. Based on the requirement, users can modify the config. By default, the binding is set with basicHttpsBinding.

To run the application, click on the debug icon that is present in Visual studio. This opens a WCF test client. The test client provides the address and endpoints that are exposed.

Users can provide inputs and get the output from the endpoints.

Also, the input and output can be viewed in XML format. The same application can be hosted in IIS and can be called through SOAP UI (a third-party tool for testing services).

Similarly, the application can be referred to and consumed on the client side.

Conclusion

WCF provides the flexibility and advantage for developers to achieve interoperable functionality without focusing too much on how it is being implemented. So go ahead and try it out in your next application.