BasicHttpBinding in WCF

This binding is probably the most comprehensive binding when talking in terms of interoperability. If you have written ASPX web services in the past, then you have used BasicHttpBinding in your web service even if you weren’t aware that you were doing so. BasicHttpBinding uses HTTP as the transport protocol and Text/XML as the default message encoding. It represents bindings that a service can use to communicate with ASMX-based clients and web services.
Though simple to use, employment of the BasicHttpBinding presents some issues that developers need to be aware of. These issues are as follows:

  • Security is disabled by default. To add security to this binding you can use the BasicHttpSecurityMode enumeration and set the value to anything other than None.
  • This binding does not provide WS-* functionality and is fairly weak on the interoperability. It does not provide SOAP security or any transaction support.

BasicHttpBinding Properties

The following table shows a list of attributes, and their descriptions, that are available to be used with the BasicHttpBinding binding.

Attribute Description
allowCookies Boolean value, default of False, which specifies whether or not the client accepts cookies and to propagate them of any future requests.
bypassProxyOnLocal Boolean value, default of False, which specifies whether or not to bypass the proxy server for local Internet resources.
closeTimeout A time interval value, which must be greater than zero, that specifies the amount of time for a close operation to complete. The default value is 1 minute (00:01:00).
hostnameComparisonMode Specifies the HTTP hostname comparison node used to parse URIs. Acceptable values are Exact, StrongWildCard, and WeakWildCard. The default value is StrongWildCard.
maxBufferPoolSize Specifies the maximum buffer size for a buffer pool, which stores messages processed by the binding. This is an integer value with a default of 512*1024, or 524388.
maxBufferSize Specifies the maximum buffer size for a buffer, which stores messages processed by the binding. This is an integer value with a default of 65536.
maxReceivedMessageSize Specifies the maximum size of a message, including headers. The number is specified in bytes with a default value of 65536.If a message is larger than the value specified, the sender receives a SOAP fault message and the receiver drops the message and creates an event in the trace log.
messageEncoding Defines the type of encoding used to encode the message. Acceptable values are Text (text encoding) and Mtom (Message Transmission Organization Mechanism 1.0 encoder). Default is Text.
Name A unique string value that contains the configuration name of the binding.
openTimeout A time interval value that specifies the amount of time a message has to complete. Value should be greater than zero. Default is 1 minute (00:01:00).
proxyAddress Used in conjunction with the useDefaultWebProxy attribute. This attribute is a URI that specifies the address of the HTTP proxy. If the useDefaultWebProxy attribute is set to True, this value must be null.
receiveTimeout A time interval value that specifies the amount of time a receive operation has to complete. Value should be greater than zero. Default is 1 minute (00:01:00).
sendTimeout A time interval value that specifies the amount of time a send operation has to complete. Value should be greater than zero. Default is 1 minute (00:01:00).
textEncoding Specifies the character encoding set. Acceptable values are UnicodeFffeTextEncoding,
Utf16TextEncoding, and Utf8TextEncoding. Default is Utf8TextEncoding. This value is used for emitting binding messages.
transferMode A valid TransferMode value that specifies whether messages are buffered or streamed during a response or request.
useDefaultWebProxy Boolean value, default of True, which specifies whether the auto-configured HTTP proxy should be used if one exists.

The following example illustrates some of the properties being configured in a configuration file:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name = “basichttpbind”
closeTimeout = “00:00:30”
openTimeout = “00:00:30”
sendTimeout = “00:00:30”
receiveTimeout = “00:00:30”>
</binding>
</basicHttpBinding>
</bindings>
<system.ServiceModel>

The same can be done through code, as illustrated in the following code snippet:

Uri ba = new Uri(“http://localhost:8080/WroxService”);
BasicHttpBinding binding = new BasicHttpBinding();
binding.CloseTimeout = 30000;
binding.OpenTimeout = 30000;
binding.SendTimeout = 30000;
binding.ReceiveTimeout = 30000;
ServiceHost host = new ServiceHost(serviceType, baseAddresses);
sh.AddServiceEndpoint(“”, binding, ba);

Notes
Windows Communication Foundation relies quite heavily on buffers. Creating and destroying buffers every time they are used is very expensive. Garbage collection for buffers is also very expensive. The solution is to employ buffer pools, which take a buffer from the pool, use it as needed, and then return it to the pool when the task is completed. This method still requires garbage collection cleanup but the overhead of creating and destroying buffers is eliminated.
The hostnameComparisonMode attribute is of the type HostnameComprisonMode enumeration of the System.ServiceModel namespace. The HostnameComparisonMode enumeration has the following members:

  • StrongWildCard ignores the hostname when performing URI matching. This means that a service is reachable via any valid hostname.
  • Exact requires that an exact match must be found with the URI specified. No equivalence matching between short hostnames and fully qualified domain names is performed.
  • WeakWildCard performs matches by ignoring the hostname if no strong match was found.
Tagged . Bookmark the permalink.

Leave a Reply