Tôi có một ASP.Net Web API được thiết lập trên trang web của tôi được sử dụng để giao tiếp với một ứng dụng máy tính để bàn WPF. Tôi có một thiết lập hành động trên API để nhận tệp nhị phân từ ứng dụng khách. Tuy nhiên trong một số trường hợp (dường như ngẫu nhiên) khi tôi nhận được tất cả các byte từ yêu cầu, không phải tất cả các byte đều được đọc. Hy vọng rằng bạn có thể cho tôi một ý tưởng làm thế nào để làm điều này theo một cách mà sẽ làm việc tất cả các thời gian. Dưới đây là các mã:ASP.Net Web API không đọc tất cả các byte từ StreamContent
Client Side:
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}&turnId={1}",
LocalSettings.Instance.AuthenticationKey,
turnId
),
content
).Result;
response.EnsureSuccessStatusCode();
return response.Content.ReadAsAsync<SubmitTurnResult>().Result;
}
SubmitTurnResult là một enum định nghĩa kết quả trên máy chủ, turnId là ID cho đơn vị tập tin này được gắn vào, và fileStream là một FileStream thực tế đọc các byte của đĩa.
Server Side:
[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 => u.AuthKey == authKey);
if (player != null)
{
var turn = player.Turns.FirstOrDefault(t => 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;
}
}
Đây có phải là Windows Server 2003 hoặc Windows XP 32 bit không? Chúng tôi đang chạy vào cùng một hành vi này với StreamContent, nhưng khi phát trực tuyến phản hồi từ dịch vụ Web API của Windows Server 2003. Nó không repro vào năm 2008. Ngoài ra, chúng tôi đã tìm thấy rằng điều này chỉ repros với một FileStream. Chuyển đổi FileStream thành MemoryStream bỏ qua vấn đề (với chi phí bộ nhớ của khóa học). Chúng tôi đã nhận thấy rằng khi luồng phản hồi chấm dứt sớm, nó luôn ở ranh giới 4096 byte và nó đạt đến giới hạn khoảng 3,5MB. –