title: Need How To help moving system.serviceModel config section into my code
body: <p>I have developed a Client/Server application for my current employer and one of the requirements is that the client part be distributed as a DLL. Since DLLs don't 'do' dllName.dll.config files, I need to move the config into the code.  I have done this to the best of my ability, but I'm now getting this exception; </p>
  
  <blockquote>
    <p>The X.509 certificate CN=ComputerName chain building failed.<br>
    The certificate that was used has a trust chain that cannot 
    be verified.  Replace the certificate or change the certificateValidationMode.
    An internal certificate chaining error has occurred.</p>
  </blockquote>
  
  <p>I have googled, and otherwise searched all over for a resolution to this, but so far I can't find anything with a clear enough explaination of the various close-but-not-really-it solutions I've seen.</p>
  
  <p>Anyway, there is a certificate in My LocalStore, but it's really only used by the server (currently testing both client and server on the same XP Pro box), at least, as far as I understand things. (Still pretty new to WCF) (Note how the client config file below does not get or otherwise point out the aforementioned cert to the client whatsoever.</p>
  
  <p>The client's app.config looks like the following, and works just great via our test EXE, which contains the  section of this config file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
   &lt;system.serviceModel&gt;
    &lt;bindings&gt;
     &lt;netTcpBinding&gt;
      &lt;binding name="MyTcpBinding_IFileXferService" 
         receiveTimeout="02:00:00" 
         sendTimeout="02:00:00" 
         transferMode="Streamed" 
         maxBufferSize="65536" 
         maxReceivedMessageSize="2147483647"&gt;
       &lt;readerQuotas maxStringContentLength="2147483647" 
            maxArrayLength="2147483647" 
            maxBytesPerRead="65536" /&gt;
  
       &lt;security mode="Transport"&gt;
        &lt;transport clientCredentialType="None" /&gt;
       &lt;/security&gt;
  
      &lt;/binding&gt;
     &lt;/netTcpBinding&gt;
    &lt;/bindings&gt;
  
    &lt;behaviors&gt;
     &lt;endpointBehaviors&gt;
      &lt;behavior name="ClientConfigBehavior"&gt;
       &lt;dataContractSerializer maxItemsInObjectGraph="6553600" /&gt;
       &lt;clientCredentials&gt;
  
        &lt;serviceCertificate&gt;
         &lt;authentication certificateValidationMode="None" /&gt;
        &lt;/serviceCertificate&gt;
  
       &lt;/clientCredentials&gt;
      &lt;/behavior&gt;
     &lt;/endpointBehaviors&gt;
    &lt;/behaviors&gt;
  
    &lt;client&gt;
     &lt;endpoint name="ClientConfig" 
         binding="netTcpBinding" 
         bindingConfiguration="MyTcpBinding_IFileXferService" 
           behaviorConfiguration="ClientConfigBehavior" 
         contract="ServiceRefs.IFileXferService" /&gt;
    &lt;/client&gt;
  
   &lt;/system.serviceModel&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>But now that I've moved the configuration information into the code (so that the DLL can be distributed without a .config file), I am, again, getting the exception mentioned above.  (By the way, if we figure out that a given setting needs to be changable, I can add it to our own xml config file.)</p>
  
  <p>The code meant to replace the above config looks like this:</p>
  
  <pre><code> ...
   netTcpBinding = new NetTcpBinding();
   endpointAddr = new EndpointAddress(HostURI);
  
   updateMaxItemsBehavior(); // method below this snippet
  
   TimeSpan tsTwoHours = new TimeSpan(2,0,0);
   netTcpBinding.ReceiveTimeout = tsTwoHours;
   netTcpBinding.SendTimeout = tsTwoHours;
  
   netTcpBinding.TransferMode = TransferMode.Streamed;
   netTcpBinding.MaxBufferSize = 65536;
   netTcpBinding.MaxReceivedMessageSize = 2147483647;
  
   netTcpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
   netTcpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
   netTcpBinding.ReaderQuotas.MaxBytesPerRead = 65536;
  
   // double the 64k default
   netTcpBinding.MaxBufferPoolSize = 131072;
  
   netTcpBinding.Security.Mode = SecurityMode.Transport;
   netTcpBinding.Security.Transport.ClientCredentialType =
    TcpClientCredentialType.None;
  
   // now to instantiate the service proxy client 
   svcClient = new FileXferServiceClient(netTcpBinding, endpointAddr);
  </code></pre>
  
  <p>I've tried this with various combinations of these next bits, but can't
  get around the dang exception.</p>
  
  <pre><code> // Set the certificate for the client.
   X509Certificate2 cert = new X509Certificate2();
   svcClient.ClientCredentials.ClientCertificate.Certificate = cert;
  
   //svcClient.ClientCredentials.ClientCertificate.SetCertificate(
   //    StoreLocation.LocalMachine, 
   //    StoreName.My, 
   //    X509FindType.FindByThumbprint,
   //    "ThumbprintThumbprintThumbprintThumbprint");
  
   // here's this
   //
   private void updateMaxItemsBehavior()
   {
    svcEndpoint = new ServiceEndpoint(
     new ContractDescription("IFileXferService"));
  
    foreach (OperationDescription operation in
     svcEndpoint.Contract.Operations)
    {
     operation.Behaviors
      .Find&lt;DataContractSerializerOperationBehavior&gt;()
       .MaxItemsInObjectGraph = 6553600;
    }
   }
  </code></pre>
  
  <p>In summary, our test Winforms app (and thus the client-side of our WCF app) works fine with the config settings in the wfappname.exe.config file, but now that I've tried to move this all into code, it's apparently demanding a certificate.  I guess what I'm really hoping for is 'how do I duplicate the above app.config in code so that the app.config is not needed by my WCF Client.'</p>
  
  <p>Can anyone help me with this?  (I'll be yer best friend!)  ; )</p>
  
  <p>Thanks,
  Scott</p>