我一直致力于IIS托管的WebApi服务,该服务从S3流式传输文件(通过HttpWebRequest),并通过StreamContent从控制器返回。对于较大的文件,大约50mb,我们看到高CPU峰值。这是一个动作的片段,
public async Task<HttpResponseMessage> Get(Guid id)
{
var stream = await GetResponseStreamFromS3(id);
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = doc.FileName };
response.Content.Headers.ContentLength = streamAndLength.Length;
return response;
}
深入了解StreamContent,在Stream.CopyToAsync调用期间似乎出现尖峰。关于它为什么会导致峰值以及如何减轻它的任何想法?