free geoip Creating Network Aware Applications -- System.Net.NetworkInformation - Jayson's Blog - jaysonKnight.com
jaysonKnight.com
A conduit to the voices inside my head.
Creating Network Aware Applications -- System.Net.NetworkInformation

I was recently in the process of rolling my own library to be able to drop into an application to make it "network aware" i.e. the application can periodically check to make sure it still has a network connection...I've seen plenty of apps in the past that completely lock up if there is even the slightest network hiccup; invariably you have to zap it with task manager with the caveat of losing whatever you were working on.

The library itself was to be easy enough...just execute a simple ping request on a threading timer at a specified interval, if the request fails raise an event letting calling code know that there is a problem with the network connection.  Sounds easy enough, but about an hour into it I thought "there has got to be an easier way outside of doing raw Win32 or WMI hookups."

Lo and behold there is: Enter the System.Net.NetworkInformation namespace, which is new to .Net 2.0.  In it you'll find a variety of classes dealing with numerous aspects of of how your machine is connected to the network, details about the network interface itself, and statistics gathering for network traffic.

The core class for checking on network availability is the NetworkChange class, which contains 2 core events: the NetworkAddressChanged event and the NetworkAvailabilityChanged event.  By hooking up to these events, you can get (almost) real-time notifications of network changes, and thus provide your users with a transparent experience should network connectivity be interrupted (i.e. saving their work, switching to a local data repository, etc)  You can also proactively check network availability by polling the NetworkInterface.GetIsNetworkAvailable method, which returns true if any NIC on the machine has a connection deemed "up and running" by the .Net framework.  And of course the trusty Ping class resides in this namespace as well, which was one of the most requested networking functions missing from the 1.x releases of .Net.

This namespace is a welcome addition to the framework, especially as the number of applications that need to be network aware are in the majority now.


Posted Tue, Jan 23 2007 7:13 PM by Jayson Knight
comments powered by Disqus

Comments

Kyle wrote re: Creating Network Aware Applications -- System.Net.NetworkInformation
on Thu, Jan 25 2007 7:52 AM

Great tip Jayson!  You inspired me to checkout the methods that are available on the client to provide similar functionality.  There is a System.Net.WebRequest object in the ASP.NET AJAX library which, when used creatively, could potentially be used to provide events for client connectivity, etc.  Interesting.

Jayson Knight wrote re: Creating Network Aware Applications -- System.Net.NetworkInformation
on Thu, Jan 25 2007 2:36 PM

Kyle, interesting that you should mention the WebRequest (and related WebResponse) classes as that's how I was checking for connectivity in earlier applications...I would just create a small utility class that would attempt a request to a website, then attempt to read a response. If it failed for any reason, the app would assume there was no network connectivity. While it did indeed work, I always felt it was clunky plus it created unnecessary dependencies.

Thankfully those days are over, however the NetworkInfo classes aren't w/o they're own quirks as well ;-).

DotNetKicks.com wrote Creating Network Aware Applications -- System.Net.NetworkInformation
on Thu, Jan 25 2007 4:32 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy