Fiona
Fiona

Reputation: 1599

creating a wcf client instance using unity

So currently I'm creating a wcf client instance using the following code:

Service1Client client = (Service1Client)_container.Resolve<IService1>(new ParameterOverride("remoteAddress", url),
                    new ParameterOverride("endpointConfigurationName", "basicEndpoint"));

however this doesn't work when I'm creating unit tests as I'm casting the object as a Service1Client so my unit test bombs out as it cannot cast a Mock object:

 //Mock the WCF service
 var wcfMock = new Mock<IService1>();

 //register with container!
 var container = new UnityContainer();
 container.RegisterInstance(wcfMock.Object);

Any ideas on how best to resolve this issue?

Upvotes: 0

Views: 119

Answers (1)

CodeCaster
CodeCaster

Reputation: 151594

You claim to be casting to Service1Client so you can access the methods Open(), Abort() and Close() of System.ServiceModel.ClientBase.

Those methods are defined in System.ServiceModel.ICommunicationObject, so let your interface IService1 inherit from that:

public interface IService1 : ICommunicationObject
{
}

Then you can omit the cast.

Upvotes: 1

Related Questions