.Net C# RESTSharp 10 Minute Timeout

删除回忆录丶 提交于 2021-01-28 19:02:02

问题


I have embedded a browser control into a .Net form and compiled it as a window's executable. The browser control is displaying our HTML5 image viewer. 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.

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. 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.

Here is the RESTSharp code I am using:

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.

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.

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.

I have had limited success with forcing a Garbage Collection with this call:

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

Any thoughts?


回答1:


It is possible you are hitting the connection limit for .Net apps, as in MS docs:

"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).

Closing the connections should help, or you might be able to increase that limit, that is also in the doc




回答2:


I ended up putting

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

in a timer that fired every 2 minutes. 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. 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.



来源:https://stackoverflow.com/questions/54350909/net-c-sharp-restsharp-10-minute-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!