none
Error: The underlying connection was closed: An unexpected error occurred on a receive in VSTO project RRS feed

  • Question

  • I created a VSTO project to build an add-in for outlook 2016. In the project, I am calling an POST API to upload a file to the server. It suddenly stopped working yesterday. I am getting 4 errors when execute the PostAsync:

    1. HttpRequestException: An error occurred while sending the request.

    2. WebException: The underlying connection was closed: An unexpected error occurred on a receive.

    3. IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

    4.  SocketException: An existing connection was forcibly closed by the remote host

    I did some research and find the solution here. https://stackoverflow.com/questions/28286086/default-securityprotocol-in-net-4-5

    So I changed my code as 

    var stream = new FileStream(filePath, FileMode.Open);
    var content = new StreamContent(stream);
    client.DefaultRequestHeaders.Add("X-Atlassian-Token", "nocheck");
    System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    var response = client.PostAsync(apiUrl, content).Result;

    But I am still getting the same error. Does this has sth to do with VSTO project itself? I am using .net framework 4.6.1 and windows 10.

    The strange part is, if I am making the same API call in POSTMAN or coded in PowerShell script to call it, the API works without any error. If I am calling the same PowerShell scripts(which was working when I run in the PowerShell command line) in my VSTO project. I am getting this similar error: Exception calling "UploadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error occurred on a receive.

    The core part of the PowerShell script:

    $wc = New-Object System.Net.Webclient
    $wc.Headers.Add("Authorization", "Basic $global:token")
    $wc.Headers.Add("X-Atlassian-Token", "nocheck")
    $wc.UploadFile($url, $(Resolve-Path $attachment)) > $null

    Does anyone know how to fix this error in VSTO project?




    Thursday, August 8, 2019 2:30 PM

All replies

  • Hello,

    Did you try to run the same code in a regular windows forms application (with the same target .net framework set)? Do you get the same results?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, August 8, 2019 9:52 PM
  • Yes, I am getting the same error if I run the same code in a regular windows forms application(with the same target .net framework set).
    Friday, August 9, 2019 2:22 PM
  • Hey, did you manage to solve this. Our production plugin just went down randomly with this exact issue.
    Thursday, January 30, 2020 10:33 PM