1

I have noticed that when "Add Service Reference" is used in VS2010 then a ton of files are created. One of the files is Reference.cs which creates a ClientBase for the service contract.

Thus, I was wondering is there any advantage to the slew of extra files VS creates or can I just use a ClientBase myself and skip the "Add Service Reference" option.

I should note that in my case by sides are under my control.

Telavian
  • 3,752
  • 6
  • 36
  • 60

1 Answers1

5

Here's an overview of the pros and cons with the different approaches from my point of view:

Using the Visual Studio generated service proxies

Pros

  • Minimal effort required to get started
  • Ability to quickly update the proxy whenever the service contract changes
  • No custom code to maintain

Cons

  • Makes consumers difficult to unit test with any of the popular isolation frameworks for .NET, such as Rhino Mocks or Moq, due to high coupling with the WCF infrastructure through the ClientBase<TChannel> class
  • Unnecessary extra files are created

Using the WCF Channels API

Pros

  • High testability with any isolation framework thanks to the decoupling provided by the IChannelFactory<TChannel> interface
  • More control over how services are invoked

Cons

  • Takes some initial effort in order to get started
  • Some custom code to maintain

To summarize using proxies provides less friction when consuming WCF services while giving up some control and testability. Using the Channel API requires more code in exchange for a higher degree of flexibility, which comes in handy especially if you want to unit test components separately from the WCF services they communicate with.

Related resources:

Community
  • 1
  • 1
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • @Enrico: I have had no trouble unit testing consumers. I simply inject a mock object that implements the service contract. What are you referring to? – John Saunders Feb 22 '11 at 20:16
  • @John That will work as long as you don't call any WCF-specific infrastructure methods to release the underlying channel like the [ICommunicationObject.Close](http://msdn.microsoft.com/en-us/library/ms195520.aspx) or [ICommunicationObject.Abort](http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.abort.aspx) methods since they are not part of the service contract. – Enrico Campidoglio Feb 22 '11 at 21:05
  • @Enrico: that's correct. But even then, all I need is a mock class that implements both interfaces. No difficulty in unit testing. Again, what problems have you found in testing? – John Saunders Feb 22 '11 at 21:45
  • @John My goal is to have consumers depend upon a single interface that exposes both the service operations and provides the infrastructure methods from [ICommunicationObject](http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.aspx). The only way I've found to achieve that is by using the IChannelFactory interface. That way I can easily generate fake implementors with an isolation framework without having to manually create test doubles. If you have an alternative approach to solve this problem I would definitely be interested in it. – Enrico Campidoglio Feb 22 '11 at 22:08
  • @Enrico: I haven't tried this using an isolation framework. Perhaps they are unable to create mock classes which implement multiple interfaces? If so, perhaps create your own interface inheriting both, then have the framework create a mock with that interface? In any case, there's certainly no _general_ problem with unit testing; only with unit testing using the methodology you happen to prefer. – John Saunders Feb 22 '11 at 22:16
  • @John Agreed, it's definitely an evaluation taken from my perspective. I've clarified it in my answer. – Enrico Campidoglio Feb 22 '11 at 23:11
  • @Enrico: thanks, but I also think you should elaborate on this requirement of yours. Last year, I did a large amount of unit testing of WCF clients and services (and services which were clients), and found no problem testing clients. The issue seems specific to the methodology you are using, so if you could elaborate on that methodology, I'd appreciate it. – John Saunders Feb 22 '11 at 23:46