WCF Bindings
Bindings are a crucial component of Windows Communication Foundation (WCF) that define how a service endpoint communicates with another endpoint. They encapsulate the details of network transport, message encoding, and protocol handling. WCF provides a set of built-in bindings, and you can also create custom bindings.
Understanding Bindings
A binding in WCF is represented by a collection of binding elements. Each binding element handles a specific aspect of the communication stack. The order of these elements is important as it defines the pipeline through which messages flow.
Key Binding Elements:
- Transport Manager: Determines the underlying transport protocol (e.g., HTTP, TCP, MSMQ, Named Pipes).
- Message Encoder: Specifies how messages are serialized and deserialized (e.g., Text/XML, Binary, MTOM).
- Security: Configures security mechanisms like transport-level security (SSL/TLS) or message-level security (SAML, Kerberos).
- Reliability: Ensures reliable message delivery.
- Transactions: Supports distributed transactions.
Common Built-in Bindings
WCF offers several pre-configured bindings for common scenarios. These bindings simplify the configuration process by providing sensible defaults for their associated binding elements.
Binding Name | Transport | Encoding | Security | Use Case |
---|---|---|---|---|
BasicHttpBinding |
HTTP | Text/XML | None or Transport (HTTPS) | Interoperability with non-WCF clients, simple web services. |
WSHttpBinding |
HTTP | Text/XML | Transport or Message (WS-Security) | Web services adhering to WS-* standards. |
NetTcpBinding |
TCP | Binary | Transport (SSL/TLS) or Message | High-performance communication between .NET clients and services within the same network. |
NetNamedPipesBinding |
Named Pipes | Binary | None or Transport | High-performance inter-process communication on the same machine. |
MsmqBinding |
MSMQ | Binary or Text/XML | None or Message | Disconnected applications, reliable queuing. |
WebHttpBinding |
HTTP | JSON or XML | None or Transport (HTTPS) | RESTful services, JavaScript clients. |
Configuring Bindings
Bindings can be configured in several ways:
- Programmatically: By creating and configuring binding objects in your code.
- Declaratively (in `app.config` or `web.config`): By defining binding configurations in the service's configuration file. This is the most common approach for production applications.
Example: Programmatic Configuration of WSHttpBinding
using System.ServiceModel;
using System.ServiceModel.Channels;
// ...
// Create a WSHttpBinding
WSHttpBinding binding = new WSHttpBinding();
// Configure security settings
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
// Configure transport settings
binding.MaxReceivedMessageSize = 2147483647; // Set to a large value
// Create an endpoint address
Uri address = new Uri("http://localhost:8000/MyService");
// Create a service host
ServiceHost host = new ServiceHost(typeof(MyService), address);
// Add a service endpoint with the configured binding
host.AddServiceEndpoint(typeof(IMyService), binding, address);
// Open the host
host.Open();
Example: Declarative Configuration in web.config
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="MyWSHttpBinding">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="mex" binding="mexHttpBinding" />
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration="MyWSHttpBinding"
contract="MyNamespace.IMyService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<!-- Add other behaviors like authentication, authorization -->
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Note: When using web.config
or app.config
, the name
attribute for the binding is essential if you intend to reference it in your service endpoints.
Custom Bindings
For scenarios that require a specific combination of transport, encoding, or custom protocols, you can create custom bindings. This involves programmatically composing binding elements.
Key Takeaway: The choice of binding significantly impacts the performance, security, and interoperability of your WCF services. Carefully select or configure bindings that best match your application's requirements.
Explore the specific documentation for each built-in binding to understand its configuration options and best practices.