How to determinate if the connection to internet is OK

A

ad

Hi,
It will fail to call a web service when the connection to internet is not
OK.

I wnant to check the connection to internet before call a web service.

How to determinate if the connection to internet is OK?
 
H

hashimisayed

Hi,

The Offline Application Block provides a mechanism to determine if you
have an internet connection. Here is the class that implements this:


using System;
using System.Runtime.InteropServices;
using System.Xml.XPath;
using System.Configuration;
using Microsoft.ApplicationBlocks.Common;

namespace Microsoft.ApplicationBlocks.SmartClient.Offline
{
/// <summary>
/// This is a simple implementation of a ConnectionDetectionStrategy.
It uses the WinINet
/// function GetInternetConnectionState to detect whether you are
actually connected to
/// another system anywhere. This strategy is not smart enough to tell
if you can talk
/// to a particular host -- it merely checks to see that you can talk
to *any* host.
/// This means that there is an ethernet connection to another
computer or a network,
/// or that a modem is connected.
/// </summary>
public class WinINetDetectionStrategy
{
private int pollInterval;

/// <summary>
/// Public constant used in test code.
/// </summary>
public const int MinimumPollingIntervalInSeconds = 1;

[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int
connectionDescription, int reservedValue ) ;

/// <summary>
/// Getter property to retrieve the polling interval in seconds
/// </summary>
/// <value>Polling interaval in seconds</value>
public int PollInterval { get { return pollInterval; }}

/// <summary>
/// Query method to cause provider to actively detect the connection
state
/// </summary>
/// <returns>True if provider believes we are connected</returns>
public bool IsConnected()
{
int connectionDescription = 0;
return InternetGetConnectedState(out connectionDescription, 0);
}

/// <summary>
/// IProvider.Initialize method implementation. Retrieves
configuration information from app.config file
/// </summary>
/// <param name="configurationNode">XmlNode to parse for
configuration information</param>
public void Initialize(XPathNavigator configurationNode)
{
XPathNodeIterator iter =
configurationNode.Select("pollingInterval");
iter.MoveNext();

pollInterval = GetCurrentPollingInterval(iter.Current.Value);
if(pollInterval < MinimumPollingIntervalInSeconds)
{
throw new
ConfigurationException(ProvidersResourceTable.GetString(ProvidersResourceTable.ConnectionManagerPollingIntervalMessage));
}
}

private int GetCurrentPollingInterval(string pollingIntervalString)
{
try
{
return Convert.ToInt32(pollingIntervalString);
}
catch(FormatException e)
{
throw new
ConfigurationException(ProvidersResourceTable.GetString(ProvidersResourceTable.PollingIntervalShouldBeNumeric),
e);
}
}
}
}


If you use this class, then you'll want to call the IsConnected()
method to determine connectivety. You can learn more about the Offline
block at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/offline-ch01.asp


Thanks,

Sayed Y. Hashimi

http://www.sayedhashimi.com
Shameless Book Plug: Service-Oriented Smart Clients with .NET 2.0
http://www.amazon.com/exec/obidos/t...f=sr_1_1/102-5068238-6758524?v=glance&s=books
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top