title: Dependency Injection with WCF proxy
body: <p>I have a service (Service1) that uses another serice (Service2). I am using Dependency injection for both services and need to inject the proxy for Service2 into Service1.</p>
  
  <p>I am unsure how to deal with the fact that the proxy is not a simple class of type IService2 but a proxy inheriting from ClientBase as well. Obviously my Service1 implementation needs to open the proxy and should also close it after use or abort it if an exception occurs but if I am just injecting an instance of IService2 then I cannot do this (without casting) because the Open, Close and Abort methods are on the base class whilst my operations are on the interface.</p>
  
  <p>When it comes to testing Service1, I would expect to mock just the interface but if the Service1 implementation expect Open, Close and Abort methods, then this is tricky. In the past, I have done something hacky like this but there must be a better way!</p>
  
  <pre><code>var proxyBase = _service2 as ClientBase;
  
  if (proxyBase != null)
  {
    proxyBase.Open();
  }
  
  _service2.DoOperation("blah"); //the actual operation
  
  if (proxyBase != null)
  {
    proxyBase.Close();
  }
  
  // repeat for Abort in exception handler(s).
  </code></pre>
  
  <p>What are other people doing?</p>
  
  <p>Thanks</p>