title: How to call WCF service from .NET class library
body: <p>I have a WCF web service built in .NET 4.0 that is configured to require a username/password on each call and uses a Server certificate in the behaviour to encrypt the request. It currently uses wsHttpBinding. I have a .NET 4 utility app that lets me call the methods on this WCF web service just fine.</p>
  
  <p>I need to call this service from a .NET 2.0 class library. The library is in VS 2010 but targetting .NET 2.0 - I would like to simply switch to .NET 4.0 but can't for political reasons and time constraints. So therefore wsHttpBinding and the server certificate as used now are out of the question from what I've read so far.</p>
  
  <p>From reading bits and pieces I did the following:</p>
  
  <ol>
  <li>Changed the binding to use
  basicHttpBinding</li>
  <li>Removed mention of the server certificate from the behaviours</li>
  <li>Installed and
  enabled WSE on the class library
  project.</li>
  <li>Try to use on the WCF web service methods in .NET 2.0 winforms app - epic fail!!</li>
  </ol>
  
  <p>It doesn't matter how I seem to configure the binding at the server end HTTPS is just not acceptible; the only way to get it to let me call it at all is do use security mode of "None", grrrghhh! That of course allows me to call it but then the username/password details are not supplied and never get set on the WCF service and hence when it eventually tries to save it fails because the user ID is 0.</p>
  
  <pre><code>&lt;system.serviceModel&gt;
    &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false"/&gt;
      &lt;services&gt;
          &lt;service name="MyService.NameHere" behaviorConfiguration="NameOfMyConfigHere"&gt;
              &lt;endpoint binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="DirectoryServices.IDirectorySubmitService"&gt;
                  &lt;identity&gt;
                     &lt;dns value="localhost"/&gt;
                  &lt;/identity&gt;
              &lt;/endpoint&gt;
              &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
          &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name="MyBehaviourName"&gt;
                  &lt;serviceMetadata httpGetEnabled="true"/&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
              &lt;/behavior&gt;
          &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;bindings&gt;
          &lt;basicHttpBinding&gt;
              &lt;binding name="basicBinding" maxReceivedMessageSize="5242880"&gt;
                  &lt;readerQuotas maxStringContentLength="5242880"/&gt;
                  &lt;security mode="None"&gt;
                      &lt;transport clientCredentialType="Basic" proxyCredentialType="Basic" /&gt;
                      &lt;message clientCredentialType="UserName"/&gt;
                  &lt;/security&gt;
              &lt;/binding&gt;
          &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>If anyone could help me figure out the correct magic mumbo to put in I would be most appreciated.</p>
  
  <p>All I want to do is call a WCF web method supplying a username/password and have it use SSL to encrypt. The service is running on IIS. The client is a winforms app.</p>