title: TargetInvocationException on mocking ChannelFactory with moq
body: <p>Why <code>TargetInvocationException</code> is thrown when mocking <code>ChannelFactory&lt;IService&gt;</code>?</p>
  
  <pre><code>public interface IService
  {
      void Do();
  }
  
  public class Service : IService
  {
      private ChannelFactory&lt;IRemoteService&gt; factory;
  
      public Service(ChannelFactory&lt;IRemoteService&gt; factory)
      {
          this.factory = factory;
      }
  
      public void Do()
      {
          using (var remote = factory.CreateChannel())
          {
              remote.DoRemote();
          }
      }
  }
  
  [TestFixture]
  public class ChannelFactoryMoqTest
  {
      private IService service;
      private Mock&lt;ChannelFactory&lt;IRemoteService&gt;&gt; factory;
  
      [Test]
      public void Test()
      {
          this.factory = new Mock&lt;ChannelFactory&lt;IRemoteService&gt;&gt;();
  
          this.service = new Service(this.factory.Object);
          // Calling Object on this.factory throws TargetInvocationException
      }
  }
  </code></pre>
  
  <p>I want to use <code>ChannelFactory</code> dependency instead of simple <code>IRemoteService</code> cause it seems to me it's safer in terms of concurrency to create <code>Service</code> instance per call.</p>
  
  <p>Here is the exception stack trace:</p>
  
  <pre><code>  System.Reflection.TargetInvocationException was unhandled by user code
    HResult=-2146232828
    Message=Exception has been thrown by the target of an invocation.
    Source=mscorlib
    StackTrace:
         at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
         at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
         at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark&amp; stackMark)
         at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
         at System.Activator.CreateInstance(Type type, Object[] args)
         at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
         at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
         at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
         at Moq.Mock`1.&lt;InitializeInstance&gt;b__2()
         at Moq.PexProtector.Invoke(Action action)
         at Moq.Mock`1.InitializeInstance()
         at Moq.Mock`1.OnGetObject()
         at Moq.Mock.GetObject()
         at Moq.Mock.get_Object()
         at Moq.Mock`1.get_Object()
         at TR.Eikon.Services.AppVersion.Tests.Workflows.ChannelFactoryMoqTest.Test() in d:\Temp.cs:line 61
    InnerException: System.NullReferenceException
         HResult=-2147467261
         Message=Object reference not set to an instance of an object.
         Source=System.ServiceModel
         StackTrace:
              at System.ServiceModel.ChannelFactory.EnsureSecurityCredentialsManager(ServiceEndpoint endpoint)
              at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
              at System.ServiceModel.ChannelFactory`1..ctor()
              at Castle.Proxies.ChannelFactory`1Proxy..ctor(IInterceptor[] )
         InnerException: 
  </code></pre>