Overview Of Azure Service Bus - Relay

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

image.png

  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'.

image.png

Click on 'Create' button to create the relay namespace.

image.png

Give a proper name to the namespace and click on 'Create'.

image.png

Create WCF Relay

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

image.png

Click on '+WCF Relay' to create a new relay.

image.png

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

image.png

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.
    namespace WCFRelayLib  
    {  
        [ServiceContract(Namespace = "https://recharge.servicebus.windows.net/rechargerelay")]  
        public interface IRecharge  
        {  
            [OperationContract]  
            string DoRecharge(string message);  
        }  

        interface IRechargeChannel : IRecharge, IClientChannel { }  
    }     
    namespace WCFRelayLib  
    {  
        public class Recharge : IRecharge  
        {  
            public string DoRecharge(string message)  
            {  
                return "Recharge is done for: " + message;  
            }  
        }  
    }

Host WCF Service (Console Application)

  • Create a console application to host the previously created service.
  • Add a NuGet Pakage Microsoft.ServiceBus.
<?xml version="1.0" encoding="utf-8"?>  
<packages>  
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.0.0" targetFramework="net461" />  
  <package id="ServiceBus.v1_1" version="1.0.6" targetFramework="net461" />  
</packages>
  • Expose NetTcpRelayBinding and host the service using ServiceHost.
    namespace WCFRelayHost  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                string scheme = "sb";  
                string serviceNamespace = "recharge";  
                string servicePath = "https://recharge.servicebus.windows.net/rechargerelay";  
                string policy = "RootManageSharedAccessKey";  
                string accessKey = "Kn1GO+dihGWMbMLEe6DfsuxJd6ptgvfQVG6EF6GivdY=";  

                Uri address = ServiceBusEnvironment.CreateServiceUri(scheme, serviceNamespace, servicePath);  

                ServiceHost sh = new ServiceHost(typeof(Recharge),address);  

                sh.AddServiceEndpoint(typeof(IRecharge), new NetTcpRelayBinding(),address)  
                    .Behaviors.Add(new TransportClientEndpointBehavior  
                    {  
                        TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(policy, accessKey)  
                    });  

                sh.Open();  

                Console.WriteLine("Press ENTER to close");  
                Console.ReadLine();  

                sh.Close();  
            }  
        }  
    }

WCF Client (Console Application)

  • Create a console application for WCF Client.
  • Add NuGet Pakage Microsoft.ServiceBus.
    <?xml version="1.0" encoding="utf-8"?>  
    <packages>  
      <package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.0.0" targetFramework="net461" />  
      <package id="ServiceBus.v1_1" version="1.0.6" targetFramework="net461" />  
    </packages>
  • Get the ServiceName, ServicePath, Policy, and Accesskey from the portal.
  • Implement the client code as below.
namespace WCFRelayLib  
{  
    [ServiceContract(Namespace = "https://recharge.servicebus.windows.net/rechargerelay")]  
    public interface IRecharge  
    {  
        [OperationContract]  
        string DoRecharge(string message);  
    }  

    interface IRechargeChannel : IRecharge, IClientChannel { }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Console.WriteLine("-------------------------------------------------------");  
            Console.WriteLine("Mobile Recharge");  
            Console.WriteLine("-------------------------------------------------------");  
            Console.WriteLine("Operators");  
            Console.WriteLine("1. Vodafone");  
            Console.WriteLine("2. Airtel");  
            Console.WriteLine("3. JIO");  
            Console.WriteLine("-------------------------------------------------------");  

            Console.WriteLine("Operator:");  
            string mobileOperator = Console.ReadLine();  
            Console.WriteLine("Amount:");  
            string amount = Console.ReadLine();  
            Console.WriteLine("Mobile:");  
            string mobile = Console.ReadLine();  

            Console.WriteLine("-------------------------------------------------------");  

            switch (mobileOperator)  
            {  
                case "1":  
                    mobileOperator = "Vodafone";  
                    break;  
                case "2":  
                    mobileOperator = "Airtel";  
                    break;  
                case "3":  
                    mobileOperator = "JIO";  
                    break;  
                default:  
                    break;  
            }  

            string message = mobileOperator + "*" + mobile + "*" + amount;  

            string scheme = "sb";  
            string serviceNamespace = "recharge";  
            string servicePath = "https://recharge.servicebus.windows.net/rechargerelay";  
            string policy = "RootManageSharedAccessKey";  
            string accessKey = "Kn1GO+dihGWMbMLEe6DfsuxJd6pEF6GivdY=";  

            ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;  

            var cf = new ChannelFactory<IRechargeChannel>(new NetTcpRelayBinding(),new EndpointAddress(ServiceBusEnvironment.CreateServiceUri(scheme, serviceNamespace,servicePath)));  

            cf.Endpoint.Behaviors.Add(new TransportClientEndpointBehavior { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(policy, accessKey) });  

            using (var ch = cf.CreateChannel())  
            {  
                Console.WriteLine(ch.DoRecharge(message));  
            }  
        }  
    }  
}
  • Run the WCF Client application.
  • Provide required inputs and you can see the response returned from the WCF service.

image.png

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

image.png

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 .