This sample demonstrates using the WS2007HttpRelayBinding binding with reliable session enabled. It also shows how to specify Service Bus credentials in configuration instead of doing so programmatically.

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

The service project defines a simple session contract (IPingContract):

C# 
[ServiceContract(SessionMode = SessionMode.Required, Name = "IPingContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public interface IPingContract
{
    [OperationContract(IsInitiating = true, IsTerminating = false)]
    void Open();
 
    [OperationContract(IsInitiating = false, IsOneWay = true, IsTerminating = false)]
    void Ping(int count);
 
    [OperationContract(IsInitiating = false, IsTerminating = true)]
    void Close();
}

The Open operation is used to initiate a session. Ping is a one-way operation that can be called an arbitrary number of times. The Close operation terminates the sequence. The service implements this contract in the PingService class.

The endpoints for this service are defined in the application configuration file, as follows:

Xml 
<service name="Microsoft.ServiceBus.Samples.PingService">
    <endpoint name="ServiceBusEndpoint"
                       contract="Microsoft.ServiceBus.Samples.IPingContract"
                       bindingConfiguration="default"
                       binding="ws2007HttpRelayBinding"
                       behaviorConfiguration="sharedSecretClientCredentials" />
</service>


This endpoint is configured to use a binding of type WS2007HttpRelayBinding. It references (via the bindingConfiguration attribute) a binding configuration named default:

Xml 
 <bindings>
    <!-- Application Binding -->
    <ws2007HttpRelayBinding>
        <binding name="default">
            <reliableSession enabled="true" />
            <security mode="Transport" relayClientAuthenticationType="RelayAccessToken"/>
        </binding>
    </ws2007HttpRelayBinding>
</bindings>


The endopint also references a behavior configuration (via the behaviorConfiguration attribute) named sharedSecretClientCredentials.
This is where you can specify the issuer name and secret to be used by the service to authenticate with the Service Bus:

Xml 
<behaviors>      
    <endpointBehaviors>
        <behavior name="sharedSecretClientCredentials">
            <transportClientEndpointBehavior credentialType="SharedSecret">
                <clientCredentials>
                    <sharedSecret issuerName="ISSUER_NAME" issuerSecret="ISSUER_SECRET" />
                </clientCredentials>
            </transportClientEndpointBehavior>
        </behavior>
    </endpointBehaviors>
</behaviors>

Client

The client is configured (again, via the application configuration file) with the following endpoint:

Xml 
 <client>
    <!-- Application Endpoint -->
    <endpoint name="ServiceBusEndpoint"
                       binding="ws2007HttpRelayBinding"
                       contract="Microsoft.ServiceBus.Samples.IPingContract"
                       behaviorConfiguration="sharedSecretClientCredentials"
                       bindingConfiguration="default" />
</client>

In the code, an endpoint is opened.

C# 
Console.Write("Your Service Namespace: ");
string serviceNamespace = Console.ReadLine();

Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "PingService");
ChannelFactory channelFactory = new ChannelFactory("ServiceBusEndpoint", new EndpointAddress(serviceUri));

IPingContract channel = channelFactory.CreateChannel();
Console.WriteLine("Opening Channel.");
channel.Open();

After the ChannelFactory has been created, the client application creates a channel to the service and then interacts with it. Once the interaction is complete, the client closes the channel and the ChannelFactory, then exits.

Building and Running the Sample

Follow the steps below to run the application:

  1. Open the App.config files in both the Service and Client projects and replace the strings ISSUER_NAME and ISSUER_SECRET with the issuer name and secret you want to use.
    Note that you may use the same values in both projects or alternately, you can set up multiple issuers and use different values for the Service and Client.

  2. Build the Service and Client projects.

  3. From a command prompt with elevated privileges, run the service (Service\bin\Debug\Service.exe).

  4. When prompted, provide the service namespace you want to use.
    At this point, the service should indicate that it is listening at the configured address.

  5. From another command prompt, run the client (Client\bin\Debug\Client.exe).

  6. Provide the service namespace you want to connect to.

When finished, press Enter to exit the client and the service.

Expected Output – Client

 
Your Service Namespace: <service namespace>
Opening Channel.
Ping: 1
Ping: 2
Ping: 3
Ping: 4
Ping: 5
Ping: 6
Ping: 7
Ping: 8
Ping: 9
Ping: 10
Ping: 11
Ping: 12
Ping: 13
Ping: 14
Ping: 15
Ping: 16
Ping: 17
Ping: 18
Ping: 19
Ping: 20
Ping: 21
Ping: 22
Ping: 23
Ping: 24
Ping: 25
Closing Channel.

Expected Output – Service

 
Your Service Namespace: <service namespace>
Service address: https://<serviceNamespace>.servicebus.windows.net/PingService/
Press [Enter] to exit
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Opened.
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 1
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 2
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 3
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 4
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 5
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 6
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 7
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 8
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 9
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 10
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 11
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 12
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 13
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 14
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 15
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 16
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 17
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 18
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 19
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 20
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 21
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 22
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 23
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 24
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 25
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Closed.


Did you find this information useful? Please send your suggestions and comments about the documentation.