Telavian
Telavian

Reputation: 3832

VS2010 Advantages of Add Service Reference over direct ClientBase<>

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.

Upvotes: 1

Views: 2990

Answers (1)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59973

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:

Upvotes: 5

Related Questions