isual Studio 2008 and the .NET 3.0 extensions for Visual Studio 2005 add several new templates that simplify creating .NET 3.0 items such as WPF forms, Workflows, and WCF services. Developers have used templates such as these since the beginning of time to create traditional application elements such as Windows Forms, Web Forms, and User Controls—so you might think that these templates are equally great. Unfortunately, creating WCF projects or project items come with more baggage than you can imagine. They also don't exactly promote some best practices when designing WCF services.
So if you don't use the built-in templates to create your services, what do you use? Well keep reading and I'll show you how to create everything you need manually with ease while maintaining good design and coding practices.
WCF is probably one of the absolutely coolest technologies to come out of Microsoft. I think the unified model toward developing message-based applications and service-oriented applications is long overdue in our industry, and I'm glad to see that it came from Microsoft. That said, I'm not too crazy about the templates that come with Visual Studio 2008 or the 3.0 extensions for Visual Studio 2005. I feel Microsoft could have done a better job with code separation and the enforcement of good practices—never mind best practices.
Another area where I see developers doing things automatically and falling a bit short is in referencing services from client application. When developers used mainly web services, the choices were limited, but such is no longer the case. Adding service references automatically may not be the best way to write service consumers.
Fortunately you can easily solve these problems by creating all your service components manually; it's not even that hard to do. This article assumes you have some basic knowledge of WCF services and the elements that comprise them; not too much, just a bit.
The Problem with WCF Templates
I love getting into political conversations, and one of my pet peeves is when politicians criticize and destroy current policy without offering a better solution for how things should be done. I won't fall into that trap; instead, I'll tell you exactly what I feel is wrong with the WCF-related project and item templates that come with Visual Studio, and then I'll tell you how I think you can overcome these problems.
The current WCF templates in Visual Studio as well as the "Add Service Reference" tool suffer from one major problem: they do too much. Developers would have benefited a lot more from having more templates that do less. When you add a service, Visual Studio automatically creates both the contract and the service in your project—two things that should be in separate projects (for reasons I will explain later). Creating service web projects not only creates these two items but also the service host—which again, I feel should be in its own assembly. Both situations also add to the configuration file. While this is good for educational purposes, it can also clutter up an existing clean configuration file.
Instead, Visual Studio should include templates for individually creating all these elements. In addition, I'd like to see a project template to create a self-host project that's not IIS-based. The Add Service Reference tool creates too much for you, and does not promote good code reuse. Not only does it create proxies for you, but it adds an immense amount of configuration information.
To illustrate what I mean, I'll walk you through a project using the WCF templates and tools to develop a simple service and client. Then I'll analyze what's wrong with the results and provide alternatives.
|Editor's Note: This article was first published in the September/October 2008 issue of CoDe Magazine, and is reprinted here by permission.|