.Net C# RESTSharp 10 分钟超时
.Net C# RESTSharp 10 Minute Timeout
我已经将浏览器控件嵌入到 .Net 表单中并将其编译为 window 的可执行文件。浏览器控件正在显示我们的 HTML5 图像查看器。该应用程序打开套接字,因此它可以侦听来自各种服务器的 "push" 请求。这允许将图像推送到单个用户的桌面。
当收到传入的图像推送请求时,应用程序使用 RESTSharp 调用 REST 服务来为查看器生成令牌以用于显示图像。
只要请求始终如一地到达,一切都会很好。如果出现间歇(10 分钟似乎是时间范围),则 RESTSharp 请求超时。几乎就好像 RESTSharp 工件的新实例的创建是在尝试 .Net 优化中重用旧实例。
这是我使用的 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
这是发生超时的行:
IRestResponse response = await postResultAsync(client, request);
我尝试使用 .Net 的 HttpWebRequest 重写它,但我遇到了同样的问题。
如果我延长 RESTSharp 超时,我可以在应用程序运行时调用服务器(使用不同的客户端)"timing out",所以我知道服务器不是问题所在。
代码的初始版本没有等待异步调用结构 - 添加该结构是为了尝试获取有关该问题的更多信息。
除了 REST 超时,我没有收到任何错误。
我通过这个调用强制垃圾收集取得了有限的成功:
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
有什么想法吗?
您可能达到了 .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."
(https://docs.microsoft.com/en-us/dotnet/framework/network-programming/managing-connections).
关闭连接应该会有帮助,或者您可以增加该限制,这也在文档中
我最后把
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
在每 2 分钟触发一次的计时器中。这完全解决了我的问题。
这让我感到非常惊讶,因为我的 HttpWebRequest 代码被包裹在 "using" 语句中,所以资源应该被正确释放。我只能得出结论,.Net 正在优化 class 的使用,并试图重用陈旧的 class 而不是让我从头开始创建一个新的。
一种新的做事方式。
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);
我已经将浏览器控件嵌入到 .Net 表单中并将其编译为 window 的可执行文件。浏览器控件正在显示我们的 HTML5 图像查看器。该应用程序打开套接字,因此它可以侦听来自各种服务器的 "push" 请求。这允许将图像推送到单个用户的桌面。
当收到传入的图像推送请求时,应用程序使用 RESTSharp 调用 REST 服务来为查看器生成令牌以用于显示图像。
只要请求始终如一地到达,一切都会很好。如果出现间歇(10 分钟似乎是时间范围),则 RESTSharp 请求超时。几乎就好像 RESTSharp 工件的新实例的创建是在尝试 .Net 优化中重用旧实例。
这是我使用的 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
这是发生超时的行:
IRestResponse response = await postResultAsync(client, request);
我尝试使用 .Net 的 HttpWebRequest 重写它,但我遇到了同样的问题。
如果我延长 RESTSharp 超时,我可以在应用程序运行时调用服务器(使用不同的客户端)"timing out",所以我知道服务器不是问题所在。
代码的初始版本没有等待异步调用结构 - 添加该结构是为了尝试获取有关该问题的更多信息。
除了 REST 超时,我没有收到任何错误。
我通过这个调用强制垃圾收集取得了有限的成功:
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
有什么想法吗?
您可能达到了 .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."
(https://docs.microsoft.com/en-us/dotnet/framework/network-programming/managing-connections).
关闭连接应该会有帮助,或者您可以增加该限制,这也在文档中
我最后把
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
在每 2 分钟触发一次的计时器中。这完全解决了我的问题。
这让我感到非常惊讶,因为我的 HttpWebRequest 代码被包裹在 "using" 语句中,所以资源应该被正确释放。我只能得出结论,.Net 正在优化 class 的使用,并试图重用陈旧的 class 而不是让我从头开始创建一个新的。
一种新的做事方式。
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);