1

我在 Azure 开发结构下托管一个 IIS 应用程序。

当应用程序部署到 Azure 计算模拟器时,会创建一个临时 IIS 应用程序,该应用程序在 5100 附近的端口上进行侦听。来自公共终结点的传入请求将重定向到此端口。

不过,Azure 开发结构似乎并不总是使用项目配置中已声明的公共端口。因此,例如,我们的应用程序应该公开一个公共端口 80——但是当我运行它时,它几乎总是端口 81,但有时是端口 82,等等。

所以我可以确保在我的应用程序中创建的 URL 是正确的,我想知道这个外部端口是什么。

不幸的是,我不能简单地查看Request.Url.Port,因为这是临时应用程序的端口号——通常是 5100。RoleEnvironment.CurrentRoleInstance.InstanceEndpoints,也不起作用,因为它也返回从服务器看到的端口,5100 及以下。

4

4 回答 4

1

通过使用 Reflector 查看 csrun.exe 来解决这个问题。

似乎 SDK DLLMicrosoft.ServiceHost.Tools.DevelopmentFabric是关键,尤其是方法FabricClient.GetServiceDeployments()FabricClient.GetServiceInformation(). 所以:

using System; using Microsoft.ServiceHosting.Tools.DevelopmentFabric;

class Program
{
    static void Main(string[] args)
    {
        FabricClient client = FabricClient.CreateFabricClient();

        foreach (string tenantName in client.GetServiceDeployments())
        {
            var information = client.GetServiceInformation(tenantName);
            foreach (var item in information)
            {
                Console.WriteLine(string.Format("{0} {1} {2} {3}", item.ContractName, item.InterfaceName, item.UrlSpecification, item.Vip));
            }
        }

        Console.ReadLine();
    }
}

我所追求的是作为item.Vip.

请注意,显然,这仅适用于开发结构......但这就是我在这里寻找的东西。

于 2011-07-18T14:23:32.540 回答
0

你试过用

RoleEnvironment.CurrentRoleInstance.InstanceEndpoints

我不确定自动取款机,因为我不在我的电脑后面,但很快会再次检查。无论如何,在每个端点上都有一个属性 IPEndPoint,它也有端口。如果您也获得了公共端点(我认为您是为当前角色实例做的),您应该能够从那里获得地址。

希望能帮助到你...

于 2011-07-18T05:07:19.180 回答
0

我认为“错误”端口(如 81/82 等)的原因是所需端口正忙。

可能您有一些其他应用程序正在侦听端口 80,因此它永远不可用。此外,我已经多次看到模拟器中的 Azure 计算实例没有足够快地关闭,所以如果你启动一个新实例,它会获得下一个端口,等等。如果我杀了它,请稍等一下,然后重新开始 - 它会得到想要的港口。

它不应该在生产中成为问题,这可能是它不通过 API 公开的原因。在测试期间,只需确保您没有与其他应用程序发生冲突,并留出一些时间让计算笔记干净地关闭并释放端口。

于 2011-07-18T08:29:04.137 回答
0

我无法让开发结构程序集为我工作(看起来 API 已更改),因此我求助于解析“csrun /status”的输出来查找给定角色名称的 IP 地址。这是获取 IP 的一些代码,但您必须做一些额外的工作才能获取端口。

public static string GetEmulatorIPAddress(string roleName)
    {
        var psi = new ProcessStartInfo();
        psi.FileName = @"C:\Program Files\Microsoft SDKs\Windows Azure\Emulator\csrun.exe";
        psi.Arguments = "/status";
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.UseShellExecute = false;
        psi.CreateNoWindow = true;

        StringBuilder sb = new StringBuilder();

        DataReceivedEventHandler errorHandler = (object sender, DataReceivedEventArgs e) =>
            {

            };

        string lastIPAddress = null;
        string foundIPAddress = null;

        DataReceivedEventHandler dataHandler = (object sender, DataReceivedEventArgs e) =>
        {
            string line = e.Data;
            if (line != null && foundIPAddress == null)
            {
                if (line.StartsWith("EndPoint: http://"))
                {
                    int ipStart = line.IndexOf("://") + "://".Length;
                    int ipEnd = line.IndexOf(":", ipStart);
                    lastIPAddress = line.Substring(ipStart, ipEnd - ipStart);
                }

                if (line.Trim() == roleName)
                {
                    foundIPAddress = lastIPAddress;
                }
            }
        };

        Process p = new Process();
        p.StartInfo = psi;
        p.ErrorDataReceived += errorHandler;
        p.OutputDataReceived += dataHandler;
        p.EnableRaisingEvents = true;
        p.Start();
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();

        p.WaitForExit();

        return foundIPAddress;
    }
于 2014-04-17T16:05:43.763 回答