title: Just need to check whether WSS server is On/Off via WSS client using .Net System.Net.WebSockets with Certificate?
body: <p>Why always getting error Cannot find the requested object? See trace detail in code my comment.</p>
  <p>I need to implement WSS client and check WSS server availability using certificates.</p>
  <p>But as try to create object of <code>X509Certificate2</code>, getting error:</p>
  <blockquote>
  <p>Cannot find the requested object</p>
  </blockquote>
  <pre class="lang-cs prettyprint-override"><code>bool IsWssConnAlive(string WssUrl)
  {
      bool flg = false;
      try
      {
          //string _certificateKeyStorePath = @&quot;E:\bk\WSS\gateway\test\keystoreserver.der&quot;;
          string _certificateKeyStorePath = @&quot;E:\bk\WSS\gateway\test\keystoreserver.p12&quot;;
          X509Certificate2 x509_1 = new X509Certificate2(_certificateKeyStorePath, &quot;test@123&quot;);
  
          /*
           * Above X509Certificate2 statement aways giving error : &quot;Cannot find the requested object.\r\n&quot;}
           * I tried .der, p21, .jks getting same error
           * stack trace details :-&gt;
           
           *    at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
                at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertFileType(String fileName)
                at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags)
                at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)
                at Test_WssConnectivity.Program.IsWssConnAlive(String WssUrl, Int32 CheckTimeInSec) in E:\Development\SolutionForTestProject\Test_WssConnectivity\Program.cs:line 142
  
           */
  
          ClientWebSocket websocket = new ClientWebSocket();
          websocket.Options.ClientCertificates.Add(x509_1);
  
  
          X509CertificateCollection xcol = new X509CertificateCollection();
          xcol.Add(x509_1);
  
          websocket.Options.ClientCertificates = xcol;
  
          //connect to URL
          websocket.ConnectAsync(new Uri(WssUrl), CancellationToken.None);
  
          //Now checking actaul status
          if (websocket.State == WebSocketState.Open)
          {
              flg = true;
          }
          else if (websocket.State == WebSocketState.Closed)
          {
              flg = false;
          }
      }
      catch (Exception ex)
      {
          flg = false;
      }
  
      return flg;
  }
  </code></pre>