title: C# all pipe instances are busy
body: <p>The following code creates a new thread acting first as a named pipe client for sending parameters and then as a server for retrieving results. After that it executes a function in another AppDomain acting as a named pipe server and after that as a client to send the results back.</p>
  
  <pre><code>public OrderPrice DoAction()
  {
    Task&lt;OrderPrice&gt; t = Task&lt;OrderPrice&gt;.Factory.StartNew(NamedPipeClient, parameters);
  
    if (domain == null)
    {
      domain = AppDomain.CreateDomain(DOMAINNAME);
    }
    domain.DoCallBack(AppDomainCallback);
  
    return t.Result;
  }
  
  static OrderPrice NamedPipeClient(object parameters) {
    OrderPrice price = null;
  
    using (NamedPipeClientStream stream = new NamedPipeClientStream(PIPE_TO)) {
      stream.Connect();
      SerializeToStream(stream, parameters);
    }
  
    using (NamedPipeServerStream stream = new NamedPipeServerStream(PIPE_BACK)) {
      stream.WaitForConnection();
  
      price = (OrderPrice)DeserializeFromStream(stream);
    }
  
    return price;
  }
  
  void AppDomainCallback() {
    OrderPrice price = null;
  
    using (NamedPipeServerStream stream = new NamedPipeServerStream(PIPE_TO)) {
      stream.WaitForConnection();
  
      List&lt;object&gt; parameters = (List&lt;object&gt;)DeserializeFromStream(stream);
  
      if (mi != null)
        price = (OrderPrice)mi.Invoke(action, parameters.ToArray());
  }
  
    using (NamedPipeClientStream stream = new NamedPipeClientStream(PIPE_BACK)) {
      stream.Connect();
      SerializeToStream(stream, price);
    }
  }
  </code></pre>
  
  <p>The code is called once per second on average and it worked fine for 7+ hours. But at some point "system.io.ioexception all pipe instances are busy" is thrown and they wont reconnect anymore after that. Browsing here it seems like it could be because of not properly disposing the pipe objects, but I guess thats all good since they are inside using statements.
  Does anyone have any clue what could be wrong here? The code is in .NET 4.0 running on windows server 2008.</p>