This topic has not yet been rated - Rate this topic

How to: Change the Connection Mode

The connection mode defines how your Windows Azure 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 the Service Bus.

  • Hybrid – the initial connection between the applications uses the 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 the 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: the 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, the 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 an 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):");
    
    

Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.