简体   繁体   English

什么会影响使用 C# 中的 REST API 的 HTTP POST 的响应时间

[英]What can affect the response time for an HTTP POST using a REST API in C#

I am trying to make a simple REST Web API with a C# back-end.我正在尝试使用 C# 后端制作一个简单的 REST Web API。

My main question is: Why does it take so long for my web client to show a response was received if I use a very large file?我的主要问题是:如果我使用一个非常大的文件,为什么我的 Web 客户端需要很长时间才能显示收到响应? My method returning the response seems to complete very quickly regardless of file size.无论文件大小如何,我的返回响应的方法似乎都很快完成。

My code is below as well as the output and the two different HTTP POST statements and their results.下面是我的代码以及输出和两个不同的 HTTP POST 语句及其结果。 In my first example I use a file that is 246 KB and get the response on the client side within 20ms.在我的第一个示例中,我使用了一个 246 KB 的文件,并在 20 毫秒内在客户端获得响应。 The second example uses a file that is 2089344 KB and gets a response on the client side about 45 seconds later.第二个示例使用一个 2089344 KB 的文件,并在大约 45 秒后在客户端获得响应。 Both logs show that there are only a few milliseconds between the beginning of the method and the result being created.两个日志都显示从方法开始到创建结果之间只有几毫秒。

Any help is appreciated!任何帮助表示赞赏! Thanks.谢谢。

CODE:代码:

[HttpPost, Route("test/upload")]
    public HttpResponseMessage Post([FromUri]string filename)
    {


        var task = Request.Content.ReadAsStreamAsync();
        System.Diagnostics.Debug.WriteLine("1: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
        task.Wait();
        System.Diagnostics.Debug.WriteLine("2: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
        Stream requestStream = task.Result;


        try
        {
            System.Diagnostics.Debug.WriteLine(filename);
            System.Diagnostics.Debug.WriteLine("3: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
            requestStream.Close();

        }
        catch (IOException)
        {
            requestStream.Close();
            throw new HttpResponseException(HttpStatusCode.InternalServerError);
        }
        System.Diagnostics.Debug.WriteLine("4: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
        HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.Created);
        System.Diagnostics.Debug.WriteLine("5: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
        return result;
    }

FIRST HTTP POST:第一个 HTTP POST:

POST /test/upload?filename=test HTTP/1.1
Content-Length: 252335
Host: localhost:64105
Content-Type: application/octet-stream

[message-body; type: application/octet-stream, size: 252335 bytes]

FIRST RESPONSE:第一反应:

HTTP/1.1 201 Created
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?BQzpcVXNlcnNcSWFuXERvY3VtZW50c1xXb3JraW5nRm9sZGVyXFNpbXBsZVJlc3RcU2ltcGxlUmVzdFx0ZXN0XHVwbG9hZA==?=
X-Powered-By: ASP.NET
Date: Wed, 04 Oct 2017 17:50:07 GMT
Content-Length: 0

FIRST LOG:第一个日志:

1: 63642721970786
2: 63642721970788
test
3: 63642721970791
4: 63642721970792
5: 63642721970793

SECOND HTTP POST:第二个 HTTP POST:

POST /test/upload?filename=test HTTP/1.1
Content-Length: 2139488256
Host: localhost:64105
Content-Type: application/octet-stream

[message-body; type: application/octet-stream, size: 2139488256 bytes]

SECOND RESPONSE:第二个回应:

HTTP/1.1 201 Created
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcSWFuXERvY3VtZW50c1xXb3JraW5nRm9sZGVyXFNpbXBsZVJlc3RcU2ltcGxlUmVzdFx0ZXN0XHVwbG9hZA==?=
X-Powered-By: ASP.NET
Date: Wed, 04 Oct 2017 17:56:07 GMT
Content-Length: 0

SECOND LOG:第二个日志:

1: 63642722167480
2: 63642722167481
test
3: 63642722167484
4: 63642722167485
5: 63642722167487

It looks like you're not doing any work with requestStream .看起来您没有对requestStream做任何工作。 Add this before your print out the filename and you should see a time difference.在打印文件名之前添加此内容,您应该会看到时差。

new StreamReader(requestStream).ReadToEndAsync().Wait();

As for why you're not seeing a time difference now, the entire request is sent to the server before the controller is called.至于为什么你现在没有看到时差,在调用控制器之前将整个请求发送到服务器。 See this question for a solution to streaming the request instead waiting for the whole body.请参阅此问题以获取流式传输请求而不是等待整个主体的解决方案。

ASP.Net Web API: Send response before request body is read/uploaded ASP.Net Web API:在读取/上传请求正文之前发送响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM