BizTalk Utilities CV ,   Jobs ,   Code library
Go to the front page to continue learning about XML or select below:


ReBlogger Contents

Previous posts in WS, web services

Page 17 of 21350

Making WCF services amenable to testing

Blogger : Daniel Cazzulino
All posts : All posts by Daniel Cazzulino
Category : WS, web services
Blogged date : 2009 Mar 26

You know that using WebOperationContext.Current is BAD for making your service implementation testable, don't you?

My friend Pablo Cibraro continued to evolve his ideas around decoupling your service implementation from this static dependency, and came up with the most simple yet totally unobtrusive (for production code) approach to this, which he calls WCFMock.

The idea is quite simple: you can leverage C# class aliasing functionality (which few know it even exists) to conditionally (i.e. DEBUG vs RELEASE builds) replace the WebOperationContext implementation, like so:

using WebOperationContext = System.ServiceModel.Web.MockedWebOperationContext;

What will happen here is that on DEBUG builds, the actual class invoked when your code calls WebOperationContext.Current will be different, and it will be the one that allows easy testability by providing interfaces for the context and all its properties. You can setup this mock context quite easily in your tests:

Mock mockContext = new Mock { DefaultValue = DefaultValue.Mock };
using (new MockedWebOperationContext(mockContext.Object))
  var formatter = catalog.GetProducts("foo");

// Just making sure the content type was properly set 
mockContext.VerifySet(c => c.OutgoingResponse.ContentType = "application/atom+xml");


Of course, WCF service implementations, in my opinion, should not contain much core, and be basically a REST/webservices facade on top of the actual implementation which is something that never depends on any WCF specifics. But for those cases where you must ensure some behavior from your service implementations, this is certainly the best way to do it.

Read comments or post a reply to : Making WCF services amenable to testing
Page 17 of 21350

Newest posts

    Email TopXML