Avoiding Visual Studio Service References

By Jeffrey Polk at January 29, 2010 05:48
Filed Under: .Net, WCF

I've been using WCF in practice for about a year.  My least favorite part of the technology is Visual Studio's service reference.  I think this for several reasons:

 

  • The generated source files are difficult to maintain
  • Creating the service reference is sometimes unstable
  • The increased maintenance each time the service changes

 

Generated source files

Creating a service reference in Visual Studio generates a lot of files.  My sample project contains a simple service with a single method.  The service reference for this generates 10 new files! 

 

 

I find it cumbersome to maintain these files as the service changes.  Files can be added and made obsolete with what seems like trivial changes made to the service.  This is a challenge keeping straight in source control.

 

The unstable service reference

 

Too many times I've updated the service reference and my code doesn't compile.  After some digging, I find the service reference now contains this code:

 

 

Where did my methods and types go?  I remove the reference and try again.  Same result.  Where do I go from here?  I've never found the root cause of this behavior.  There is no feedback from the reference tool to tell me something's wrong.  Sometimes I can get past this by deselecting the "reuse types" option before creating the reference.  Other times, I cannot.  This is very frustrating when it happens.

 

Increased maintenance when service changes

Let's say I have a service with one method.  I also have three projects in my solution which consume this service.  Now i decide to add a second method.  Before using this new method in any of my other projects, I must update the service reference.  Three times.  What if I forget to do this?  Well, in this example forgetting just means the new method won't be available.  But what if I change my service's method to accept a new parameter?  Now I have a bigger issue.  My project with the old reference will still compile without any errors.  But at run time, an error will occur.  I want to catch these type of issues when I build.  Every interface change made to the service must be propogated n times.  There must be a better way.

 

Avoid the service reference completely!

Putting all these factors together forced me to find another way.  Something cleaner, less error-prone and easier to maintain.  The pattern is to break the service into separate assemblies, each with its own purpose.  If you create a new WCF Service Application using the project template, Visual Studio will provide the following:

 

 

This is already starting you down a wrong path for a couple reasons.  First, it combines the service and the interface into the same application.  Anything that consumes this service must recreate this interface.  That's part of what adding a service reference does for you.  But, that means that a change to this interface requires all those copies of it to be updated.  Another misstep in this template is that it defines a type in the same file as the interface.  Again, it makes it very tough to share the type if it is bundled into the service application.  I should state that I don't fault MS for putting everything into the project this way.  It's a template afterall, and they needed to include all the needed pieces to make it work. 

 

Clear these muddy waters by separating all three pieces into separate assemblies. 

  • DTO - defines types used by the service
  • Client - defines the interface and client-side proxy class
  • Service - the actual serivce which references both the DTO and Client

 

Now the only thing in the service application is the actual implementation of the service.  The interface and types are in separate projects which can now be shared among our service and consumer applications.  Our solution looks like this:

 

 

In addition to defining the interface in the PS.Client project, I also create a proxy class to be reused by all consumer applications.  This is part of what Visual Studio does for you when you add a service reference.  If you look a little deeper into what is generated, you will see it is fairly straightforward.  In my example, the AccountService service contains one method, which returns an Account object.  The proxy class is very simple:

 

 

Now that everything is separated, I am promoting code reuse - the service and consumer applications share both the types and the interface.  All consumer applications share the same proxy class.  Earlier I mentioned how using a service reference could lead to run time errors if the service interface changes.  In the new setup, this is not possible.  If a new parameter is added to the GetAccount service method, all consumer applications will fail to compile until they are changed.  I am also not relying on a Visual Studio utility to get my service and consumers talking.  If I want to add my service in a new consumer application, all I need to do is add a "normal" reference to the PS.Client & PS.DTO projects and add an endpoint to my config file.  While there is a little more set up early on in the process, I've found this pattern to be beneficial in the long run.  It has made the services very consistent from a structure standpoint, and it has been a lot easier to maintain as changes are made.

 

Download the source code:

WCFReference.zip (242.38 kb)

 

 

Comments

2/1/2010 12:53:54 AM #

trackback

Avoiding Visual Studio Service References

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com |

3/1/2010 3:55:48 PM #

facebook chips

i'm quite often roaming across the net nearly all of the night and so I have a tendency to read quite a lot, which unfortunately isnt usually a good factor as several of the pages I find are constructed of useless crap copied from some other web sites a thousand times, nevertheless I have to give you credit this webpage is really quite informative and delivers some genuine material, so kudos for splitting the trends of only replicating other people's blogs and forums, in case you ever wanna have fun with playing a couple of hands of myspace poker together with me just hit me up - you have my email address Smile

facebook chips United States |

3/3/2010 10:13:45 AM #

Kindle best buy

Thats a great article. I'm looking for more information on this and will surely keep checking back at this blog. Thanks.

Kindle best buy United States |

3/5/2010 6:46:39 PM #

Robbin Veiga

Though I would've loved it much more if you added a relevant video or at least pictures to back up the explanation, I still thought that your write-up quite helpful. It's usually hard to make a complicated matter seem very easy. I enjoy your weblog and will sign up to your feed so I will not miss anything. Fantastic content

Robbin Veiga United States |

3/6/2010 10:33:02 AM #

ELC

A thoughtful opinion and ideas I will use on my blog. You've obviously spent a lot of time on this. Well done!

ELC United Kingdom |

3/6/2010 12:28:07 PM #

Daniel Millions

Wasn't sure where to post this but I would like to add your site to one of my web directories. Please contact me at your convience because I only list webistes with the webmasters consent - thank you.

Daniel Millions United States |

3/6/2010 12:45:42 PM #

Bari Preas

Well said, you just earned another subscriber

Bari Preas United States |

3/6/2010 1:58:57 PM #

moon in my room

Thanks for taking this opportunity to talk about this, I feel strongly about it and I benefit from learning about this subject. If possible, as you gain data, please update this blog with new information. I have found it extremely useful.

moon in my room United States |

3/7/2010 1:45:11 AM #

stock prices

Great blog it's not often that I comment but I felt you deserve it.

stock prices United States |

3/7/2010 6:47:20 PM #

reverse phone lookup

Hello everyone. I was just surfing the Internet for fun when I found your website. Fabulous post. Thanks a ton for sharing your knowledge! It is nice to know that some people still put in an effort into handling their websites. I'll be sure to look in again real soon.

reverse phone lookup United States |

3/9/2010 3:29:43 AM #

mortgage loan modification

Hello, I have browsed most of your posts. This page iswhere I got the most useful information for my research. Thanks for posting, maybe we can see more on this. I also like this layout can you tell me where to get it  

mortgage loan modification United States |

3/9/2010 6:33:31 PM #

Lyle Tabb

Great site, where did you come up with the information in this blog? Im pleased I found it though, ill be checking back soon to see what other articles you have.

Lyle Tabb United States |

3/12/2010 8:37:00 AM #

Water Filters

Interesting articles. I favorited your page. I'm searching for quality sites that have visitors that would find value from a popular blog I maintain on water filters. Let me know if you would like to trade some posts or reviews. I think we could both benefit.

Water Filters United States |

3/14/2010 2:04:10 PM #

Reisen Tuerkei

Hiya there, I encountered your blog via Google and your position looks extremely occupying for me.

Reisen Tuerkei Austria |

3/14/2010 9:38:49 PM #

Dave

Useful. I was having a scan around this sort of area &  although penny shares sound "cheap" beginners often think that they are more suited to people with less money. I would highlight that they are just as suitable for people with high net wealth because the truth is we need to look at percentage gains. Also - having someone with little money especially who is new to the markets start on penny shares is probably a bad idea (they cant be expeted to learn to trade on a stock that has such little accurate information out there!). Ive added this to my Mixx bookmarks.

Dave Puerto Rico |

Comments are closed

About the author

software architect, husband, golfer, gun owner, dog owner, first baseman, hunter, hiker, beer drinker, right-leaning independent and quiet thinker

Month List