c++azureclr-hosting

Azure and native code


It looks like you can host native code on Azure: http://msdn.microsoft.com/en-us/library/dd573362.aspx. Is it possible to run a socket server (listening tcp/udp) here? And even hosting a CLR on top?


Solution

  • It's easy to run a socket server on a worker role, but only tcp, not udp. You can start your own process from the worker role's OnStart() method You can do it from the Run() method too but once you hit the run state, your role is seen by the load balancer and outside world, so you might get tcp traffic before your socket server is running.

    You'll need to create a tcp endpoint in your worker role's configuration (right-click the worker role and view Properties):

    alt text

    That port number you specify is for the outside world. The load balancer will give each of your role's instances a unique port that your code will bind to. For example, imagine your MyApp.exe that takes a --tcpport parameter on startup:

            var rootDirectory = Path.Combine(Environment.GetEnvironmentVariable("RoleRoot") + "\\", "approot\\MyApp");
            int port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["MyExternalEndpoint"].IPEndpoint.Port;
            var cmdline = String.Format("--tcpport {0}",port);
            MyProcess = new Process()
                {
                    StartInfo = new ProcessStartInfo(Path.Combine(rootDirectory, "myapp.exe"), cmdline)
                    {
                        UseShellExecute = false,
                        WorkingDirectory = rootDirectory
                    }
                };
                MyProcess.Start();
    

    Then in your Run() method, simply wait forever, knowing you should never exit:

    MyProcess.WaitForExit();
    throw new Exception("MyApp quit on me!");