Search code examples
wcfdependency-injectionunity-containeroperationcontext

Making WCF OperationContext play nicely with DI and testing


I'm running a WCF service hosted in a Windows service; dependencies of the WCF service are injected via Unity, which is all good. As a result, the service is also easy to write unit tests for.

Recently, I added functionality to the service that makes use of the OperationContext to inspect incoming messages. Unfortunately this rather blows the testability of the service out of the water, owing to Microsoft's fondness for sealed and/or static classes and lack of interfaces and/or virtual methods.

So I turned to the .NET'ers favourite tool in this situation, a wrapper class. Since this is a common problem, someone's already done the hard work for us. So this adds a new dependency to my WCF service, an implementation of IOperationContext. This was no problem for my unit tests, NSubstitute is my mock framework of choice (like Moq, but without the curse of the .Object).

However, when I try to fire up the service for real, I have the following problem - the OperationContext which is being wrapped has not been initialised at the time the IoC container registrations are done. My initialisation code (using Unity here) is:

container.RegisterType<IOperationContext, OperationContextWrapper>(new InjectionConstructor(OperationContext.Current));

but at this point, OperationContext.Current is null, so Unity promptly throws an exception, and my dreams of retiring before 40 go up in smoke.

So, the question is: how do you get WCF to play nicely with DI and a wrapped OperationContext?


Solution

  • OperationContext.Current is a settable property. Can you change your test initialization to

    OperationContext.Current = new OperationContextWrapper();
    

    and have it work that way? If you need it in unity, you could also:

    var oc = new OperationContextWrapper();
    OperationContext.Current = oc;
    container.RegisterInstance<IOperationContext>(oc);