When unit-testing WCF interfaces we can simply instantiate classes that implement them and call them directly.
However in real life DTOs are serialized and transferred via the network before hitting the method.
Return value is serialized/deserialized in the same way. Because of serialization, scenarios that work in tests may fail in real life.
The following is a simple helper class that creates proxy which serializes arguments and return values using WCF serializer.
The code depends on Unity and Unity.Interception.
This is the code helper:
public static class WcfTestProxy
{
public static T Build<T>(T obj, Type[] knownTypes) where T: class
{
return Intercept.ThroughProxy<T>(obj, new InterfaceInterceptor(),
new IInterceptionBehavior[]
{
new EnterProxyBehaviour(knownTypes),
new ExitProxyBehaviour(knownTypes)
});
}
}
Entry behaviour serializes arguments:
internal class EnterProxyBehaviour : IInterceptionBehavior
{
public IMethodReturn Invoke(IMethodInvocation input,
GetNextInterceptionBehaviorDelegate getNext)
{
for (int i = 0; i < input.Inputs.Count; i++)
{
input.Inputs[i] = SerDeser(input.Inputs[i]);
}
var methodReturn = getNext().Invoke(input, getNext);
return methodReturn;
}
}
Exit behaviour serializes the return value:
internal class ExitProxyBehaviour : IInterceptionBehavior
{
public IMethodReturn Invoke(IMethodInvocation input,
GetNextInterceptionBehaviorDelegate getNext)
{
var methodReturn = getNext().Invoke(input, getNext);
methodReturn.ReturnValue = SerDeser(methodReturn.ReturnValue);
return methodReturn;
}
}