title: getting ProtocolViolationException and some bytes missing at end of file while copy
body: <p>I have a file <code>test.txt</code> which is continuous written by an some external application and every 2 minute my I am copy the file through <code>web api</code> call.</p>
  
  <p>Sometime I am getting <code>ProtocolViolationException</code> error (<code>Bytes to be written to the stream exceed the Content-Length bytes size specified.</code>) and as I know this may be thrown if the file being streamed is being written to at the same time.</p>
  
  <p>Here is my code,</p>
  
  <p><strong>Server Side</strong></p>
  
  <pre><code>[HttpPost]
      public async Task&lt;IHttpActionResult&gt; Post()
      {
          try
          {
              using (FileStream fs = new FileStream(@"C:\test\test_copy.txt", FileMode.CreateNew, FileAccess.Write))
              {
                  await Request.Content.CopyToAsync(fs);
              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
  
          return Ok&lt;bool&gt;(true);
      }
  </code></pre>
  
  <p><strong>Client Side</strong></p>
  
  <pre><code>static void Main(string[] args)
      {
          using (var fileStream = new FileStream(@"C:\test\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
          {
              CopyFile(fileStream).Wait();
          }
      }
  
      public static async Task&lt;bool&gt; CopyFile(FileStream fileStream)
      {
          bool Success = false;
  
          try
          {
              using (var client = new HttpClient())
              {
                  var response = await client.PostAsync("http://localhost:59805/api/Values", new StreamContent(fileStream));
              }
          }
  
          catch (ProtocolViolationException protocolViolationException)
          {
              //do nothing
          }
  
          return Success;
      }
  </code></pre>
  
  <p>As lots of resource saying we can suppress <code>ProtocolViolationException</code>and this works for me, but the <code>issue</code> is that sometimes some bytes at end of file is missing (entire file content copy is <code>not</code> happening)</p>
  
  <p>Note - on every file copy I am not getting this error, but when receives this error, some bytes is missing.</p>
  
  <p>Is there any suggestion. Do I need any <code>Flush</code>, etc. Thanks!</p>