c++ - Azure 和 native 代码

标签 c++ azure clr-hosting

看起来您可以在 Azure 上托管 native 代码:http://msdn.microsoft.com/en-us/library/dd573362.aspx 。是否可以在这里运行套接字服务器(监听 tcp/udp)?甚至在顶部托管一个 CLR?

最佳答案

在辅助角色上运行套接字服务器很容易,但只能运行 tcp,不能运行 udp。您可以从辅助角色的 OnStart() 方法启动自己的进程,也可以从 Run() 方法执行此操作,但是一旦进入运行状态,负载均衡器和外界就会看到您的角色,因此您可能会获得 tcp套接字服务器运行之前的流量。

您需要在辅助角色的配置中创建一个 tcp 端点(右键单击辅助角色并查看“属性”):

alt text

您指定的端口号用于外部世界。负载均衡器将为您的每个角色实例提供一个您的代码将绑定(bind)到的唯一端口。例如,假设您的 MyApp.exe 在启动时采用 --tcpport 参数:

        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();

然后在您的 Run() 方法中,只需永远等待,知道您永远不应该退出:

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

关于c++ - Azure 和 native 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2938689/

相关文章:

Azure架构设计

c# - Cosmos DB 中是否支持使用 OData 进行分页?

c# - 托管 clr 并捕获线程异常

c++ - 显式模板特化问题

c++遗传算法变异错误

azure - 2012 年 10 月更新后, worker 角色继续回收

sql-server - 用于生成序列号的 SQL Server 函数

c++ - 什么类型的字符串最适合用于 Win32 和 DirectX?

c++ - 使用 decltype 初始化容器

.net - ProvideAssembly 可以从 CLR 主机加载 AppDomainManager 吗?