Programmatically define WCF Endpoints with Behaviours
<p>Can anyone show me how to programmatically create WSHttpBindings with Endpoint behaviours?</p>

<p>This is what was generated from WCF Add Service references to 6 WebServices which I manually editted to add behaviours so that all the endpoints uses the same behaviour . </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;system.serviceModel&gt;
  &lt;behaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name="epBehaviour"&gt;
        &lt;clientCredentials&gt;
          &lt;clientCertificate findValue="This is my TEST Cert" storeLocation="LocalMachine" x509FindType="FindBySubjectName" storeName="My" /&gt;
        &lt;/clientCredentials&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;bindings&gt;
    &lt;wsHttpBinding&gt;
      &lt;binding name="WSHttpBinding_IQuote" messageEncoding="Mtom"&gt;
        &lt;security mode="TransportWithMessageCredential"&gt;
          &lt;transport clientCredentialType="None" /&gt;
          &lt;message clientCredentialType="Certificate" establishSecurityContext="false" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
      &lt;binding name="WSHttpBinding_ICommit" messageEncoding="Mtom"&gt;
        &lt;security mode="TransportWithMessageCredential"&gt;
          &lt;transport clientCredentialType="None" /&gt;
          &lt;message clientCredentialType="Certificate" establishSecurityContext="false" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
      &lt;binding name="WSHttpBinding_IRetrieve" messageEncoding="Mtom"&gt;
        &lt;security mode="TransportWithMessageCredential"&gt;
          &lt;transport clientCredentialType="None" /&gt;
          &lt;message clientCredentialType="Certificate" establishSecurityContext="false" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
      &lt;binding name="WSHttpBinding_IInfo" messageEncoding="Mtom"&gt;
        &lt;security mode="TransportWithMessageCredential"&gt;
          &lt;transport clientCredentialType="None" /&gt;
          &lt;message clientCredentialType="Certificate" establishSecurityContext="false" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
      &lt;binding name="WSHttpBinding_IPurge" messageEncoding="Mtom"&gt;
        &lt;security mode="TransportWithMessageCredential"&gt;
          &lt;transport clientCredentialType="None" /&gt;
          &lt;message clientCredentialType="Certificate" establishSecurityContext="false" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
      &lt;binding name="WSHttpBinding_ISearch" messageEncoding="Mtom"&gt;
        &lt;security mode="TransportWithMessageCredential"&gt;
          &lt;transport clientCredentialType="None" /&gt;
          &lt;message clientCredentialType="Certificate" establishSecurityContext="false" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/wsHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;client&gt;
    &lt;endpoint address="https://my.service.com/WCFServices/Info.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IInfo" contract="InfoWcfWS.IInfo" name="WSHttpBinding_IInfo" /&gt;
    &lt;endpoint address="https://my.service.com/WCFServices/Quote.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IQuote" contract="QuoteWcfWS.IQuote" name="WSHttpBinding_IQuote" /&gt;
    &lt;endpoint address="https://my.service.com/WCFServices/Commit.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICommit" contract="CommitWcfWS.ICommit" name="WSHttpBinding_ICommit" /&gt;
    &lt;endpoint address="https://my.service.com/WCFServices/Retrieve.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IRetrieve" contract="RetrieveWcfWS.IRetrieve" name="WSHttpBinding_IRetrieve" /&gt;
    &lt;endpoint address="https://my.service.com/WCFServices/Purge.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IPurge" contract="PurgeWcfWS.IPurge" name="WSHttpBinding_IPurge" /&gt;
    &lt;endpoint address="https://my.service.com/WCFServices/Search.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISearch" contract="SearchWcfWS.ISearch" name="WSHttpBinding_ISearch" /&gt;
  &lt;/client&gt;
&lt;/system.serviceModel&gt;</code></pre>
</div>
</div>
</p>

<p>Currently with the generated Proxy from the "Add Service"  and adding the behaviour "epBehaviour" which is referenced in the endpoints as a behaviourConfiguration, I can make calls to the endpoints.</p>

<p>Now, my next step is to define the behaviour part and the URL as configurations (May be in the  of the web.config) so that I can have different config for Staging and Production.</p>

<p>So my sample Staging config will have the URL defined as below</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;appSettings&gt;
  &lt;add key="ServiceUrl.Tls12.Quote" value="https://my.service.com/WCFServices/Quote.svc" /&gt;
  &lt;add key="ServiceUrl.Tls12.Commit" value="https://my.service.com/WCFServices/Commit.svc" /&gt;
  &lt;add key="ServiceUrl.Tls12.Retrieve" value="https://my.service.com/WCFServices/Retrieve.svc" /&gt;
  &lt;add key="ServiceUrl.Tls12.Info" value="https://my.service.com/WCFServices/Info.svc" /&gt;
  &lt;add key="ServiceUrl.Tls12.Purge" value="https://my.service.com/WCFServices/Purge.svc" /&gt;
  &lt;add key="ServiceUrl.Tls12.Search" value="https://my.service.com/WCFServices/Search.svc" /&gt;
&lt;/appSettings&gt;</code></pre>
</div>
</div>
</p>

<p>So if I'm consuming the "Info" WebService, my code should be something like </p>

<pre><code>//Code is missing how to define all the specifics like behaviours.
WSHttpBinding binding = new WSHttpBinding();
                EndpointAddress endpoint = new EndpointAddress(new Uri(ConfigurationManager.AppSettings["ServiceUrl.Tls12.Info"]));
             InfoWcfWS.InfoClient proxyNew = new InfoWcfWS.InfoClient(binding, endpoint);
</code></pre>

<p>In Summary, given the generated config after an "Add Service", to generate my Proxy Classes, I would like to remove all the config generated by VS and define my own config which will hold the URL;  Everything else like endpoints URI, behaviours ..etc should be instantiated programmatically.</p>

<p>Thanks</p>