Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net Web API doesn't read all bytes from StreamContent
    text
    copied!<p>I have an ASP.Net Web API set up on my website that is used to communicated with a WPF desktop application. I have an action setup on the API to receive binary files from the client application. However in some (seemingly random) cases when I get all the bytes from the request not all the bytes are read. Hopefully you can give me an idea of how to do this in a way that will work all of the time. Here's the code:</p> <p><strong>Client Side:</strong></p> <pre><code>public static SubmitTurnResult SubmitTurn(int turnId, Stream fileStream) { HttpClient client = CreateHttpClient(); HttpContent content = new StreamContent(fileStream); content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); content.Headers.ContentDisposition.FileName = "new-turn.Civ5Save"; content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); content.Headers.ContentLength = fileStream.Length; HttpResponseMessage response = client.PostAsync( string.Format("SubmitTurn?authKey={0}&amp;turnId={1}", LocalSettings.Instance.AuthenticationKey, turnId ), content ).Result; response.EnsureSuccessStatusCode(); return response.Content.ReadAsAsync&lt;SubmitTurnResult&gt;().Result; } </code></pre> <p><em>SubmitTurnResult</em> is an enum that defines the result on the server, <em>turnId</em> is the ID for the entity this file is attached to, and <em>fileStream</em> is an actual FileStream reading the bytes of disk.</p> <p><strong>Server Side:</strong></p> <pre><code>[HttpGet, HttpPost] public SubmitTurnResult SubmitTurn(string authKey, int turnId) { try { bool worked = false; int gameId = 0; using (GmrEntities gmrDb = new GmrEntities()) { var player = gmrDb.Users.FirstOrDefault(u =&gt; u.AuthKey == authKey); if (player != null) { var turn = player.Turns.FirstOrDefault(t =&gt; t.TurnID == turnId); if (turn != null) { byte[] saveFileBytes = null; using (MemoryStream tempStream = new MemoryStream()) { var task = this.Request.Content.CopyToAsync(tempStream); task.Wait(); saveFileBytes = tempStream.ToArray(); tempStream.Close(); } if (saveFileBytes.Length != this.Request.Content.Headers.ContentLength.Value) { throw new Exception(string.Format("Byte array length ({0}) not equal to HTTP content-length header ({1}). This is not good!", saveFileBytes.Length, this.Request.Content.Headers.ContentLength.Value)); } worked = GameManager.SubmitTurn(turn, saveFileBytes, gmrDb); if (worked) { gameId = turn.Game.GameID; gmrDb.SaveChanges(); } } } } return SubmitTurnResult.OK; } catch (Exception exc) { DebugLogger.WriteExceptionWithComments(exc, string.Format("Diplomacy: Sumbitting turn for turnId: {0}", turnId)); return SubmitTurnResult.UnexpectedError; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload