简体   繁体   中英

Inter-service Communication Architecture Using WCF, Dependency Injection, and Unit Testing

I'm new to WCF and in a large part, also distributed programming. I am working on a project that requires 4 discrete services.

I am trying to correctly define the responsibilities for each component of each service. Suppose I have service B which needs to communicate with service A. For each service, I have defined the service implementation class, the service host, and a proxy class.

In order to unit test, I am using dependency injection - since service B needs to communicate with service A, I have passed an instance of A's proxy class as a constructor argument to service B.

When I am unit testing service B, I must have A's service host up and running.

  1. Is this the wrong way of going about dependency injection? If so, why, and how do you recommend I do it?
  2. Is there a better way of going about dependency injection?
  3. Should I have to run the service host to get the right results in the unit test?

Consider using

  • ChannelFactory instead of generated clients.

     ChannelFactory<IHello> clientFactory = new ChannelFactory<IHello>("targetConfiguration"); IHello client = clientFactory.CreateChannel(); string result = client.SayHello(); 
  • Interface types wherever possible

  • one of the mock object frameworks ( example ) to inject interface implementations when writing your tests.

Regarding your third question, the answer is "No" if your aim is testing particular small units (the whole point of unit testing :). But it's always better to write some integration tests to make sure you don't have any serialization/hosting problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM