The HTTP request is unauthorized with client authentication scheme ‘Ntlm’. The authentication header received from the server was ‘NTLM’

For one of the SharePoint implementation I was suppose to call the SharePoint 2010 ASMX service within a Custom WCF services hosted in a different server than the SharePoint 2010 host.

So as usual I added the service reference (PS: The add web reference is not available in WCF solution). Then I was getting the following error whenever the ASMX method was hit in the WCF.

"The HTTP request is unauthorized with client authentication scheme ‘Ntlm’. The authentication header received from the server was ‘NTLM’"

After breaking my head for few minutes, decided to hit the Google and got the solution in the source mentioned at the end.

So finally the code which worked is listed below.

 
  EndpointAddress endpoint = new EndpointAddress(new Uri("http://SharePointserver/_vti_bin/InvoiceServices.svc"));

  BasicHttpBinding httpBinding = new BasicHttpBinding();

  httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

  httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

  InvoiceServicesClient myClient = new InvoiceServicesClient(httpBinding, endpoint);

  myClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

  //Invoke the service method hereā€¦

Source: http://stackoverflow.com/questions/2608887/sharepoint-web-services-the-http-request-is-unauthorized-with-client-authenti

Note, in the above it might look like half of the link is missing and actually it works.