简体   繁体   English

.Net C# RESTSharp 10 分钟超时

[英].Net C# RESTSharp 10 Minute Timeout

I have embedded a browser control into a .Net form and compiled it as a window's executable.我已将浏览器控件嵌入到 .Net 表单中,并将其编译为窗口的可执行文件。 The browser control is displaying our HTML5 image viewer.浏览器控件正在显示我们的 HTML5 图像查看器。 The application opens sockets so it can listen to "push" requests from various servers.应用程序打开套接字,以便它可以侦听来自各种服务器的“推送”请求。 This allows images to be pushed to individual user's desktops.这允许将图像推送到单个用户的桌面。

When an incoming image push request comes in, the application calls a REST service using RESTSharp to generate a token for the viewer to use to display the image.当传入的图像推送请求进来时,应用程序使用 RESTSharp 调用 REST 服务来生成一个令牌,供查看者用来显示图像。

As long as the requests are consistently arriving, everything works great.只要请求始终如一地到达,一切都会很好。 If there is a lull (10 minutes seems to be the time frame), then the RESTSharp request times out.如果有一个停顿(似乎是 10 分钟的时间范围),那么 RESTSharp 请求就会超时。 It is almost as though the creation of a new instance of the RESTSharp artifacts are reusing the old ones in an attempted .Net optimization.几乎就好像创建 RESTSharp 工件的新实例正在尝试 .Net 优化中重用旧实例一样。

Here is the RESTSharp code I am using:这是我正在使用的 RESTSharp 代码:

private async Task<string> postJsonDataToUrl(string lpPostData) {
    IRestClient client = new RestClient(string.Format("{0}:{1}", MstrScsUrlBase, MintScsUrlPort));
    IRestRequest request = new RestRequest(string.Format("{0}{1}{2}", MstrScsUrlContextRoot, MstrScsUrlPath, SCS_GENERATE_TOKEN_URL_PATH));
    request.Timeout = 5000;
    request.ReadWriteTimeout = 5000;
    request.AddParameter("application/json", lpPostData, ParameterType.RequestBody);

    IRestResponse response = await postResultAsync(client, request);
    return response.Content;
} // postJsonDataToUrl

private static Task<IRestResponse> postResultAsync(IRestClient client, IRestRequest request) {
    return client.ExecutePostTaskAsync(request);
} // PostResultAsync

This is the line where the time out occurs:这是发生超时的行:

    IRestResponse response = await postResultAsync(client, request);

I have tried rewriting this using .Net's HttpWebRequest and I get the same problem.我尝试使用.Net 的 HttpWebRequest 重写它,但我遇到了同样的问题。

If I lengthen the RESTSharp timeouts, I am able to make calls to the server (using a different client) while the application is "timing out" so I know the server isn't the issue.如果我延长 RESTSharp 超时,我可以在应用程序“超时”时调用服务器(使用不同的客户端),所以我知道服务器不是问题。

The initial version of the code did not have the await async call structure - that was added as an attempt to get more information on the problem.代码的初始版本没有等待异步调用结构 - 添加它是为了尝试获取有关该问题的更多信息。

I am not getting any errors other than the REST timeout.除了 REST 超时,我没有收到任何错误。

I have had limited success with forcing a Garbage Collection with this call:我在通过这个调用强制垃圾收集方面取得了有限的成功:

GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

Any thoughts?有什么想法吗?

It is possible you are hitting the connection limit for .Net apps, as in MS docs:您可能会遇到 .Net 应用程序的连接限制,如 MS 文档所示:

"By default, an application using the HttpWebRequest class uses a maximum of two persistent connections to a given server, but you can set the maximum number of connections on a per-application basis." “默认情况下,使用 HttpWebRequest 类的应用程序最多使用两个到给定服务器的持久连接,但您可以在每个应用程序的基础上设置最大连接数。”

( https://docs.microsoft.com/en-us/dotnet/framework/network-programming/managing-connections ). https://docs.microsoft.com/en-us/dotnet/framework/network-programming/managing-connections )。

Closing the connections should help, or you might be able to increase that limit, that is also in the doc关闭连接应该会有所帮助,或者您可以增加该限制,这也在文档中

I ended up putting我最终把

GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

in a timer that fired every 2 minutes.在每 2 分钟触发一次的计时器中。 This completely solved my issue.这完全解决了我的问题。

This is very surprising to me since my HttpWebRequest code was wrapped in "using" statements so the resources should have been released properly.这让我感到非常惊讶,因为我的 HttpWebRequest 代码包含在“using”语句中,因此资源应该已正确释放。 I can only conclude that .Net was optimizing the use of the class and was trying to reuse a stale class rather than allow me to create a new one from scratch.我只能得出结论,.Net 正在优化类的使用,并试图重用一个陈旧的类,而不是让我从头开始创建一个新的类。

A new way of doing things.一种新的做事方式。

    var body = @"{ ""key"": ""value"" }";

    // HTTP package
    var request = new RestRequest("https://localhost:5001/api/networkdevices", Method.Put);
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("Keep-Alive", "");// set "timeout=120" will work as well
    request.Timeout = 120;
    request.AddBody(body);

    // HTTP call
    var client = new RestClient();
    RestResponse response = await client.ExecuteAsync(request);

    Console.WriteLine(response.Content);

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

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