WCF the Manual Way… the Right Way
Visual Studio 2008 as well as the .NET 3.0 extensions for Visual Studio 2005 add several new templates that allow you to quickly create .NET 3.0 items such as WPF forms, Workflows, and WCF services. We’ve 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 would think that these are equally great; or would you? Unfortunately, creating WCF projects or project items come with more baggage than you can imagine. They also don’t exactly promote the best practices I feel should be considered 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.
Sometimes, when tools do a lot of work for you, the results may not exactly be the best. In my humble opinion, such is the case with WCF. Now, let me start by saying that 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 being 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 toward 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 more 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 and it’s not even that hard to do.
I don’t want to come across as overly critical or make this sound like a bashing session, because it is definitely not. I merely want to point out a few shortcomings and show you how to overcome them. In doing so, I think you will get a good understanding on how the individual pieces of WCF-based applications work and fit together as well as my recommended assembly separation for WCF-based applications.
I also want to mention that this article assumes some basic knowledge of WCF services and the elements that comprise them. Not too much, just a bit.
So what Exactly Is Wrong?
Anyone who knows me knows that I love getting into political conversations with friends and otherwise (sometimes friends become otherwise because of this); and one of my pet peeves is when politicians criticize and destroy current policy without offering a better solution of how things should be done. Well I won’t fall into the very practices I despise. Instead, I will 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. So look out Congress! Here I come.
The current WCF templates in Visual Studio as well as the Add Service Reference tool suffer from one major problem: they do too much. I think developers would have benefited a lot more from many templates that do less, instead of a few that do a lot. Creating service items automatically create both the contract and the service; two things that should be in separate projects (for reasons I will explain later). Creating service web projects also creates these two items as well as the service host; which I also feel should be its own assembly. Both situations also contribute to the configuration file. While this is good for educational purposes, it can also clutter up an existing clean configuration file. Templates that I feel are missing include ones 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. As far as the Add Service Reference tool, it also 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 using the WCF templates and tools in order to develop a simple service and client. I’ll later analyze what I see wrong with the results and provide alternatives.
By: Miguel Castro
Miguel is an architect with IDesign who specializes in architecture consulting and building .NET solutions. He is a Microsoft MVP and INETA speaker and has been a software developer for over 22 years. With a Microsoft background that goes all the way back to VB 1.0 (and QuickBasic in fact), Miguel jumped on .NET as soon as the first public Beta was released and has provided .NET solutions for clients around the country in a variety of industries. He considers himself to be a .NET Developer and Architect and has equal love for both VB and C#, and no tolerance for language bigotry. He’s spoken at numerous user groups around the country as well as developer conferences.
He’s the author of the CodeBreeze code-generator, which among things can be found on his Web site:
Miguel currently lives in Lincoln Park, NJ with his wife Elena and his daughter Victoria.
Service orientation is not a replacement for object orientation or component orientation. Just like component-oriented architectures build on the principles of object-oriented programming, service-oriented designs use concepts of component design and bring other concepts on top of that.