0

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.

        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.

Please suggest me some good logic here

Thanks in advance.

oberfreak
  • 1,799
  • 13
  • 20
Arun Rana
  • 8,426
  • 14
  • 67
  • 107
  • possible duplicate of [C# How can I check if a URL exists/is valid?](http://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid) – V4Vendetta Nov 30 '11 at 10:31
  • 1
    possible duplicate of [Azure Detect Staging vs Production](http://stackoverflow.com/questions/6261586/azure-detect-staging-vs-production) – Jeremy McGee Nov 30 '11 at 10:37
  • How about using System.Net.NetworkInformation.Ping class? – Amit Nov 30 '11 at 13:11

4 Answers4

1

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

which provides a more robust answer to your real question.

Community
  • 1
  • 1
Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
0

How about initiating a socket that tries to connect to the server over port 80 (HTTP)? that'll be a good indication whether the site is up or not.

Shai
  • 25,159
  • 9
  • 44
  • 67
0

The only way to "ping" a resource is to get an error code. just make sure it's just a get operation so that no change occurs on the server.

Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84
0

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. Then you can just do

If (ConfigurationManager.AppSettings["Envirmonment"] == "Live")
{
    //do live stuff
}
else
{
    //do testing stuff.
}
Ben Robinson
  • 21,601
  • 5
  • 62
  • 79