title: BasicHttpBinding equivalent CustomBinding using WCF Client and PHP WebService
body: <p>I was running my Visual Studio 2008 Unit Test C# with a WebService PHP using WCF and I received the following error:</p>
  
  <blockquote>
    <p>System.ServiceModel.Security.MessageSecurityException:
    The HTTP request is unauthorized with
    client authentication scheme
    'Anonymous'. The authentication header
    received from the server was
    'NTLM,Basic realm="(null)"'. ---> 
    System.Net.WebException: The remote
    server returned an error: (401)
    Unauthorized..</p>
  </blockquote>
  
  <p>I am using Windows XP SP3 machine, unit test VS 2008 and WCF for connect to PHP WebService.</p>
  
  <p>I have no control about PHP WebService. I cannot modify it (neither configuration security).</p>
  
  <p>Here is my config file for the client that use the webservice PHP:</p>
  
  <pre><code> &lt;system.serviceModel&gt;
  
      &lt;extensions&gt;
        &lt;bindingElementExtensions&gt;
          &lt;add name="customTextMessageEncoding"
               type="COMPANY.IntegracionEasyVista.CustomTextEncoder.CustomTextMessageEncodingElement,COMPANY.IntegracionEasyVista.CustomTextEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" /&gt;
        &lt;/bindingElementExtensions&gt;
  
      &lt;/extensions&gt;
  
  
          &lt;bindings&gt;
  
                &lt;customBinding&gt;
                      &lt;binding name="ISO8859Binding"&gt;
                            &lt;customTextMessageEncoding messageVersion="Soap11WSAddressing10"
                                  encoding="ISO-8859-1" /&gt;
                            &lt;httpTransport /&gt;
                      &lt;/binding&gt;
                &lt;/customBinding&gt;
          &lt;/bindings&gt;
          &lt;client&gt;
                &lt;endpoint address="http://serverphp/webservice/SmoBridge.php"
                      binding="customBinding" bindingConfiguration="ISO8859Binding"
                      contract="ServiceEasyVista.WebServicePortType" name="EasyVistaSvcEndPoint" /&gt;
          &lt;/client&gt;
      &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Really, I use C# code:</p>
  
  <pre><code>var endpointAddress = Config.AppSettings.Settings[EasyVistaSvcEndPointAddress].Value;
  var endpoint = new System.ServiceModel.EndpointAddress(endpointAddress);
  
  var endpointBinding = new System.ServiceModel.Channels.CustomBinding();
  endpointBinding.Name = "ISO8859Binding";
  
  var bindingElement = new  IntegracionEasyVista.CustomTextEncoder.CustomTextMessageBindingElement();
  
  bindingElement.Encoding = "ISO-8859-1";
  bindingElement.MessageVersion = System.ServiceModel.Channels.MessageVersion.Soap11WSAddressing10; // "Soap11WSAddressing10" 
  endpointBinding.Elements.Add(bindingElement);
  
  var transportBinding = new HttpTransportBindingElement();
  endpointBinding.Elements.Add(transportBinding);
  
  ConfigurarBinding(endpointBinding);
  
  var svcClient = new WebServicePortTypeClient(endpointBinding, endpoint);
  return svcClient;
  </code></pre>
  
  <p>Update: tests</p>
  
  <p>I add this line:</p>
  
  <pre><code>transportBinding.AuthenticationScheme = System.Net.AuthenticationSchemes.Negotiate;
  </code></pre>
  
  <p>I get this error: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM,Basic realm="(null)"'</p>
  
  <p>I add this line:</p>
  
  <pre><code>transportBinding.AuthenticationScheme = System.Net.AuthenticationSchemes.Basic;
  </code></pre>
  
  <p>I get thiserror:</p>
  
  <blockquote>
    <p>System.ServiceModel.CommunicationObjectFaultedException:
    El objeto de comunicación,
    System.ServiceModel.Channels.ServiceChannel,
    no se puede usar para la comunicación
    porque se encuentra en el estado
    Faulted.</p>
    
    <p>//Server stack trace:  //   en
    System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan
    timeout)</p>
  </blockquote>
  
  <p>I add this line:</p>
  
  <pre><code> transportBinding.AuthenticationScheme = System.Net.AuthenticationSchemes.Ntlm;
  </code></pre>
  
  <p>I get thiserror:</p>
  
  <blockquote>
    <p>/Exception =
    System.ServiceModel.CommunicationException:
    Versión de mensaje no reconocida.</p>
    
    <p>//Server stack trace:  //   en
    System.ServiceModel.Channels.ReceivedMessage.ReadStartEnvelope(XmlDictionaryReader
    reader)</p>
    
    <p>//   en
    System.ServiceModel.Channels.StreamedMessage..ctor(XmlDictionaryReader
    reader, Int32 maxSizeOfHeaders,
    MessageVersion desiredVersion)</p>
    
    <p>//   en
    System.ServiceModel.Channels.Message.CreateMessage(XmlDictionaryReader
    envelopeReader, Int32
    maxSizeOfHeaders, MessageVersion
    version)</p>
    
    <p>//   en
    System.ServiceModel.Channels.Message.CreateMessage(XmlReader
    envelopeReader, Int32
    maxSizeOfHeaders, MessageVersion
    version)</p>
    
    <p>//   en
    CustomTextEncoder.CustomTextMessageEncoder.ReadMessage(Stream
    stream, Int32 maxSizeOfHeaders, String
    contentType) en
    E:\TFS\pro\CustomTextEncoder\CustomTextMessageEncoder.cs:línea
    66</p>
    
    <p>//   en
    System.ServiceModel.Channels.MessageEncoder.ReadMessage(Stream
    stream, Int32 maxSizeOfHeaders)</p>
    
    <p>//   en
    CustomTextEncoder.CustomTextMessageEncoder.ReadMessage
    (ArraySegment`1 buffer, BufferManager
    bufferManager, String contentType) en
    E:\TFS\pro\CustomTextEncoder\CustomTextMessageEncoder.cs:línea
    60</p>
    
    <p>//   en
    System.ServiceModel.Channels.MessageEncoder.ReadMessage(Stream
    stream, BufferManager bufferManager,
    Int32 maxBufferSize, String
    contentType)</p>
    
    <p>//   en
    System.ServiceModel.Channels.HttpInput.ReadChunkedBufferedMessage(Stream
    inputStream)</p>
  </blockquote>
  
  <p>I think, the following is right configuration (if were basichttpbinding)</p>
  
  <pre><code>&lt;security mode="TransportCredentialOnly"&gt;
      &lt;transport clientCredentialType="Basic" /&gt;
  &lt;/security&gt;
  </code></pre>
  
  <p>Web service of WCF in basichttpbinding might not be flexible enough. CustomBinding as the name describes that it alllows users design their own web service binding.</p>
  
  <p>I need use <strong>CustomBinding</strong> and SecurityMode (like basichttpbinding): WCF BasicHttpBinding equivalent CustomBinding</p>
  
  <p>I need Security Mode: TransportCredentialOnly, Basic, HTTP transport and  encoding="ISO-8859-1"</p>
  
  <p><a href="http://www.codemeit.com/security/wcf-basichttpbinding-equivalent-custombinding.html" rel="nofollow">http://www.codemeit.com/security/wcf-basichttpbinding-equivalent-custombinding.html</a></p>
  
  <p><a href="http://social.msdn.microsoft.com/Forums/en/wcf/thread/6cefadf1-9939-4f3b-a502-2d79a30c7d3a" rel="nofollow">http://social.msdn.microsoft.com/Forums/en/wcf/thread/6cefadf1-9939-4f3b-a502-2d79a30c7d3a</a></p>
  
  <p><a href="http://offroadcoder.com/2008/03/23/CallingYourNusoapPHPWebServiceFromWCF.aspx" rel="nofollow">http://offroadcoder.com/2008/03/23/CallingYourNusoapPHPWebServiceFromWCF.aspx</a></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ms731092(v=VS.90).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms731092(v=VS.90).aspx</a></p>
  
  <p>I try again using this configuration, and I get the error:</p>
  
  <pre><code>svcPro.ClientCredentials.UserName.UserName = "MY USER";
  svcPro.ClientCredentials.UserName.Password = "XXXX";
  </code></pre>
  
  <p>Config:</p>
  
  <pre><code>&lt;customBinding&gt;
  
  &lt;binding name="ISO8859Binding"&gt;
      &lt;customTextMessageEncoding messageVersion="Soap11WSAddressing10"        encoding="ISO-8859-1" /&gt;
  
      &lt;!--&lt;textMessageEncoding MessageVersion="Soap11" /&gt;--&gt;
      &lt;!--&lt;httpTransport /&gt;--&gt;
      &lt;httpTransport authenticationScheme="Basic" /&gt;
  
  &lt;/binding&gt;
  
  &lt;/customBinding&gt;
  </code></pre>
  
  <p>The error (InnerException is null):</p>
  
  <blockquote>
    <p>Error: Unrecognized message version</p>
    
    <p>Tipo:
    System.ServiceModel.CommunicationException
    Mensaje: Versión de mensaje no
    reconocida. StackTrace:  Server stack
    trace: </p>
    
    <p>en
    System.ServiceModel.Channels.ReceivedMessage.ReadStartEnvelope(XmlDictionaryReader
    reader)</p>
    
    <p>en
    System.ServiceModel.Channels.StreamedMessage..ctor(XmlDictionaryReader
    reader, Int32 maxSizeOfHeaders,
    MessageVersion desiredVersion)</p>
    
    <p>en
    System.ServiceModel.Channels.Message.CreateMessage(XmlDictionaryReader
    envelopeReader, Int32
    maxSizeOfHeaders, MessageVersion
    version)</p>
    
    <p>en
    System.ServiceModel.Channels.Message.CreateMessage(XmlReader
    envelopeReader, Int32
    maxSizeOfHeaders, MessageVersion
    version)</p>
    
    <p>en
    Reale.IntegracionEasyVista.CustomTextEncoder.CustomTextMessageEncoder.ReadMessage(Stream
    stream, Int32 maxSizeOfHeaders, String
    contentType) en
    E:\IntegracionEasyVista\CustomTextEncoder\CustomTextMessageEncoder.cs:línea
    66</p>
    
    <p>en
    System.ServiceModel.Channels.MessageEncoder.ReadMessage(Stream
    stream, Int32 maxSizeOfHeaders)
        en Reale.IntegracionEasyVista.CustomTextEncoder.CustomTextMessageEncoder.ReadMessage(ArraySegment`1
    buffer, BufferManager bufferManager,
    String contentType) en
    E:\IntegracionEasyVista\CustomTextEncoder\CustomTextMessageEncoder.cs:línea
    60</p>
    
    <p>en
    System.ServiceModel.Channels.MessageEncoder.ReadMessage(Stream
    stream, BufferManager bufferManager,
    Int32 maxBufferSize, String
    contentType)</p>
    
    <p>en
    System.ServiceModel.Channels.HttpInput.ReadChunkedBufferedMessage(Stream
    inputStream)</p>
    
    <p>en
    System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(Exception&amp;
    requestException)</p>
    
    <p>en
    System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
    timeout)</p>
    
    <p>en
    System.ServiceModel.Channels.RequestChannel.Request(Message
    message, TimeSpan timeout)</p>
    
    <p>en
    System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message
    message, TimeSpan timeout)</p>
    
    <p>en
    System.ServiceModel.Channels.ServiceChannel.Call(String
    action, Boolean oneway,
    ProxyOperationRuntime operation,
    Object[] ins, Object[] outs, TimeSpan
    timeout)</p>
    
    <p>en
    System.ServiceModel.Channels.ServiceChannel.Call(String
    action, Boolean oneway,
    ProxyOperationRuntime operation,
    Object[] ins, Object[] outs)</p>
    
    <p>en
    System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
    methodCall, ProxyOperationRuntime
    operation)</p>
    
    <p>en
    System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
    message)</p>
  </blockquote>
  
  <p>More info: part of WDSL of Service.php</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt; 
   &lt;definitions 
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd" 
  xmlns:tns="http://192.168.110.50/WebService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
  xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://192.168.110.50/WebService"&gt;
   &lt;types&gt;
   &lt;xsd:schema targetNamespace="http://192.168.110.50/WebService"&gt;
    &lt;xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" /&gt; 
    &lt;xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" /&gt; 
    &lt;/xsd:schema&gt;
    &lt;/types&gt;
  </code></pre>