Mocking Wcf ServiceContract

I want to mock ServiceContract. The problem is that Moq (and Dynamic-Proxy Castle) copies attributes from the interface to a dynamic proxy server that Wcf doesn't like. Wcf says: the ServiceContractAttribute attribute should only be defined for an interface or implementation, not both.

Exception: InvalidOperationException. The class of service is Castle.Proxies.IWorkOrderRequestServiceProxy and defines a ServiceContract and inherits ServiceContract from the IWorkOrderRequestService type. Contract inheritance can only be used between interface types. If a class is labeled ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute. Try moving the ServiceContractAttribute attribute to the IWorkOrderRequestService type on a separate interface, the IWorkOrderRequestService type implements

+7
moq wcf
source share
1 answer

Just experienced the same problem - so +1 for the solution !:-)

Update: http://code.google.com/p/moq/source/browse/trunk/Source/Proxy/CastleProxyFactory.cs contains a link to a property (collection) called AttributesToAvoidReplicating, looks like a place to start a search in the Moq source the code.

Update # 2: Nailed!

Add

Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add (TypeOf (ServiceContractAttribute));

before you plug in anything in your kernel.

+21
source share

All Articles