Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The Windows Communication Foundation: A Primer

The Windows Communication Foundation (WCF)—formerly known as Indigo—is Microsoft's new connected systems platform for Windows. This is the first in a WCF article series covering everything from first principles and "Hello, World!" to building fully connected applications.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

he WCF is an incremental, yet evolutionary technology that brings all the formerly distinct and separate Microsoft connectivity technologies together under a single umbrella within the System.ServiceModel namespace. Included in WCF are Web services (ASMX), the Web service Extensions (WS*), Microsoft Message Queuing (MSMQ), Enterprise Services, COM+, and .NET Remoting.

Having a single namespace that subsumes all of these into a coherent package is enormously useful, and makes designing, developing, and deploying applications that require connectivity far simpler. With WCF you won't have to choose between implementations in a variety of different namespaces and coding types to create a connected application. Whether your application connects via loosely coupled Web services, or tightly coupled Enterprise Services, the coding model will be consistent and the transition between different communication types will be much smoother—because they will all be using the same programming namespace.

Using the WCF
WCF follows the "software as a service" model, where all units of functionality are defined as services. Rather than concentrating on how the communications work, developers need to focus on the service locations, how the services talk to each other, and how to describe what they do. So, for any service, you need to know the answer to these three groups of questions:

  1. The Service Address. Where is the service? Is it on the internet, on a machine on my network, or on the same machine as my process?
  2. The Service Binding. How do I talk to it? Do I use SOAP? MSMQ?
  3. The Service Contract. What does it do for me? What kind of data should I expect to pass to it, and what does it return?
If you're familiar with Web services, you probably already understand these three aspects in terms WSDL. And you'd be right. WCF takes this popular and successful methodology for defining how services work and extends it to work with the other forms of communication: Microsoft Message Queuing (MSMQ), Enterprise Services, COM+, and .NET Remoting.

Author's Note: This article is based on the September Community Technical Preview versions of WCF, which is part of the WinFX SDK and runtime. Installing these broke my installation of Visual Studio.NET 2005 CTP; At present there seems to be no way of using VS.NET 2005 CTP for Indigo. Bear in mind that this is beta software, so the problem should be fixed quickly. Therefore, this article is based entirely on using the command line tools to build and configure the WCF service. You don't need VS.NET.

Your First WCF Application: Creating the Service
If you aren't already familiar with the WCF terminology, such as binding, contracts, addressing, and the like, don't worry; such terms are best learned by example. For the rest of this article, you'll build your first Indigo application, and then you'll be off and running. To build the application, there are a few steps that you'll have to follow.

Before starting, create a physical directory on your hard drive to contain the application at C:\tconvert and then map that to an IIS virtual directory called "Temperatures." Make sure that the ASPNET process has access to this directory if you are using Windows Server 2003.

The first step is to create the Windows Communication Foundation service contract. This is basically an interface that is marked up to show that it is a service contract. You can see it in Listing 1.

Note the markup through attribution. The ServiceContract() attribute indicates the ITemperatures interface will be a service contract in WCF, and the OperationContract() indicates that ftoc and ctof will be defined as operations in the contract. So far, the process is simple.

The second step expands on this to create the code that implements the interface defined in Listing 1. The following code creates the Service class.

using System; using System.ServiceModel; namespace Devx.Indigo.Samples { [ServiceContract()] public interface ITemperatures { [OperationContract] double ftoc(double n1); [OperationContract] double ctof(double n1); } public class TemperatureService : ITemperatures { public double ftoc(double f) { double dReturn = 0.0; dReturn = ((f - 32) * 5) / 9;   return dReturn; } public double ctof(double c) { double dReturn = 0.0; dReturn = ((c + 32) * 9) / 5;   return dReturn; } } }

You compile this code using the following command, issued on a single line from a command prompt. You'll need a PATH set up to the .NET framework if you don't already have it.

csc /r:System.ServiceModel.dll /out:TemperatureService.dll /t:library TemperatureService.cs

Create a BIN subdirectory under tconvert and copy the resulting DLL file to it.

Next, you need a web.config file (see Listing 2). The web.config file contains all the information that WCF needs to hold everything together. It's worth looking over in some detail. Note that the syntax for this file seems to change drastically with every release, and even differs between Microsoft's online documentation and the documentation that comes with the download. That's the fun of living on the beta edge.

It's a good idea to get to know the web.config file in Listing 2 well when writing WCF apps, particularly if you aren't using Visual Studio .NET. The <system.ServiceModel> tag holds all the information for a WCF service.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date