Windows Communication Foundation
Part-1
Abstract
Learn Digital Forensics
Over the year, we have learned innumerable ways of consuming services across the network such as Remoting, COM, COM+, MSMQ, Web Services using ASP.NET and DCOM. Every Technology has its advantage and disadvantage. This article commences by framing the need for WCF and examining the problems it intended to resolve, by way of quick regression of earlier distributing computing technology. After completion of this blog, you would be able to build several WCF services, host the service and, consume it into client application using various WCF development tools. This article aims to get the understanding of in-house development of WCF services rather than actual hosting environment such as IIS web server.
Legacy Distributed Technologies
You have a far greater chance of ensuring that each connected computer is running the same operating system and using the same platform. While, the Windows operating system has provided us many APIs for building distributed systems. Here the following section depicts a quick recap of some of the major distributed technology so that we can get the obvious need of WCF.
.NET Remoting
The .NET Remoting API allows to distribute objects across multiple computers under .NET platforms. This technology provided some useful features such as performance, managing and manipulating the service just by editing the configuration files rather than whole application to be rebuilt. But interoperability between other programming languages such as JAVA is still not possible in Remoting technology. Object distribution could happen across windows operating systems only.
COM / DCOM
The DCOM (Distributed Component Object Model) was the Remoting API of choice for Microsoft Centric developers. DCOM is extending the features of COM. It's possible to build a distributed system using COM objects. You can program client software in such a way that the physical locations of the remote objects were not hard-coded in the application. During the era of COM, it was not possible to consume objects or proxy class across LAN but regardless of whether the remote object was on the same machine or secondary networked machine, the code base could remain neutral because the actual location was recorded externally in the system registry.
XML Web Services
The main idea behind XML service is to achieve a high degree of interoperability across operating systems and platforms. The XML web service faces a major problem because they are not 100% compatible with other web service implementations. To overcome such a problem W3C has written several specifications that laid out how vendors should build web services to ensure interoperability. Microsoft implementation of most of these standards is encapsulated in WSE toolkit. But when we build a WCF services application, you do not need to adhere these semantics defined in the WSE toolkit.
COM+ (MTS)
Microsoft released COM+ to fill in the missing pieces of features that were absent in the COM/DCOM technology. COM+ providing a number of features that serviced component leverage, including pooling service, object life time, transaction management and role base security. The COM+ technology is a windows-only that is best suited for in-house application development.
Anatomy of WCF
We can build web services and client applications that can communicate and interoperate with web services and client applications running on other non-windows operating systems. So why do we need WCF? If you are building a distributed application for windows, which technology should we use? The objective behind WCF is to provide a unified programming model for many of these technologies such as Web services, MSMQ, Remoting, COM+ and DCOM, enabling you to build an application that is as independent as possible from the underlying mechanism used to connect services and applications together.
WCF stands for Windows Communication Foundation, introduced with .NET 3.0 frameworks, and integrates those previously independent distributed technologies into a streamline API, represented by the System.ServiceModel namespace. We can expose the service to callers using a wide variety of tactics by employing WCF. Integration and Interoperability of diverse API are typically two major aspects of WCF. Here, consider the following list of major features of WCF as:
When you build a WCF distributed system, you will typically have to create three interrelated assemblies;
- WCF Service Assembly: This *.dll contains the classes and interface that represent the overall functionality you want to expose to external caller.
- WCF Service Host: This software module is the entity that hosts your WCF service assembly.
- WCF Client: This is the application that accesses the service's functionality through an intervening proxy.
if we develop a WCF service on local machine, then we do not need to create three files, we can eliminate the host dll generation part
Building WCF Service Assembly
Visual studio 2010 IDE provides the ideal environment for building and consuming WCF web service and applications. The visual studio development tools for the .NET framework 4.0 include a project template that you can use for creating WCF service.
At this movement, we are creating a WCF service which is hosted locally. Since, we are hosting it locally, we do not need to create a separate solution in order to host this service which typically happens in the case of on HTTP or TCP binding.
To begin, open the Visual studio 2010 IDE and create a WCF Service Library project named MagicMathWcf, making sure the correct option under the WCF node of the New Project Dialog box as following;
Defining the Contract
The WCF service requires an interface like structure referred to as Contracts that the service will implement. The will build a service that confirms all of these contracts. The WCF typically stipulates three types of contracts as Data Contract, Message Contract and Service Contracts. The Data contract specifies the details of products that the WCF service can pass to operation. In simple term, the Data contract defines the variables. On the other hand, the service contact defines the operations (methods) that the WCF service implements. For a CLR interface to participate in the service provided by WCF, it must be adorned with the [ServiceContract] attribute.
The following code depicted in the implementation of Service contract. Change the name of the initial IService.cs file to IMath.cs and Service1.cs to MathLib.cs. Once you do so, delete all the existing example code from both of files and place the following code in the IMath.cs as following;
[c]
using System;
namespace MagicMathWcf
{
[ServiceContract]
public interface IMath
{
[OperationContract]
[OperationContract]
string WelcomeMsg(string str);
}
}
Implementing the Service
Now, we have specified the structure of the data passed to the WCF service by using a data contract and define the shape of the WCF service by using a service contract, next step is to write the code that actually implements the service code like an interface.
So, open the MathLib.cs file and implement the IMath interface over there. Here, specify the previously defined methods body implementation logic as following:
[c]
using System;
namespace MagicMathWcf
{
public class MathLib : IMath
{
public double calSqrt(double value)
{
return Math.Sqrt(value);
public string WelcomeMsg(string str)
{
return "Hello " + str;
}
}
}
Testing and Deploying the Service
Lastly, double-check the details in the app.config file in order to make sure and verify that the file renaming operation is completed correctly. Finally, debug the service project. One benefit of WCF service project is that it provides a service simulator where we can test our service implementation. Just double click over the method that you wanted to test, supply the necessary argument and click the Invoke button. It will yield the output as the following:
We can also test the WCF service from command prompt by wcftestclient.exe utility. Just pass the service metadata URL and testing begin with a similar simulator as earlier.
WCF Service Binding
Once you have defined and implemented the service and data contract in your service library, the next arbitrary step is to build the hosting agent for WCF service. Fortunately, we have diverse possible range of hosts to choose from, all of which must specify the binding used by remote caller to gain access to the service. WCF ships with many binding choices, each of which is tailored to a specific need.
HTTP Binding
The BasicHttpBinding class is invented in HTTP binding mechanism in order to expose contracts types through XML web service protocols. This binding uses HTTP as the transport and Text/XML as the default encoding message. The main reason for using this binding is to maintain backward compatibility with applications that were previously built to communicate with ASP.NET web services.
TCP binding
The NetTcpBinding class is behind the TCP binding implementation. This class uses TCP to move binary data between the client and WCF service which results in higher performance than the web service protocol. The TcpNetBinding supports transaction, reliable session and secure communication.
MSMQ binding
The NetMsmgBinding class are of immediate interest if you want to integrate with Microsoft MSMQ server. This type of binding is used for cross machine communication between .NET applications. This is the preferred approach among the MSMQ-centric binding.
Hosting WCF Service
Since, we are hosting the WCF service on local machines, then we do not need this section indeed. Basically, this step is required when we create a WCF service that is hosted on IIS server. During that process, a final endpoint address is generated that is reference in the configuration file. Just for knowledge point of view, we create a separate console based application which works as a middleware responsible for start and stop the service. In order to start the service, first add the reference of WCF service dll and ServiceModel.dll in that solution and place the following code:
[c]
using System;
using System.ServiceModel;
namespace MathMagicHOST
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost sHost=new ServiceHost(typeof(MathLib)))
{
sHost.Open();
Console.WriteLine("The Service Started.......");
Console.ReadLine();
}
}
}
}
Thereafter, add a configuration file and mention the endpoint address and base address which is responsible to invoke the service.
[xml]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name ="MagicMathWcf.MathLib">
<endpoint address =""
binding="basicHttpBinding"
<host>
<baseAddresses>
<add baseAddress =""/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
We shall dig deeper in this section as well as the next article of this series.
Consuming WCF Service
Now, the final task is to build a piece of software to communicate with this WCF service type. Consuming of WCF service is basically generating a proxy class of that service which is later referenced in the client application. There are plenty of ways to generate proxy class. In this section, we shall create proxy class by using visual studio IDE. So, first create a Console Base application as MagicMathClient and right click over the project in the solution explorer. Here, choose the Add Service Reference option, then the following dialog box, will ask you to enter the address of WCF service. Just enter the address and Hit Go button. It will expose the metadata reside in the WCF reference as following:
In the bottom of that dialog box, you can set the name of this proxy class for instance ServiceReference1. Finally, click the OK button, once this done, you will notice that a service reference is added under the solution explorer as the following:
Now, import the WCF ServiceReference1 namespace and place this following code in the program.cs class as the following;
[c]
using System;
namespace MagicMathClient
{
class Program
{
static void Main(string[] args)
{
using (MathClient obj=new MathClient())
{
Console.Write("Enter the Value:");
double x = double.Parse(Console.ReadLine());
Console.ReadLine();
}
}
}
}
Here, we are just instantiating the class MathLib defined in the WCF Service and consuming its methods. Finally, it's time to run the project. The output would be:
One thing you need to always remember, before running this application, first start the hosted service application, otherwise this application throws an exception.
Summary
Learn Digital Forensics
As we have stated, WCF is platform independent like ASP.NET web services but offer a similar features like Remoting, XML Web services and MSMQ. In this article, we learned how to use WCF between a client and server application and explore the various aspects of WCF such service contract, data contract, its advantage over inherent technology ,how to configure the service and finally, how to consume its functionality into a client console based application. In the next article of this series, we will illustrate the HTTP, TCP, MSMQ binding implementation and other features such as security, session management