11/1/12

There was no endpoint listening - WCF Certificate Policy

When using WCF with transport security, you may encounter this error:

“There was no endpoint listening at https://servername/Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action

This is often caused because we tend to use a dev or expired certificate which is actually not valid and causes an invalid certificate exception that drops the communication with the server. To address this on the dev environment, we need to add a certificate policy that can handle the invalid certificate.

This can be done by first adding a policy class:

public sealed class CertificatePolicy
{
        /// <summary>
        /// certificate policy handler
        /// </summary>
        public static void SetPolicy()
        {
System.Net.ServicePointManager.ServerCertificateValidationCallback += RemoteCertValidate;
        }

        /// <summary>
        /// remote certificate validation.
        /// </summary>
 private static bool RemoteCertValidate(object sender,       System.Security.Cryptography.X509Certificates.X509Certificate cert,
System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors error)
        {
            //ignore invalid certificates by returning true
            return true;
        }
  }

With the policy now in place, we need to add it to the client code before calling the web service as follows:

CertificatePolicy.SetPolicy();
//TODO ADD CALL TO WEB SERVICE HERE

With the SetPolicy call, we added a policy to validate the remote certificate. In the case of an invalid certificate and with no policy, this usually creates an un-handled exception which terminates the communication. With this policy, we handle the validation of the certificate and return true to ignore any invalid certificate exception. This policy should only be used on dev environment. In production, the certificates should be valid for the most part.

I hope I was able to show how to handle this exception and manage invalid certificates on your WCF service.