title: How do I set the correct content type in a WCF binding without using app.config?
body: <p>I have created a service reference to the CRM 2011 Organization.svc from a test console application. <strong>Everything works perfectly using the binding generated in the app.config.</strong> (Example of the service hosted by Microsoft <a href="https://engineering.api.crm.dynamics.com/XRMServices/2011/Organization.svc" rel="nofollow">here</a>.)</p>
  
  <p>This now needs to be moved into our "real" application and hosted in a DLL that will be deployed to the GAC. In following the app's conventions, the binding <strong>needs to be generated by code</strong>.</p>
  
  <p>I've started off trying to use the binding we use for our other WCF services:</p>
  
  <pre><code>BasicHttpBinding binding = new BasicHttpBinding();
  binding.SendTimeout = TimeSpan.FromMinutes(1);
  binding.OpenTimeout = TimeSpan.FromMinutes(1);
  binding.CloseTimeout = TimeSpan.FromMinutes(1);
  binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
  binding.AllowCookies = true;
  binding.BypassProxyOnLocal = false;
  binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
  binding.MessageEncoding = WSMessageEncoding.Text;
  binding.TextEncoding = System.Text.Encoding.UTF8;
  binding.TransferMode = TransferMode.Buffered;
  binding.UseDefaultWebProxy = true;
  binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
  binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
  </code></pre>
  
  <p>Unfortunately at the point the WCF service is called (using the Execute method with an OrganizationRequest), this error occurs:</p>
  
  <blockquote>
    <p><strong>System.ServiceModel.ProtocolException:</strong> Content Type text/xml; charset=utf-8 was not supported by service <a href="http://server:5555/xrmservices/2011/organization.svc" rel="nofollow">http://server:5555/xrmservices/2011/organization.svc</a>.  The client and service bindings may be mismatched.</p>
  </blockquote>
  
  <p>I'm not sure <strong>what the specific problem is with the binding</strong>, but my attempts at converting it to code have failed with the same error so far. Here's the working binding defined in app.config:</p>
  
  <pre><code>&lt;bindings&gt;
      &lt;customBinding&gt;
          &lt;binding name="CustomBinding_IOrganizationService"&gt;
              &lt;security defaultAlgorithmSuite="Default" authenticationMode="SspiNegotiated"
                  requireDerivedKeys="true" securityHeaderLayout="Strict" includeTimestamp="true"
                  keyEntropyMode="CombinedEntropy" messageProtectionOrder="SignBeforeEncryptAndEncryptSignature"
                  messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
                  requireSecurityContextCancellation="true" requireSignatureConfirmation="false"&gt;
                  &lt;localClientSettings cacheCookies="true" detectReplays="true"
                      replayCacheSize="900000" maxClockSkew="00:05:00" maxCookieCachingTime="Infinite"
                      replayWindow="00:05:00" sessionKeyRenewalInterval="10:00:00"
                      sessionKeyRolloverInterval="00:05:00" reconnectTransportOnFailure="true"
                      timestampValidityDuration="00:05:00" cookieRenewalThresholdPercentage="60" /&gt;
                  &lt;localServiceSettings detectReplays="true" issuedCookieLifetime="10:00:00"
                      maxStatefulNegotiations="128" replayCacheSize="900000" maxClockSkew="00:05:00"
                      negotiationTimeout="00:01:00" replayWindow="00:05:00" inactivityTimeout="00:02:00"
                      sessionKeyRenewalInterval="15:00:00" sessionKeyRolloverInterval="00:05:00"
                      reconnectTransportOnFailure="true" maxPendingSessions="128"
                      maxCachedCookies="1000" timestampValidityDuration="00:05:00" /&gt;
                  &lt;secureConversationBootstrap /&gt;
              &lt;/security&gt;
              &lt;textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                  messageVersion="Default" writeEncoding="utf-8"&gt;
                  &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                      maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
              &lt;/textMessageEncoding&gt;
              &lt;httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                  maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                  bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                  keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                  realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                  useDefaultWebProxy="true" /&gt;
          &lt;/binding&gt;
      &lt;/customBinding&gt;
  &lt;/bindings&gt;
  </code></pre>
  
  <p><strong>Does anyone know how to set the correct binding in code and/or read the binding from XML?</strong></p>