Search code examples
c#wpfclickoncerestsharp

Simple HTTP GET leads to "An existing connection was forcibly closed by the remote host" but only in WPF with ClickOnce?


I'm trying to find an explanation for a very strange behaviour.

The pice of code is quite simple:

// A simple HTTP GET call with RestSharp.
var url = "https://sandbox.tradier.com/v1/markets/quotes?symbols=MSFT";
var bearerToken = "thisCouldBeYourToken";
var client = new RestClient(url);
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", $"Bearer {bearerToken}");

var response = await client.ExecuteTaskAsync<MyClass>(request)

The code is part of a .NET 4.7.2 WPF applicaiton deployed with ClickOnce.

A couple of days ago, without any changes, this call suddenly returned An existing connection was forcibly closed by the remote host. However only for this particular URL. I use the exact same code for other URLs as well without any problems.

What was really strange, when I debugged the application the exception didn't occur, I received the desired response instead. Even when I started the application without debuggung, no error. I redeployed the app, but still the error occurred there. So for some reason the call is made differently after it was deployed!? BTW: Also the deployed version is running on my computer, not somewhere else.

After some intensive googling I came across this post, which suggested to explicitly set the SecurityProtocol to TLS 1.2 (default value: SystemDefault):

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

It worked like a charm. Now also the deployed version is running without any problems. So I guess the URL demands TLS 1.2 now, alright. But why did the app run even before when started directly and ran into problems when deployed via ClickOnce?


Solution

  • I've not verified it, but it seems that KB4520405 (KB4519565) changes ClickOnce application from TLS 1.2 to 1.0 .

    So, via ClickOnce it started to launch by default and older TLS version.