在Azure Compute Emulator中外部公开端口

问题描述 投票:0回答:1

我在C#中创建了一个Azure Cloud Service TCP侦听器,现在我尝试在本地调试它。下面的服务定义公开了我感兴趣的端口,但是当它在本地运行时,地址被绑定到localhost,这对同一网络上的其他设备是不可见的。

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="Foo.Listener.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6">
  <WorkerRole name="Foo.Listener" vmsize="Small">
    <ConfigurationSettings>
      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" />
    </ConfigurationSettings>
    <Endpoints>
      <InputEndpoint name="Foo Protocol" protocol="tcp" port="9100" localPort="9100" />
    </Endpoints>
  </WorkerRole>
</ServiceDefinition>

我尝试使用以下命令添加端口代理:

netsh interface portproxy add v4tov4 listenport=9100 connectaddress=localhost connectport=9100 protocol=tcp

但是,当我这样做时,计算模拟器似乎认为端口号9100正在使用,它选择9101。如何在本地运行我的辅助角色,并允许它接受外部连接?

c# azure tcp port azure-cloud-services
1个回答
0
投票

我设法通过添加工作者角色的检查来实现这一点。它不漂亮,我宁愿通过配置而不是通过代码完成这个,但这是我的解决方案:

IEnumerable<IPEndPoint> endpoints;

if (RoleEnvironment.IsEmulated)
{
    endpoints = Dns.GetHostEntry(Dns.GetHostName())
        .AddressList
        .Select(address => new IPEndPoint(address, 9100));
}
else
{
    endpoints = RoleEnvironment.CurrentRoleInstance
        .InstanceEndpoints
        .Select(endpoint => endpoint.Value.IPEndpoint);
}
© www.soinside.com 2019 - 2024. All rights reserved.