This sample demonstrates using the NetTcpRelayBinding binding.
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 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 an interaction.
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. Specifically, the following endpoint is defined:
Xml | |
---|---|
<service name="Microsoft.ServiceBus.Samples.PingService"> <endpoint name="RelayEndpoint" contract="Microsoft.ServiceBus.Samples.IPingContract" binding="netTcpRelayBinding" /> </service> |
Client
The client is configured (also in the application configuration file) with the following endpoint:
Xml | |
---|---|
<client> <endpoint name="RelayEndpoint" contract="Microsoft.ServiceBus.Samples.IPingContract" binding="netTcpRelayBinding" /> </client> |
In the code, an endpoint is opened.
C# | |
---|---|
Console.Write("Your Service Namespace: ");
string serviceNamespace = Console.ReadLine();
Console.Write("Your Issuer Name: ");
string issuerName = Console.ReadLine();
Console.Write("Your Issuer Secret: ");
string issuerSecret = Console.ReadLine();
Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "PingService");
TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);
ChannelFactory<IPingContract> channelFactory = new ChannelFactory<IPingContract>("RelayEndpoint", new EndpointAddress(serviceUri));
channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
IPingContract channel = channelFactory.CreateChannel();
Console.WriteLine("Opening Channel.");
channel.Open(); |
After the ChannelFactory
is created, the client
application creates a channel to the service and interacts with it.
After the interaction is complete, the client closes both the
channel and the ChannelFactory
, and then exits.
Building and Running the Sample
After building the solution, do the following to run the application:
- From a command prompt, run the service
(Service\bin\Debug\Service.exe). You will be prompted for your service namespace, your issuer name and
secret.
- From another command prompt, run the client
(Client\bin\Debug\Client.exe). You will be prompted for your service namespace, your issuer name and
secret.
- When finished, press ENTER to exit the client and the
service.
Did you find this information useful? Please send your suggestions and comments about the documentation.