我需要你对我的执业意见。我想改进它。
所需经费如下:
好吧,这就是我得到的
对于自托管的OWIN,我遵循Microsoft文档。
端点:
FileController.cs
[HttpPost]
[Route("api/v1/upload")]
public HttpResponseMessage Post([FromUri]string filename)
{
var task = this.Request.Content.ReadAsStreamAsync();
task.Wait();
Stream requestStream = task.Result;
try
{
Stream fileStream = File.Create("./" + filename);
requestStream.CopyTo(fileStream);
fileStream.Close();
requestStream.Close();
}
catch (IOException)
{
throw new HttpResponseException( HttpStatusCode.InternalServerError);
}
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.Created;
return response;
}邮差:
邮递员返回状态代码201
发布于 2019-02-08 19:30:11
看起来有点不必要的复杂,一点也不完全是异步的。您只是强迫异步调用与.Wait()同步。正确的方法是使用异步“一路向下”:
[HttpPost]
[Route("api/v1/upload")]
public async Task<HttpResponseMessage> Post([FromUri]string filename)
{
try
{
using (Stream requestStream = await this.Request.Content.ReadAsStreamAsync())
using (Stream fileStream = File.Create("./" + filename))
{
await requestStream.CopyToAsync(fileStream);
}
return new HttpResponseMessage { StatusCode = HttpStatusCode.Created };
}
catch (IOException)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}https://codereview.stackexchange.com/questions/213112
复制相似问题