简体   繁体   English

如何从背后的代码ping URL

[英]How to ping URL from code behind

I have one issue that i want to ping URL from code behind (EXE project) by which i can know environment of Azure application on which my EXE is running. 我有一个问题,我想从(EXE项目)后面的代码ping URL,通过它我可以知道运行EXE的Azure应用程序的环境。

        string dep = RoleEnvironment.DeploymentId.ToString();
        WebRequest req = WebRequest.Create("http://" + dep + ".cloudapp.net");
        HttpWebResponse rep= null;
        try
        {
            rep = (HttpWebResponse)req.GetResponse();
        }
        catch (Exception)
        {

           //if environment is production then this URL will not work
           //do some functionality
        }

So by above code i would like to do some functionality when it will production environment ,above logic is works well but would like to get some perfect solution like ping command that if URL exist then return true otherwise false. 因此,通过以上代码,我想在生产环境中实现一些功能,上述逻辑效果很好,但是希望获得一些完美的解决方案,例如ping命令,如果URL存在,则返回true,否则返回false。

Please suggest me some good logic here 请在这里建议我一些合理的逻辑

Thanks in advance. 提前致谢。

No, I don't think that will work - if you have production code running then the check will always be successful, even if you call it from staging. 不,我认为这行不通-如果您正在运行生产代码,那么即使您从暂存阶段调用它,检查也将始终成功。

Check the answer to this: 检查答案:

Azure Detect Staging vs Production Azure检测阶段与生产

which provides a more robust answer to your real question. 可以为您的实际问题提供更可靠的答案。

How about initiating a socket that tries to connect to the server over port 80 (HTTP)? 如何启动尝试通过端口80(HTTP)连接到服务器的套接字? that'll be a good indication whether the site is up or not. 这将很好地指示该网站是否正常运行。

The only way to "ping" a resource is to get an error code. “ ping”资源的唯一方法是获取错误代码。 just make sure it's just a get operation so that no change occurs on the server. 只需确保它只是一个get操作,以使服务器上没有任何更改。

You know when you deploy whether the app is deployed to live or test/staging so why don't you just set something on the web.config. 您知道在部署时是将应用部署为实时运行还是进行测试/登台,为什么不在web.config上进行设置。 Then you can just do 那你就可以做

If (ConfigurationManager.AppSettings["Envirmonment"] == "Live")
{
    //do live stuff
}
else
{
    //do testing stuff.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM