Export (0) Print
Expand All

How to: Change the Connection Mode

Updated: June 18, 2014

The connection mode defines how your Service Bus application connects to other applications when it uses a TCP connection: either Relayed or Hybrid:

  • Relayed – all communications between the service and client applications use Service Bus.

  • Hybrid – the initial connection between the applications uses Service Bus. However, if the two applications can connect to each other, they will attempt to do this. If at any time this connection is lost, the applications will revert to communicating through Service Bus.

The connection mode is defined by the TcpRelayConnectionMode enumeration, which is in turn used by TcpRelayTransportBindingElement to define the NetTcpRelayBinding binding. Therefore, you can set the connection mode for a NetTcpRelayBinding binding by setting the ConnectionMode property. You can do this at any time in your application: Service Bus will take the change and attempt to modify the connection. As with most properties, you can decide to set the connection status in the App.config file. Finally, Service Bus exposes the current status of the connection through the IHybridConnectionStatus interface, which you can decide to implement.

The following topic describes how to determine or modify the mode of a Service Bus application.

To modify the connection mode programmatically

  1. Create the binding, and then set the property.

    NetTcpRelayBinding myTcpBinding = new NetTcpRelayBinding();
    myTcpBinding.ConnectionMode = TcpRelayConnectionMode.Hybrid;
    

To modify the connection mode with an App.config file

  1. Open the application configuration file for the client application, as in the following example.

    <bindings>
      <netTcpRelayBinding>
        <binding name="default"/>
      </netTcpRelayBinding>
    </bindings>
    
    

    Note that the netTcpRelayBinding tag does not specify the connection mode attribute. By default, the connection mode used is Relayed.

  2. Explicitly change the connection mode to Hybrid by adding the connectionMode field, which is set to Hybrid.

    <bindings>
      <netTcpRelayBinding>
        <binding name="default" connectionMode="Hybrid">
          <security mode="None" />
        </binding>
      </netTcpRelayBinding>  
    </bindings>
    
    

To determine when the connection mode changes

  1. Optionally, you might want to receive a notification when the connection between the connected endpoints changes from a Relayed connection to a Hybrid connection.

    channel.Open();
    
    IHybridConnectionStatus hybridConnectionStatus = channel.GetProperty<IHybridConnectionStatus>();
    if (hybridConnectionStatus != null)
    {
        hybridConnectionStatus.ConnectionStateChanged += (o, e) =>
        {
            Console.WriteLine("Connection state changed to: {0}.", e.ConnectionState);
        };
    }
    
    Console.WriteLine("Enter text to echo (or [Enter] to exit):");
    
    

Community Additions

ADD
Show:
© 2014 Microsoft