1 out of 1 rated this helpful - Rate this topic

Using the NetHttpBinding

.NET Framework 4.5

NetHttpBinding is a binding designed for consuming HTTP or WebSocket services and uses binary encoding by default. NetHttpBinding will detect whether it is used with a request-reply contract or duplex contract and change its behavior to match - it will use HTTP for request-reply contracts and WebSockets for duplex contracts. This behavior can be overridden using the WebSocketTransportUsage setting:

  1. Always - This forces WebSockets to be used even for request-reply contracts.

  2. Never - This prevents WebSockets from being used. Attempting to use a duplex contract with this setting will result in an exception.

  3. WhenDuplex - This is the default value and behaves as described above.

NetHttpBinding supports reliable sessions in both HTTP mode and WebSocket mode. In WebSocket mode sessions are provided by the transport.

Configuring a Service to use NetHttpBinding

The NetHttpBinding can be configured the same as any other binding. The following configuration snippet illustrates how to configure a WCF service with NetHttpBinding.

<system.serviceModel>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address=""
                  binding="netHttpBinding"
                  contract="WcfService1.IService1"/>
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
      </service>
    </services>
    <bindings>
      <netHttpBinding>
        <binding name="My_NetHttpBindingConfig">
          <webSocketSettings transportUsage="WhenDuplex"/>
        </binding>
      </netHttpBinding>
    </bindings>
    <!- ... --> 
  </system.serviceModel>

The following code snippet shows how to add the NetHtttpBinding in code.

ServiceHost svchost = new ServiceHost(typeof(Service1), baseAddress);
            NetHttpBinding binding = new NetHttpBinding();
            svchost.AddServiceEndpoint(typeof(IService1), binding, address); 
        }

See Also




Build Date:

2012-08-02
Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.