Overview Of Azure Service Bus - Relay

Azure Service Bus - Relay

This article explains about Azure Service Bus - Relay which has two entities - Hybrid Connection and WCF Relay. We will also see WCF Relay in detail.

A relay is used to communicate between on-premises applications and the outside world application. We can expose on-premises service endpoints to the public so that the outside world can access on-premise services from anywhere. We can have more than one listener for a single on-premise service endpoint. The biggest advantage of using this is that we have no need to open any port or any kind of firewall configuration. It uses Azure's network security without exposing our on-premise application.

Before we start implementing WCF Relay, let's revise WCF once.

Key points of WCF

Azure Service Bus - Relay 
  1. WCF Service exposes one or more endpoints
  2. An endpoint consists of ABC, i.e., Address, Binding, and Contract
  3. Address is a unique Uniform Resource Locator (URI) that identifies the location of the service. It defines the network address for sending and receiving the messages.
  4. Binding specifies which transport protocol to use, what message format and which any of the ws* protocols we want to use to a particular endpoint.
  5. Contract provides the additional details of the structuring contents of the various messages that will be used by the various operations exposed to the particular endpoints. 

Now, let's see the step by step implementation,

  1. Create Relay Namespace
  2. Create WCF Relay
  3. Create WCF Service
  4. Host WCF Service
  5. WCF Client

Create Relay Namespace

  • Log in to the Azure portal via portal.azure.com
  • Click on '+Create a resource', expand Integration and click on 'Relay'.

    Azure Service Bus - Relay
  • Click on 'Create' button to create the relay namespace.

    Azure Service Bus - Relay
  • Give a proper name to the namespace and click on 'Create'.

    Azure Service Bus - Relay

Create WCF Relay

Here, we can see two entities, i.e., Hybrid Connections and WCF Relays. But we are going to create WCF Relay.

Azure Service Bus - Relay 
  • Click on '+WCF Relay' to create a new relay.

    Azure Service Bus - Relay

  • Give a proper name to the relay.
  • Select 'NetTcp' as Relay Type and click on 'Create'.

    Azure Service Bus - Relay

Create WCF Service (Library)

  • Create a WCF Service Library application.
  • Create an operation contract which expects a string parameter.
  • Implement the contract and return the string.
    1. namespace WCFRelayLib  
    2. {  
    3.     [ServiceContract(Namespace = "https://recharge.servicebus.windows.net/rechargerelay")]  
    4.     public interface IRecharge  
    5.     {  
    6.         [OperationContract]  
    7.         string DoRecharge(string message);  
    8.     }  
    9.    
    10.     interface IRechargeChannel : IRecharge, IClientChannel { }  
    11. }     
    12. namespace WCFRelayLib  
    13. {  
    14.     public class Recharge : IRecharge  
    15.     {  
    16.         public string DoRecharge(string message)  
    17.         {  
    18.             return "Recharge is done for: " + message;  
    19.         }  
    20.     }  
    21. }  

Host WCF Service (Console Application)

  • Create a console application to host the previously created service.
  • Add a NuGet Pakage Microsoft.ServiceBus.
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <packages>  
    3.   <package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.0.0" targetFramework="net461" />  
    4.   <package id="ServiceBus.v1_1" version="1.0.6" targetFramework="net461" />  
    5. </packages>  
  • Expose NetTcpRelayBinding and host the service using ServiceHost.
    1. namespace WCFRelayHost  
    2. {  
    3.     class Program  
    4.     {  
    5.         static void Main(string[] args)  
    6.         {  
    7.             string scheme = "sb";  
    8.             string serviceNamespace = "recharge";  
    9.             string servicePath = "https://recharge.servicebus.windows.net/rechargerelay";  
    10.             string policy = "RootManageSharedAccessKey";  
    11.             string accessKey = "Kn1GO+dihGWMbMLEe6DfsuxJd6ptgvfQVG6EF6GivdY=";  
    12.    
    13.             Uri address = ServiceBusEnvironment.CreateServiceUri(scheme, serviceNamespace, servicePath);  
    14.    
    15.             ServiceHost sh = new ServiceHost(typeof(Recharge),address);  
    16.    
    17.             sh.AddServiceEndpoint(typeof(IRecharge), new NetTcpRelayBinding(),address)  
    18.                 .Behaviors.Add(new TransportClientEndpointBehavior  
    19.                 {  
    20.                     TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(policy, accessKey)  
    21.                 });  
    22.    
    23.             sh.Open();  
    24.    
    25.             Console.WriteLine("Press ENTER to close");  
    26.             Console.ReadLine();  
    27.    
    28.             sh.Close();  
    29.         }  
    30.     }  
    31. }  

WCF Client (Console Application)

  • Create a console application for WCF Client.
  • Add NuGet Pakage Microsoft.ServiceBus.
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <packages>  
    3.   <package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.0.0" targetFramework="net461" />  
    4.   <package id="ServiceBus.v1_1" version="1.0.6" targetFramework="net461" />  
    5. </packages>  
  • Get the ServiceName, ServicePath, Policy, and Accesskey from the portal.
  • Implement the client code as below.
    1. namespace WCFRelayLib  
    2. {  
    3.     [ServiceContract(Namespace = "https://recharge.servicebus.windows.net/rechargerelay")]  
    4.     public interface IRecharge  
    5.     {  
    6.         [OperationContract]  
    7.         string DoRecharge(string message);  
    8.     }  
    9.    
    10.     interface IRechargeChannel : IRecharge, IClientChannel { }  
    11.     class Program  
    12.     {  
    13.         static void Main(string[] args)  
    14.         {  
    15.             Console.WriteLine("-------------------------------------------------------");  
    16.             Console.WriteLine("Mobile Recharge");  
    17.             Console.WriteLine("-------------------------------------------------------");  
    18.             Console.WriteLine("Operators");  
    19.             Console.WriteLine("1. Vodafone");  
    20.             Console.WriteLine("2. Airtel");  
    21.             Console.WriteLine("3. JIO");  
    22.             Console.WriteLine("-------------------------------------------------------");  
    23.    
    24.             Console.WriteLine("Operator:");  
    25.             string mobileOperator = Console.ReadLine();  
    26.             Console.WriteLine("Amount:");  
    27.             string amount = Console.ReadLine();  
    28.             Console.WriteLine("Mobile:");  
    29.             string mobile = Console.ReadLine();  
    30.    
    31.             Console.WriteLine("-------------------------------------------------------");  
    32.    
    33.             switch (mobileOperator)  
    34.             {  
    35.                 case "1":  
    36.                     mobileOperator = "Vodafone";  
    37.                     break;  
    38.                 case "2":  
    39.                     mobileOperator = "Airtel";  
    40.                     break;  
    41.                 case "3":  
    42.                     mobileOperator = "JIO";  
    43.                     break;  
    44.                 default:  
    45.                     break;  
    46.             }  
    47.    
    48.             string message = mobileOperator + "*" + mobile + "*" + amount;  
    49.    
    50.             string scheme = "sb";  
    51.             string serviceNamespace = "recharge";  
    52.             string servicePath = "https://recharge.servicebus.windows.net/rechargerelay";  
    53.             string policy = "RootManageSharedAccessKey";  
    54.             string accessKey = "Kn1GO+dihGWMbMLEe6DfsuxJd6pEF6GivdY=";  
    55.    
    56.             ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;  
    57.    
    58.             var cf = new ChannelFactory<IRechargeChannel>(new NetTcpRelayBinding(),new EndpointAddress(ServiceBusEnvironment.CreateServiceUri(scheme, serviceNamespace,servicePath)));  
    59.    
    60.             cf.Endpoint.Behaviors.Add(new TransportClientEndpointBehavior { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(policy, accessKey) });  
    61.    
    62.             using (var ch = cf.CreateChannel())  
    63.             {  
    64.                 Console.WriteLine(ch.DoRecharge(message));  
    65.             }  
    66.         }  
    67.     }  
    68. }  
  • Run the WCF Client application.
  • Provide required inputs and you can see the response returned from the WCF service.

    Azure Service Bus - Relay

We can find our WCF Client as listener under WCF Relays.

Azure Service Bus - Relay 

For demo purposes, we have created all the applications under a single solution, but in the real world, we can put WCF Service and Client on two different servers and verify how an external application (WCFClient) uses on-premise service, i.e., WCF Service through Relay.

Download the complete sample code from here.


Similar Articles