title: Using NamedPipeServerStream and NamedPipeClientStream asynchronously
body: <p>I have the following requirements for a server/client architecture:</p>
  
  <ol>
  <li><p>Write a server/client that works asynchronously.</p></li>
  <li><p>The communication needs to be a duplex, i.e., reads and writes on both ends.</p></li>
  <li><p>Multiple clients can connect to the server at any given time.</p></li>
  <li><p>Server/client should wait until they become available and finally make a connection.</p></li>
  <li><p>Once a client connects it should write to the stream.</p></li>
  <li><p>Then the server should read from the stream and write response back to the client.</p></li>
  <li><p>Finally, the client should read the response and the communication should end.</p></li>
  </ol>
  
  <p>So with the following requirements in mind I've written the following code but I'm not too sure about it because the docs for pipes are somewhat lacking, unfortunately and the code doesn't seems to work correctly, it hangs at a certain point.</p>
  
  <pre><code>namespace PipesAsyncAwait471
  {
      using System;
      using System.Collections.Generic;
      using System.IO.Pipes;
      using System.Linq;
      using System.Threading.Tasks;
  
      internal class Program
      {
          private static async Task Main()
          {
              List&lt;Task&gt; tasks = new List&lt;Task&gt; {
                  HandleRequestAsync(),
              };
  
              tasks.AddRange(Enumerable.Range(0, 10).Select(i =&gt; SendRequestAsync(i, 0, 5)));
  
              await Task.WhenAll(tasks);
          }
  
          private static async Task HandleRequestAsync()
          {
              using (NamedPipeServerStream server = new NamedPipeServerStream("MyPipe",
                                                                              PipeDirection.InOut,
                                                                              NamedPipeServerStream.MaxAllowedServerInstances,
                                                                              PipeTransmissionMode.Message,
                                                                              PipeOptions.Asynchronous))
              {
                  Console.WriteLine("Waiting...");
  
                  await server.WaitForConnectionAsync().ConfigureAwait(false);
  
                  if (server.IsConnected)
                  {
                      Console.WriteLine("Connected");
  
                      if (server.CanRead) {
                          // Read something...
                      }
  
                      if (server.CanWrite) {
                          // Write something... 
  
                          await server.FlushAsync().ConfigureAwait(false);
  
                          server.WaitForPipeDrain();
                      }
  
                      server.Disconnect();
  
                      await HandleRequestAsync().ConfigureAwait(false);
                  }
              }
          }
  
          private static async Task SendRequestAsync(int index, int counter, int max)
          {
              using (NamedPipeClientStream client = new NamedPipeClientStream(".", "MyPipe", PipeDirection.InOut, PipeOptions.Asynchronous))
              {
                  await client.ConnectAsync().ConfigureAwait(false);
  
                  if (client.IsConnected)
                  {
                      Console.WriteLine($"Index: {index} Counter: {counter}");
  
                      if (client.CanWrite) {
                          // Write something...
  
                          await client.FlushAsync().ConfigureAwait(false);
  
                          client.WaitForPipeDrain();
                      }
  
                      if (client.CanRead) {
                          // Read something...
                      }
                  }
  
                  if (counter &lt;= max) {
                      await SendRequestAsync(index, ++counter, max).ConfigureAwait(false);
                  }
                  else {
                      Console.WriteLine($"{index} Done!");
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Assumptions:</strong></p>
  
  <p>The way I expect it to work is for all the requests I make when I call <code>SendRequestAsync</code> to execute concurrently where each request then makes additional requests until it reaches <code>6</code> and finally, it should print "Done!".</p>
  
  <p><strong>Remarks:</strong> </p>
  
  <ol>
  <li><p>I've tested it on .NET Framework 4.7.1 and .NET Core 2.0 and I get the same results.</p></li>
  <li><p>The <strong>communication</strong> between clients and the server <strong>is always local</strong> to the machine where clients are web applications that can queue some jobs like launching 3rd-party processes and the server is going to be deployed as a Windows service on the same machine as the web server that these clients are deployed on.</p></li>
  </ol>