This sample demonstrates how to configure the NetTcpRelayBinding binding to support the Hybrid connection mode which first establishes a relayed connection, and if possible, switches automatically to a direct connection between a client and a service.
Prerequisites
If you haven't already done so, read the release notes document that explains how to sign up for a Windows Azure account and how to configure your environment.
Service
In general, there are four steps to building a Service Bus service:
- Define a contract.
- Implement the contract in a service.
- Define endpoints for the service.
- Host the service.
In this sample, the service project defines
HelloService
and a simple contract named
IHelloContract
:
C# | |
---|---|
[ServiceContract(Name = "IHelloContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")] public interface IHelloContract { [OperationContract(IsOneWay = true)] void Hello(string text); } |
The service implements this contract in the
HelloService
class.
The endpoints for this service are defined in the application configuration file. Specifically, the following endpoint is defined:
Xml | |
---|---|
<service name="Microsoft.ServiceBus.Samples.HelloService"> <endpoint name="RelayEndpoint" contract="Microsoft.ServiceBus.Samples.IHelloContract" binding="netTcpRelayBinding" bindingConfiguration="default" address="" /> </service> |
This endpoint is configured to use a binding of type NetTcpRelayBinding. It references a binding configuration called "default" and specifies that the connection mode is "Hybrid."
The Hybrid connection mode first establishes a relayed connection, and if possible, switches automatically to a direct connection between a client and service.
Xml | |
---|---|
<bindings> <netTcpRelayBinding> <binding name="default" connectionMode="Hybrid" /> </netTcpRelayBinding> </bindings> |
Client
The client project defines HelloClient
. In the
application configuration file, the client is configured with the following
endpoint:
Xml | |
---|---|
<client> <endpoint name="RelayEndpoint" contract="Microsoft.ServiceBus.Samples.IHelloContract" binding="netTcpRelayBinding" bindingConfiguration="default" /> </client> |
In the code, an endpoint is opened.
C# | |
---|---|
Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "HelloService"); ChannelFactory<IHelloChannel> channelFactory = new ChannelFactory<IHelloChannel>("RelayEndpoint", new EndpointAddress(serviceUri)); channelFactory.Endpoint.Behaviors.Add(relayCredentials); IHelloChannel channel = channelFactory.CreateChannel(); channel.Open(); |
After the ChannelFactory
has been created, the client
application creates a channel to the service and then proceeds to interact with
it by calling channel.Hello("Hello")
numerous times and calculating
the time between each message response.
When a direct connection is established, throughput is significantly increased
Building and Running the Sample
After building the solution, perform the following steps to run the application:
- From a command prompt, run the service (Service\bin\Debug\Service.exe).
- When prompted, enter your service namespace, issuer name and issuer key. At this point, the
service should indicate that it is listening at the configured
address.
- In another command prompt window, run the client
(Client\bin\Debug\Client.exe).
- You will be prompted for the service namespace, issuer name and issuer key.
- When finished, press ENTER to exit the client and the service.
If a direct connection is established, you will see an increased number of sent messages.
Did you find this information useful? Please send your suggestions and comments about the documentation.