How to add a interface to auto-generated classes

by Kenny Eliasson 17. November 2010 16:13

Even though I really dont like working with webservices or WCF, sometimes you got to swallow the bitter pill and create some.

There are alot of different ways to work with webservices, you can add them as references, you can use WSDL and generate proxy classes or maybe call them directly via HttpWebRequest (why you would do that I dont know :)).

I usually ended up with generating them and making a "facade"-class around the calls to the webservice.

But sometimes you don't really feel the need for yet another class just to "hide" the webservice. What I want to accomplish is slapping a interface on top of my auto-generated proxy classes when I add the service as a reference. You can't add the interface on the proxy class since that change would be lost the next time you update the service reference.

What I came up with is that you can create a partial class


namespace MyProject.Services
{
 public partial class MyService : IMySerivce
 { }
}


Notice that you must have the same namespace as your webservice.

This is one of the few reasons where I had to use partial classes.

This solution is kinda quick and dirty and I would reconsider to use a facade instead if you need to add logic for logging etc.

Tags:
Categories: C#

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading