Reputation: 90
I am having trouble using the RestSharp client in a Windows service. When the API is down the connection is lost. But once the API runs again, the rest client keeps throwing the same error. Even if I set up a new instance of the RestClient.
Anyone with the same problem and a working solution or proposal?
Upvotes: 1
Views: 3457
Reputation: 76
Basically it appears to be a SSL/TLS negotiation failure. To ensure it, before instantiating RestClient, you could set ServicePointManager.SecurityProtocol as in the example below:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
restClient = new RestClient(options.BaseUrl);
Hope this helps someone.
Upvotes: 1
Reputation: 19
I'm a colleague of Ludwig. Our application starts and connects to the api, the application keeps running and calling the api. When the api is suddenly down (restarted) our application gets an error: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server and we call the InitODataClient and ReadConfigAsync functions to recreate the restClient. When the api is running again we expected the RestClient to work again but we keep getting: the underlying connection is closed. could not establish secure channel for ssl/tls When we restart our application everything works again. There are no certificate problems. It seems that creating a new RestClient object uses the old (invalid) connection Some of the code we use:
private RestClient restClient;
private void InitODataClient()
{
restClient = new RestClient(options.BaseUrl);
restClient.AddDefaultHeader("Authorization", "Bearer " + options.AccessToken);
}
private async Task ReadConfigAsync()
{
var requestApplication = new RestRequest("Applications/" + Guid.Parse(applicationId));
var response = await restClient.ExecuteGetTaskAsync<Application>(requestApplication);
//Here the response contains the underlying connection is closed error
}
public async Task RestartAsync()
{
Stop();
do
{
try
{
Logger.Log("Trying to reconnect to the server in 5 seconds...");
Thread.Sleep(5000);
InitODataClient();
ReadConfigAsync().Wait();
break;
}
catch (Exception) { }
}
while (true);
}
Upvotes: 1