Always look on the bright side of life...

From the Blog

Aug
03
Posted by Mark Roxberry at 7:06 pm

I spent the greater part of Monday morning troubleshooting a problem with a C#/WCF testing console application.  The service under test is a rather straight forward service that can be used to request data from my client’s database.  Because data can take a bit to prepare, we have it set up to get data as a job, basically request data gets you a job token and then some point in the future use the job token to download your data.

Seems that running the client proxy in a console app with transfermode set to streamed only lets me run a request 2x before it times out.  No issue if I run it buffered.  Google was not my friend … except for a few clues about memory and stream objects.

Thinking this may be a LOH issue or a resource that is not properly disposed of,  I took a look through the code for IDisposableness.  Found the innerChannel has a Disposable, so after closing my connection,  I call Disposable and then GC.Collect.  This “fixes” it – however I don’t know why the problem occurs (more of an instinct than knowledge) and I don’t know why what I did fixed it specifically and what are the side effects.  So I’ll be spending some time to understand it.  Not a fan of black magic and silver bullets.

Testing a WCF service that uses a self-signed certificate which will throw this exception:

There was no endpoint listening at https://www.domainname.com/servicename.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

The inner exception is : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

Basically, this is a security measure to ensure that the client uses or explicitly circumvents the certificate check.  In order to explicitly circumvent this check, the client should use this code before calling any of the service functions:

A. This is the verbose method

1.  Code to conditional enforce validation check (we added this to a static utility class):

public static bool ValidateRemoteCertificate( object sender,

X509Certificate certificate, 

X509Chain chain, 

SslPolicyErrors policyErrors )
 {
    if (Convert.ToBoolean(

      ConfigurationManager.AppSettings["IgnoreSslErrors"]))
    {
      return true;
    }
      else
    {
      return policyErrors == SslPolicyErrors.None;
    }
  }

2.  Code to call ValidateRemoteCertificate in the handler for RemoteCertificateValidationCallback (we put this code in our test fixtures).  Put this code before your first call to the service:

//Ignore SSL cert validation issues if config is set 
ServicePointManager.ServerCertificateValidationCallback += 

new System.Net.Security.RemoteCertificateValidationCallback(TestUtils.ValidateRemoteCertificate);

B. This is a more compact call, but if you need to call in multiple places, less manageable

1.  Put this code before your first call to the service:

//Trust any certificate 
System.Net.ServicePointManager.ServerCertificateValidationCallback = 

((sender, certificate, chain, sslPolicyErrors) => true);
feedback