o far, this Windows Communication Foundation (WCF, formerly known as Indigo) series has provided a primer, and discussed security and transactions. The final pillar in the tripod that supports software-as-a-service is reliability, with which you can achieve the secure, transactable, and reliable services that WCF promises.
But what is reliability, and why is it so important? First, reliability is a key differentiating factor between software systems that cost millions of dollars to develop or own, and those that don’t. Consider a financial scenario for portfolio management as an example. You have a number of assets at a brokerage; say 100 shares of Stock A and 10 shares of Stock B. You think stock B is likely to do well, but don’t have enough funds to buy it. So you issue a command to sell your 100 shares of stock A, estimating that with the proceeds you will easily be able to afford 10 shares of stock B. But rather than wait for the transaction to complete, you also issue a command to buy 10 shares of stock B. Deals don’t need to be realized for 3 days after the trade, so by the time you get the money for selling stock A you can afford stock B.
But what if the brokerage system was unreliable, and your command to sell 100 shares of stock A was never received?and you didn’t bother to check for a confirmation? For example, what if you issued the trade via a cell phone and then hung up? And then suppose stock A crashed the next day. You’d be up the proverbial creek, because you’d still need to pay for stock B within 3 days, and the funds to do so are no longer there.
This would clearly be an intolerable situation, and no brokerage that did this on a regular basis would stay in business for long. So how do you fix it?
You could grow a system that has in-built checking of messages and offers a communication protocol between the client and the trading system that ensures everything is checked, counter checked, confirmed, and updated. But then you’d be in the same boat as everyone else?forced to develop a large-scale system expensive to develop, maintain, and run.
Alternatively, you could start by looking at standards. When it comes to SOA, the standard for reliability is WS-Reliability; and fortunately, reliability has never been easier to implement than by using WCF.
Author’s Note: The previous articles in this series were based on the beta of Visual Studio.NET 2005 and the September preview of WCF. Since then, VS.NET 2005 has been officially released, and a new preview version of WinFX/WCF, called simply “the December CTP,” is now available?and it’s much easier to install and use. I’ve used this version to develop the code in this article. |
Setting up a Windows Communication Foundation Development System
Microsoft has provided a convenient download page that gives you links to all the necessary downloads.
![]() | ? |
Figure 1. New WinFX Service: The figure shows the dialog you’ll use to create a new WinFX Service using Visual Studio.NET. |
From that page, you’ll need to download and install the following:
- WinFX RTC (run-time components)
- The Windows SDK
- The Visual Studio.NET extensions for WinFX
IMPORTANT: Install the items in the sequence shown above to make sure that everything works. Also, note that the WinFX extensions balk if you do not have the MSDN documentation installed with your development environment, so if you don’t have the documentation installed, install it before you install the WinFX beta. Finally, make sure that you launch the development environment and start up the help system, configuring it for “local access” before you install them. |
After you’ve followed the instructions above and downloaded and installed WinFX, you can verify that the installation works by launching Visual Studio.NET and issuing a File->New Website command. The New Web Site dialog will appear. The first option will be to create a “New WinFX Service” (see Figure 1). Go ahead and select that option. That sets up everything for you and you’ll be ready to build a WCF service.
Implementing Reliability in WCF
WCF uses SOAP reliable messaging to provide end-to-end message transfer reliability between service endpoints in your system, and uses WS-Reliability to provide a layer of reliability between potentially unreliable or intermittently-connected networks. A number of different options are available for defining the specific reliability characteristics you require, such as exactly-once messaging, where a message is guaranteed to be delivered once and once only. Consider the brokerage situation discussed above: If the “sell” message were delivered more than once, you would be oversold and would end up shorting the stock.
When using WCF SOAP reliable messaging the reliability provided is end-to-end, regardless of the number of intermediaries between the endpoints. This is very important because most real-world systems will have a number of intermediaries between systems, such as HTTP proxies.
The service you created earlier won’t run yet, because its web.config file isn’t properly configured (this is beta software after all). To get the example temperature conversion service used for this article series up and running, you’ll need to perform the following steps:
- In your App_Code folder, you’ll have a file called Service.cs. Change the code in that file to look like the code below, which defines the temperature conversion service.
- Next, change your Service.svc file to point to this class. Open the Service.svc file, and change the Class=”” attribute to:
- Finally, you’ll need to edit your web.config file to initialize the System.ServiceModel that is at the heart of WCF. You can see the web.config that configures the TemperatureServiceClass in the code below.
// Code for a Temperature Conversion WinFX Service 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; } } }
Class="Devx.Indigo.Samples.TemperatureService"
This attribute maps the service to the TemperatureService class shown above, which is in the Devx.Indigo.Samples namespace.
Author’s Note: If you’ve been following this series or if you have been working on other samples that use earlier versions of WCF, the schema has changed slightly, so older web.config files are likely to give runtime errors. I’ve highlighted the parts in bold in the following code where you need to be careful. |
name="TemperatureServiceBehavior" returnUnknownExceptionsAsFaults="True"> name="Binding1"/>
Configuring Your Service for Reliability
Now that you have a service up and running, making it reliable is a snap. There are three standard bindings that you can use in WCF to ensure reliability in your services. The first is wsProfileBinding, which provides a secure, reliable, interoperable binding (based on the WS-I basic profile and WS-Reliability) suitable for simplex (i.e. one-way) service contracts. So, if you are calling a service to issue a command such as the trade scenario that was envisioned earlier, this is the way to go.
It’s very simple to set it up?though unfortunately the MSDN documentation hasn’t caught up with the schema in the December CTP. So, for wsProfileBinding, you no longer use a
It’s not a good idea to rely on the MSDN documentation at the moment, because that needs to be refreshed, which will happen before release. However you’ll find a huge suite of examples with the WinFX download and Windows SDK that will help you to figure out how to use the web.config properly to ensure reliability.
Now that your service is configured for basic simplex reliability you can test it by generating a client proxy and application. Full instructions on how to do that are in the primer article.
When you’ve done that, the best way to test the services is to run the client on one machine and the service on another. Call the service from the client, and ensure that it works. Then, to simulate an intermittent or problematic connection, yank the network cable from the client and call the service again. Nothing will happen, and the thread of execution will block. But plug the network cable back in, and when your network comes back online the service call will complete!
So, now you’ve seen how to install the WCF December CTP and build a simple service, making it send reliable messages using the Windows Communication Foundation. Best of all, the reliability characteristics are implemented for you by the framework; you don’t have to do any packet monitoring or include other ugly code. This simplicity is what makes WCF a winner, and is a major factor in why WCF will be a very important developer tool for many years to come.
Reliability is essential in any business application. Using built-in WCF features, you can ensure reliability as well as the security and transactability discussed earlier in this article series. Together, these provide you with a framework to turn your software services into enterprise-class applications. Most important, you can eliminate custom coding for reliability, letting you focus on your business logic code, and not on the infrastructure and “plumbing” required by enterprise-grade systems.
The classes in the System.ServiceModel namespace do most of the work for you;?you simply need to adjust the environment using the web.config file. Remember, this is still beta technology, so it’s a little rough around the edges, particularly in the mismatch between the currently documented web.config schema and the one that works; but it is well worth investing some time and effort into to bring your skill set to the next level. The samples that come with the SDK are invaluable in this regard.