Search code examples
c#azureazure-web-app-serviceazure-webjobsazure-hybrid-connections

Connect To Azure Web Job from Client over Hybrid Connection


I have an on premise server (mockhospital1) and I have an Azure Web App in an App Service Plan.

I have created a Hybrid Connection from this Azure Web App to link Server A, and I downloaded the HCM on Server A and added the connection there (this all works properly).

I am able to open a listener socket (using a c# console app) on mockhospital1:

        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        listener.Bind(new IPEndPoint(IPAddress.Any, 2113));
        listener.Listen(100);

I then created a webjob in my Azure Web App that sends to this socket over the HC:

        IPHostEntry ipHost = Dns.Resolve("mockhospital1");
        IPAddress ipAddress = ipHost.AddressList[0];
        IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, 2113);
        Socket sender = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sender.Connect(ipEndpoint);

This works well.

Now I am trying to connect the opposite way (initiating the send from mockhospital1 to Azure).

What I am trying to achieve is sending data over the same HC on port 2113 from mockhospital1 directly to a listener I have created in an Azure WebJob. I am struggling to find a way to do this. I'm not sure how to set up the sending socket.

This is what I have on my Azure WebJob (just setting up a listener on the correct port - 2113):

        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        listener.Bind(new IPEndPoint(IPAddress.Any, 2113));
        listener.Listen(100);

I am not sure how to connect to this to send from mockhospital1 though. I've tried the following code (I think I really need to know what IP_HOST_NAME should be):

        byte[] receivedBytes = new byte[1024];
        IPHostEntry ipHost = Dns.Resolve(IP_HOST_NAME);
        IPAddress ipAddress = ipHost.AddressList[0];
        IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, 2113);
        Socket sender = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sender.Connect(ipEndpoint);

I'm wondering if anyone has any idea how I can achieve what I am looking to do, or if I am going about the problem in the wrong way.


Solution

  • There are manifold restrictions in terms of network access from an Azure Web App.

    The only way an application can be accessed via the internet is through the already-exposed HTTP(80) and HTTPs(443) TCP ports, applications may not listen on other ports for packets arriving from the internet.

    However, applications may create a socket which can listen for connections from within the sandbox. For example, two processes within the same app may communicate with one another via TCP sockets; connection attempts incoming from outside the sandbox, albeit they be on the same machine, will fail.

    For more details, you could refer to this article.