Usman ur Rehman Ahmed's blog

Windows Phone 7 ResolveHostNameAsync

The class DeviceNetworkInformation packaged within namespace Microsoft.Phone.Net.NetworkInformation provides two overloaded methods to resolve hostname asynchronously.

The overloads differ such that former allows you to specify the network interface using which hostname needs be resolved where as earlier will use the first avialable network interface in the list (If both WiFi and Ethernet are available then my understanding is that Ethernet will take precedence)

Here is a simple app demonstrating how to retrieve IPs against hostname.

Resolve-hostname
Note: Since call to method (ResolveHostNameAsync) is asynchronous in nature, the foreground thread won’t let you interact with the UI interface in the callback method  nameResolutionCallback(NameResolutionResult result). That's why you will see that interaction with UI is managed by call to dispatcher. However a more sophisticated approach will be to execute this call on the background thread.

Code behind

        private void btnResolveHostName_Click(object sender, RoutedEventArgs e)
        {
            if(!string.IsNullOrEmpty(txtHostName.Text))
            {
                DnsEndPoint endPoint = new DnsEndPoint(txtHostName.Text, 80);
                Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.ResolveHostNameAsync(endPoint, nameResolutionCallback, new object());
            }
        }

        public void nameResolutionCallback(NameResolutionResult result)
        {
            if (result.NetworkErrorCode == NetworkError.Success)
            {
                List<string> items = new List<string>();
                items.Add(string.Format("Network Interface Type : {0}", result.NetworkInterface.InterfaceType));

                foreach (IPEndPoint ipEndPoint in result.IPEndPoints)
                {
                    items.Add(string.Format("Resvoeld IP : {0}:{1}", ipEndPoint.Address.ToString(), ipEndPoint.Port.ToString()));
                }

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    lstInfo.ItemsSource = items;
                    txtHostName.Focus();
                });
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(result.NetworkErrorCode.ToString(), "Error", MessageBoxButton.OK);
                    txtHostName.Focus();
                });
            }
        }

XAML

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Resolve Hostname" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Name="txtHostName" Width="300" Margin="6,-5,150,623" Text="google.com"></TextBox>
            <Button Name="btnResolveHostName" Margin="300,-5,-5,623" Click="btnResolveHostName_Click">Resolve</Button>
            <ListBox Name="lstInfo" Margin="12,79,6,39" BorderThickness="1" BorderBrush="Azure"></ListBox>
        </Grid>
        </Grid>

 

Downloading Windows Phone SDK 7.1

Windows Phone 7.5 (Mango) software development kit is now available as SDK 7.1 and can be downloaded from,

    http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27570

A total of 503 MB in size with following features downloaded during this setup,

                1- Windows Phone Emulator x64

                2- Windows Phone SDK 7.1 Assemblies

                3- Microsoft Silverlight 4 SDK

                4- Windows Phone SDK 7.1 - ENU

                5- Windows Phone SDK 7.1 Add-in for Visual Studio 2010 - ENU

                6- Microsoft XNA Game Studio 4.0 Refresh

                7- Windows Phone SDK 7.1 Extensions for XNA

                8- WCF Data Services SDK for Windows Phone

                9- Microsoft Advertising SDK for Windows Phone

                10- Microsoft Expression Blend 4 for Windows

(download)

11
To Posterous, Love Metalab