title: Unknown exception when calling a webservice
body: <p>I have in my system two WCF webservices.</p>
  
  <p>The webservice A calls the webservice B. Most part of the time, it works properly. But sometines, an exception in A is raised when it calls B :</p>
  
  <blockquote>
    <p>Exception has been thrown by the target of an invocation.</p>
  </blockquote>
  
  <p>In order to try to know where is the problem,  I put a huge Try Catch in the called method in B, from the first instruction to the last. Same problem.</p>
  
  <p>I try then to put customErrors to off and includeExceptionDetailInFaults to true in order to have details from the exception. Same problem.</p>
  
  <p>It's not a timeout problem because requests duration are lower than 1 second.</p>
  
  <p>I checked length of requests, and some good requests are longer than bad requests. The problem is not the size.</p>
  
  <p>I can't reproduce the problem, because it appears only few times.</p>
  
  <p>I think if the problem was on A, exception would have more details than this message.</p>
  
  <p>Maybe the problem is on IIS (both are on the same IIS instance), but A communicate to B via localhost:xxxx/mywebservice.svc, so It's hard to believe it's a communication problem.</p>
  
  <p>EDIT :</p>
  
  <p>There is the InnerException Message :</p>
  
  <blockquote>
    <p>Cannot access a disposed object. Object name:
    'System.ServiceModel.Channels.ServiceChannel'.</p>
  </blockquote>
  
  <p>The webservice A use a dynamic call for B, using this : <a href="http://blogs.msdn.com/b/vipulmodi/archive/2006/11/16/dynamic-programming-with-wcf.aspx" rel="nofollow">http://blogs.msdn.com/b/vipulmodi/archive/2006/11/16/dynamic-programming-with-wcf.aspx</a>
  Another link here : <a href="https://github.com/carlosfigueira/WCFQuickSamples/tree/master/WCFForums/DynamicProxy" rel="nofollow">https://github.com/carlosfigueira/WCFQuickSamples/tree/master/WCFForums/DynamicProxy</a></p>
  
  <p>Here is my code :</p>
  
  <pre><code>DynamicProxy proxy = null;
  [...]
  proxy = FactoryTest.getProxy(sServiceWsdl, sContract);
  [...]
  try {
     sXmlOUT = (String)proxy.CallMethod(sMethod, sXmlIN);
     proxy.Close();
  catch (Exception e)
  {
     // Here appears the exception
  }
  [...]
  </code></pre>
  
  <p>And FactoryTest class :</p>
  
  <pre><code>public sealed class FactoryTest
  {
      private static object syncRoot = new Object();
      private static Hashtable hashFactory = new Hashtable();
  
      public static DynamicProxy getProxy(String sServiceWsdl, String sContract)
      {
          if (hashFactory[sServiceWsdl] == null || ((ProxyTest)hashFactory[sServiceWsdl]).getTimeFromCreation().TotalSeconds &gt; 60 * 60 * 6)
          {
              lock (syncRoot)
              {
                  if (hashFactory[sServiceWsdl] == null || ((ProxyTest)hashFactory[sServiceWsdl]).getTimeFromCreation().TotalSeconds &gt; 60 * 60 * 6)
                  {
                      hashFactory.Add(sServiceWsdl, new ProxyTest(sServiceWsdl, sContract));
                  }
              }
          }
  
          return ((ProxyTest)hashFactory[sServiceWsdl]).getProxy();
      }
  
      public static bool isProxyExists(String sServiceWsdl, String sContract)
      {
          lock (syncRoot)
          {
              return hashFactory[sServiceWsdl] == null ? false : true;
          }
      }
  }
  </code></pre>
  
  <p>There is a complete Exception :</p>
  
  <blockquote>
    <p>Exception has been thrown by the target of an invocation.    at
    System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo
    method, Object target, Object[] arguments, SignatureStruct&amp; sig,
    MethodAttributes methodAttributes, RuntimeType typeOwner)    at
    System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method,
    Object target, Object[] arguments, Signature sig, MethodAttributes
    methodAttributes, RuntimeType typeOwner)    at
    System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
    invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
    Boolean skipVisibilityChecks)    at
    System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
    invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)<br>
    at System.RuntimeType.InvokeMember(String name, BindingFlags
    bindingFlags, Binder binder, Object target, Object[] providedArgs,
    ParameterModifier[] modifiers, CultureInfo culture, String[]
    namedParams)    at WS_Generic.Service.CallWsMethod(String
    sXmlSettings, String sXmlIN, String&amp; sXmlOUT)</p>
  </blockquote>
  
  <p>There is a complete InnerException :</p>
  
  <blockquote>
    <p>Cannot access a disposed object. Object name:
    'System.ServiceModel.Channels.ServiceChannel'.</p>
    
    <p>Server stack trace:     at
    System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen()
    at System.ServiceModel.Channels.ServiceChannel.Call(String action,
    Boolean oneway, ProxyOperationRuntime operation, Object[] ins,
    Object[] outs, TimeSpan timeout)    at
    System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
    methodCall, ProxyOperationRuntime operation)    at
    System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
    message)</p>
    
    <p>Exception rethrown at [0]:     at
    System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
    reqMsg, IMessage retMsg)    at
    System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;
    msgData, Int32 type)    at IWsAfsol.EmvTrxAuthorization(String sXmlIn)
    at WsAfsolClient.EmvTrxAuthorization(String sXmlIn)</p>
  </blockquote>