Published on System iNetwork (http://systeminetwork.com)
Getting Started with WCF
By chris.maxcer
Created Oct 20 2009 - 16:19

By:
Craig Pelkie [1]

One of the major development trends, across all platforms, is development and usage of web services. Web services is understood to be a platform-neutral technique that lets applications communicate with each other across a network, using standardized techniques to issue requests and exchange data. From the beginning, the .NET Framework and Visual Studio have provided support for developing web services, along with the Microsoft-specific Remoting technology, which provides optimized communications between .NET Framework enabled devices.

With the release of the .NET Framework 3.0, Microsoft introduced a new technology named Windows Communication Foundation (WCF). WCF is a "wrapper" technology that includes support for web services and remoting, but it is much more than just another version of web services. In addition to web services and remoting, WCF provides support for MSMQ (message queuing) and named pipes (which can be used to provide process-to-process communications within a machine). The importance of WCF is that, at the application level, the code that you write can look the same, regardless of the underlying communications protocol. In addition to making it easier to switch protocols as needed, you can develop applications by learning only the WCF API, rather than separate APIs that are specific to each protocol.

Although it may seem that WCF is another complex .NET feature that you want to avoid as long as possible, working with WCF can potentially simplify application development. By design, WCF services have no user interface, so you are forced to separate business logic from presentation logic. Once you've developed a WCF service, you can consume it from any number of different client applications. For example, you can develop ASP.NET applications that work with a service. You can also develop Windows Forms or Windows Presentation Foundation (WPF) applications for rich-client environments. Because you've moved all of the database access code into the WCF service layer, a WinForms application becomes radically simplified and its deployment issues become much simpler (for example, you can have a WinForms application that accesses the IBM i through a WCF service; the PC hosting the WinForms application will not need to have the IBM System i Access for Windows OLE DB or .NET Providers installed). WCF services are also the basis of communications within the Silverlight Rich Internet Application (RIA) plugin, which provides an alternative environment for browser-based applications. WCF services can also be accessed by applications running on other platforms using non-.NET applications; for example, Java and PHP applications can readily work with WCF web services.

Fortunately, getting started with WCF is easy, if you use the tooling in Visual Studio. In the following example, I show how to develop and test a WCF service in Visual Studio 2008 Professional Edition on a PC that has the .NET Framework 3.0 and 3.5 installed (in addition to .NET Framework 2.0).

From start to finish, this example will take 15 minutes or less, even if this is your very first exposure to WCF.

Getting Started with a WCF Service Library Project

To get started, create a new project in Visual Studio. Figure 1 shows where the WCF Service Library template is located for both C# (wcf0001c [2]) and Visual Basic (wcf0001v [3]). Once you've selected the template, Visual Studio generates a number of files and references for the project, as shown in the Solution Explorer (C#: wcf0002c [4], Visual Basic: wcf0002v) [5].

Figure 3 (C#: wcf0003c [6], Visual Basic: wcf0003v [7]) show the IService1 interface that is generated. This interface is used to define a service contract and its operation contracts. You can see that the ServiceContract attribute is applied to the interface statement and the OperationContract interface is applied to each of the functions defined within the interface. The ServiceContract and OperationContract attributes are used to define the name of the service that will be accessible through WCF, along with the functions that are provided by the service. If you define a function in the interface without a corresponding OperationContract attribute, that function will not be exposed through the service. You can easily add more functions to the interface by simply typing the OperationContract attribute followed by the method signature. Note that in an interface, there is no executable code, the interface is used simply to define the methods that will be implemented in the class that implements the interface.

The example code also includes a class named CompositeType. There is nothing special about that name, it is simply used to indicate that the example shows how you can define a class that is used to return data. The CompositeType class is decorated with the DataContract attribute. It contains two properties that are decorated with DataMember attributes. The purpose of those attributes is to enable WCF to determine that the class itself and some (or all) of its members are to be used in the service. An important rule to keep in mind when designing DataMember members is that the member must be serializable; at run-time, the value is converted to an XML representation for a web service. To determine if a member can be serialized, you can look at the MSDN .NET Framework documentation. If a class is decorated with the Serializable attribute, it can be used as a DataMember.

Figure 4 (C#: wcf0004c [8], Visual Basic: wcf0004v [9]) show an example program that is generated for the WCF service. The class name is Service1 and it implements the IService1 interface. Unlike the interface, the code in the class is not decorated with any attributes. The class simply provides implementations of all of the methods defined in the interface. As you can see in the figure, the code that is implemented is trivial. You are expected to provide your own more meaningful methods that perform useful functions. For example, the GetData method might be coded to run an SQL SELECT statement to get data from the IBM i database and return a result.

Run the WCF Web Service

At this point, even without having written any code, you can run the web service. Simply press the F5 key in Visual Studio to start the WCF project running. The WCF Test Client program shown in Figure 5 opens (wcf005 [10]). To use the program, double-click the method name that you want to test (GetData or GetDataUsingDataContract), then enter the input values to the method in the Request panel. Click the Invoke button; the WCF service is invoked and the response is displayed.

The WCF Test Client is a very handy tool to have available as you define and develop a WCF service. At this point, you have not invested any time or effort into client applications. You can concentrate on getting the service methods, input parameters and output correct before moving on to client applications.

Create a Console Tester Project

To use the WCF service, you will have to write some code. One of the quickest, least burdensome ways to test anything in .NET is to develop a Console application. There is essentially no user interface in a Console application, old-timers will instantly recognize Console applications as "DOS box" applications. Long live character-based user interfaces!

To get started, right-click the solution name in the Solution Explorer and add a Console Application to the solution. Figure 6 (C#: wcf0006c [11], Visual Basic: wcf0006v [12]) shows where the Visual Studio Console Application template is located. As soon as the Console project is added to the solution, right-click the newly added project and select the Add Reference item. Add a reference to System.ServiceModel, as shown in Figure 7 (wcf0007 [13]).

You now need to add a reference to the WCF project. Right-click the Console Application project again and select the Add Service Reference item, as shown in Figure 8 (C#: wcf0008c [14], Visual Basic: wcf0008v [15]). The Add Service Reference dialog shown in Figure 9 (wcf0009 [16]) is displayed. Click the Discover button to "discover" the WCF project in the solution. When the project is shown in the Services panel (left side of the display), expand it until you can select the Service1 interface, at which point the Operations will be listed. You can then click the OK button. When you return from the dialog, you can click the Show All Files button in the Solution Explorer to see the Service References files that were added to your project, as shown in Figure 10 (C#: wcf0010c [17], Visual Basic: wcf0010v [18]).

Write Some Code

At this point, you're ready to write the code to access the WCF service. Figure 11 (C#: wcf0011c [19], Visual Basic: wcf0011v [20]) shows the code needed to create an instance of the Service1Client and to invoke the GetData method. The value returned from GetData is displayed on the Console.

As you can see, there is not a lot of code that you need to write in a client application to invoke a WCF service. Granted, there is a lot of code (while, an awful lot of code) that was generated behind the scenes, that is invoked when you use the WCF service. But a lot of the reason to use the .NET Framework and Visual Studio is to take advantage of the framework. If you want to get involved with the lower-level details, you can, but the intention of the tools is to make it easy to work at the business level.

Follow Up

There is obviously much more to WCF services than is shown in this article. For example, you need to deploy the service to an IIS server to make it web-accessible.

But as you've seen, you can develop a WCF service and a corresponding client application within Visual Studio. In my experience, one of the stumbling blocks with getting started with WCF is the onslaught of terminology that you're confronted with. Using the steps shown in this article, you can get started right away and loop back around to pick up the additional terms and conditions you'll need to know about to get this going in a production environment.

© 2010 Penton Media, Inc.

Source URL: http://systeminetwork.com/article/getting-started-wcf

Links:
[1] http://systeminetwork.com/author/craig-pelkie
[2] http://systeminetwork.com/files/wcf0001c.jpg
[3] http://systeminetwork.com/files/wcf0001v.jpg
[4] http://systeminetwork.com/files/wcf0002c.jpg
[5] http://systeminetwork.com/files/wcf0002v.jpg
[6] http://systeminetwork.com/files/wcf0003c.txt
[7] http://systeminetwork.com/files/wcf0003v.txt
[8] http://systeminetwork.com/files/wcf0004c.txt
[9] http://systeminetwork.com/files/wcf0004v.txt
[10] http://systeminetwork.com/files/wcf0005.jpg
[11] http://systeminetwork.com/files/wcf0006c.jpg
[12] http://systeminetwork.com/files/wcf0006v.jpg
[13] http://systeminetwork.com/files/wcf0007.jpg
[14] http://systeminetwork.com/files/wcf0008c.jpg
[15] http://systeminetwork.com/files/wcf0008v.jpg
[16] http://systeminetwork.com/files/wcf0009.jpg
[17] http://systeminetwork.com/files/wcf0010c.jpg
[18] http://systeminetwork.com/files/wcf0010v.jpg
[19] http://systeminetwork.com/files/wcf0011c.txt
[20] http://systeminetwork.com/files/wcf0011v.txt