Externalize your Communication Protocol Details with WCF

In general the Web Service client proxy generated by “Add Web Reference” in Visual Studio is all you need to start talking to a Web Service. Some of you may need to present a certificate to the remote server you are calling and for this purpose you would typically populate the ClientCertificates collection on the generated proxy class like:

WebServiceProxy prx = new WebServiceProxy();

X509Certificate certificate = FetchRelevantCertificate();



The fictitious method FetchRelevantCertificate finds the relevant certificate using the X509Store class or a similar wrapper. While this is probably what many of us have done in the past it adds an unfortunate coupling to the communication protocol in your compiled code.


As you probably know WCF allow you to configure almost anything relevant for the communication externally to the code in the System.ServiceModel configuration section. The equivalent code using a WCF generated (svcutil.exe) client proxy would be:

WebServiceProxy prx = new WebServiceProxy();



Nothing here specifies the need of a client certificate for the communication. For the code to actually present the required certificate you should simply add the following (or similar) section to your app.config (or web.config):




        <behavior name="ClientSecureBehavior">






              x509FindType="FindBySubjectName" />







        <binding name="ClientSecureBinding">

          <security mode="Transport">

            <transport clientCredentialType="Certificate" />






      <endpoint address="https://demoappserver.mydomain.local/SSLTest/Service.asmx"








As you see this gives you complete isolation of business code and communication protocol related code. I agree that you could build a similar externalization for the wsdl.exe generated proxy classes, but with WCF this is really plain vanilla. As a side note you obviously do not have to change anything on the server side to start using WCF clients. In short: svcutil.exe is your friend ;-)

Nothing new really - I just got excited about this point late last week.