Visual Studio Load Test Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel

Introduction 
 
Recently I ran some load tests in Visual Studio, but got the error “Request Failed: The request was aborted: Could not create SSL/TLS secure channel”. Those load tests were working fine previously but suddenly stopped working. After investigation, I found that recently our production server had some changes in TLS protocol support and now TLS 1.2 support is added.
 
 
Visual Studio Load Test Giving Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel
 
So, the cause is the recent change on the server, but now we must find the solution to resolve the issue and run the load test in Visual Studio.
 

Solutions

 
We need to create Web Performance Test Plug-In to resolve this issue.
  1. Open a Web performance and load test project that contains a Web load test.

  2. In Solution Explorer, right-click on the solution and select Add and then choose New Project. The Add New Project dialog box is displayed.

  3. Under Installed Templates, select Visual C#.

  4. In the list of templates, select Class Library.

  5. In the Name text box, type a name for your class. Choose OK.

    Visual Studio Load Test Giving Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel
  1. The new class library project is added to Solution Explorer and the new class appears in the Code Editor.

  2. In Solution Explorer, right-click the References folder in the new class library and select Add Reference.

  3. The Add Reference dialog box is displayed.

  4. Choose the .NET tab, scroll down, and select Microsoft.VisualStudio.QualityTools.WebTestFramework. Choose OK.

  5. The reference to Microsoft.VisualStudio.QualityTools.WebTestFramework is added to the Reference folder in Solution Explorer.

    Visual Studio Load Test Giving Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel
  1. In Solution Explorer, right-click on the top node of the Web performance and load test project that contains the load test to which you want to add the Web performance test plug-in and select Add Reference.

  2. The Add Reference dialog box is displayed. Choose the Projects tab and select the Class Library Project. Choose OK.

    Visual Studio Load Test Giving Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel
  1. In the Code Editor, write the code of your plug-in. First, create a new public class that derives from WebTestPlugin.

  2. Implement code inside one or more of the event handlers.
    1. public class Tls12ForcedPlugin : WebTestPlugin  
    2.     {  
    3.         [Description("Enable or Disable the plugin functionality")]  
    4.         [DefaultValue(true)]  
    5.         public bool Enabled { getset; }  
    6.   
    7.         public override void PreWebTest(object sender, PreWebTestEventArgs e)  
    8.         {  
    9.             base.PreWebTest(sender, e);  
    10.               
    11.             // We're using SSL2 here and not TLS. Without this line, nothing works.  
    12.             ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc00);// SecurityProtocolType.Ssl2;  
    13.   
    14.             //we wire up the callback so we can override  behavior and force it to accept the cert  
    15.             ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCB;  
    16.   
    17.             //let them know we made changes to the service point manager  
    18.             e.WebTest.AddCommentToResult(this.ToString() + " PP has made the following modification-> ServicePointManager.SecurityProtocol set to use SSLv3 in WebTest Plugin.");  
    19.         }  
    20.         public static bool RemoteCertificateValidationCB(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)  
    21.         {  
    22.             //If it is really important, validate the certificate issuer here.  
    23.             //this will accept any certificate  
    24.             return true;  
    25.         }  
    26. }  
  1. If you are using Framework 4.0 then you may get error SecurityProtocolType.Ssl2 not found.
For other Framework,
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 
For Framework 4.0,
(SecurityProtocolType)(0xc0 | 0x300 | 0xc00)
  1. After you have written the code, build the new project.

  2. Open a Web performance test. To add the Web performance test plug-in, choose Add Web Test Plug-in on the toolbar.

  3. The Add Web Test Plug-in dialog box is displayed. Under Select a plug-in, select your Web performance test plug-in class.

    Visual Studio Load Test Giving Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel
  1. In the Properties for the selected plug-in pane, set the initial values for the plug-in to use at run time. Choose OK.

    Visual Studio Load Test Giving Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel
  1. The plug-in is added to the Web Test Plug-ins folder.

    Visual Studio Load Test Giving Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel
  1. Now if you try to run the load test, it will run fine.

    Visual Studio Load Test Giving Error - Request Failed - The Request Was Aborted - Could Not Create SSL/TLS Secure Channel