Resolving Remote Windows Azure IP Addresses

AID: 8633
  • Status: Published

1960 points

  • Byazarc3
  • TypeTips/Tricks
  • Posted on2011-11-21 at 11:56:27
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct answer immediately. Good old IPAddress(). <-- Not really.  =/   Why not?  

The crux of the issue is this: IPAddress in the .NET Framework v4.x has no way to resolve a host name back to a valid IP address. You have to use the DNS class instead.

Here's the problem:
Whenever I ran the following code it always threw the following exception:
var targetHostAddress = "http://myStagingSubDomain.cloudapp.net";
var ipAddresses = Dns.GetHostAddresses(targetHostAddress); <-- exception was being thrown here...
var ipAddress = ipAddresses[0];

using (var client = new TcpClient())
{
    client.Connect(ipAddress, port);
    Console.WriteLine("{0}Connected to {1}:{2}.", Environment.NewLine, ipAddress.ToString(), port.ToString());
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window


The Exception:
No such host is known --> System.Net.Sockets.SocketError.HostNotFound

Great. Then I realized that it just might be because I was including the address as presented in the browser... so what if I shaved off the protocal and trailing slash? Eureka! That worked!

All's good then, right? Nope. You have to do just a little more juggling to accomodate the possible peculiarities in your network/subdomain/whatever. How do I know this? Because when substituted "localhost" or "127.0.0.1" for the address I still got the exception : "No connection could be made because the target machine actively refused it 127.0.0.1:4201"

Ok, Here's The Scenario:
1. We are deploying a service with multiple TCP/IP listeners to Windows Azure.
2. One of those listeners is the target.
3. We know Windows Azure IP addresses are subject to change without warning.
4. We also know that eventually that switching could be "on purpose", or rather the result of load balancing and not security/maintenance.
5. We need to find the IP address automatically and without human interaction.
6. We need to be able to test the solution locally, then deploy it to the cloud without changing anything but the targetHostAddress.
var targetHostAddress = "localhost"; // "{substitute_your_windows_azure_host_name_instead_here--don't_include_the_protocol_or_trailing_slash}";
var ipAddresses = Dns.GetHostAddresses(targetHostAddress);
var ipAddress = !targetHostAddress.Contains("localhost") ? ipAddresses[0] : ipAddresses[1];
                                    
1:
2:
3:

Select allOpen in new window


... notice the host name test in the 3rd line? That's important (at least in my office anyway...), because ipAddress[0] was actually "::1", NOT "127.0.0.1" like I expected. For the life of me I couldn't figure out why I kept getting that exception until I added a few more breakpoints. Never assume that would should be is... trust, but verify.

I hope this saves you some valuable time. I know it would've saved me at least an hour between deployments and testing.  =/
Asked On
2011-11-21 at 11:56:27ID8633
Tags

windows azure

,

ip address

Topic

.NET

Views
1322

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top .NET Programming Experts

  1. CodeCruiser

    588,856

    Sage

    6,000 points yesterday

    Profile
    Rank: Genius
  2. kaufmed

    377,702

    Wizard

    10 points yesterday

    Profile
    Rank: Genius
  3. BuggyCoder

    268,007

    Guru

    1,600 points yesterday

    Profile
    Rank: Sage
  4. TheLearnedOne

    232,552

    Guru

    4,900 points yesterday

    Profile
    Rank: Savant
  5. Idle_Mind

    193,005

    Guru

    0 points yesterday

    Profile
    Rank: Savant
  6. JamesBurger

    156,812

    Guru

    2,000 points yesterday

    Profile
    Rank: Sage
  7. wdosanjos

    124,308

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  8. Dhaest

    115,720

    Master

    0 points yesterday

    Profile
    Rank: Genius
  9. sedgwick

    112,918

    Master

    1,600 points yesterday

    Profile
    Rank: Genius
  10. nepaluz

    101,325

    Master

    0 points yesterday

    Profile
    Rank: Sage
  11. MlandaT

    95,921

    Master

    2,100 points yesterday

    Profile
    Rank: Genius
  12. navneethegde

    74,442

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  13. Masteraco

    70,367

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  14. binaryevo

    70,365

    Master

    0 points yesterday

    Profile
    Rank: Guru
  15. ambience

    69,104

    Master

    0 points yesterday

    Profile
    Rank: Sage
  16. emoreau

    68,230

    Master

    0 points yesterday

    Profile
    Rank: Genius
  17. PaulHews

    49,486

    0 points yesterday

    Profile
    Rank: Genius
  18. AndyAinscow

    45,290

    0 points yesterday

    Profile
    Rank: Genius
  19. Chinmay_Patel

    43,411

    0 points yesterday

    Profile
    Rank: Genius
  20. ged325

    41,700

    2,600 points yesterday

    Profile
    Rank: Genius
  21. RolandDeschain

    41,317

    0 points yesterday

    Profile
    Rank: Sage
  22. nishantcomp2512

    39,486

    0 points yesterday

    Profile
    Rank: Wizard
  23. tommyBoy

    36,550

    0 points yesterday

    Profile
    Rank: Genius
  24. mroonal

    35,000

    0 points yesterday

    Profile
    Rank: Sage
  25. santhimurthyd

    34,650

    0 points yesterday

    Profile
    Rank: Wizard

Hall Of Fame