- Andrew Kos
- Bill Burlein
- Bryan Williams
- Christian Vozar
- Jeff Brown
- John Kraus
- Joseph Mak
- Josh Durbin
- Mark Daugherty
- Matt Van Bergen
- Melissa Geoffrion
- Michael Kang
- Michael Chan
- Michael Hodgdon
- Mike Motherway
- Molly McDaniel
- Nadia Maciulis
- Pat McLoughlin
- Paul Michelotti
- Puru Hemnani
- Rohit Srinath
- Ryan Lunka
- Tom Kelly
All Blogs
CITYTECH Blogroll:
Silverlight 3 – Network Availability
Thursday, February 4, 2010
Once you have an application that is running ‘Out of Browser’, network availability will most likely be your next concern. The good news is Silverlight 3 comes through and gives you a way to check if there is an actual network connection available. An interesting caveat here is that you are checking if a network connection is available, but aren’t checking to see if that network is connected to the internet. In order to check for an internet connection a WCF service can be developed that can ping a common website. Kunal Chowdhury’s Blog does a great job of explaining a possible way to do this.
Knowing whether or not an ‘Out of Browser’ Silverlight application has network access can be very powerful. Having this knowledge allows an application to decide whether or not to call out to its services tier. A common use of this would be for an application that saves data to a database through services. Instead of just putting up an error when network connectivity is lost, the application can save data to a local storage in xml or database form and wait for network connectivity to be restored. At that moment the application can then sync the data that it had stored locally through its service interface.
We are going to look at some simple code that would allow an application to be aware of network availability.
1. First we will add some xaml to a form that will act as an indicator of network connectivity. When there is a live network connection we will indicate it with a green circle and no connection with be indicated with a red circle.
1: <Grid x:Name="InternetConnectionGrid" Background="White" Width="175">
2: <Grid.ColumnDefinitions>
3: <ColumnDefinition Width="165"></ColumnDefinition>
4: <ColumnDefinition Width="10"></ColumnDefinition>
5: </Grid.ColumnDefinitions>
6:
7: <TextBlock Text="Current Internet Connection:" Grid.Column="0"></TextBlock>
8: <Ellipse x:Name="circleDisc" Grid.Column="1" Fill="Red" Width="10" Height="10"/>
9: <Ellipse x:Name="circleConn" Grid.Column="1" Fill="Green" Width="10" Height="10"/>
10: </Grid>
2. Snapshot of a live network connection.
3. Snapshot of a disconnected network connection.
4. Now on to the fun part! We need to add code to actually check the network availability. In the constructor we add two lines. The first line registers the NetworkAddressChanged event. This event fires anytime there is a change in network connectivity. If the IP Address changes because there is a move from ethernet connection to wireless connection this event will fire. If a network cord is unplugged this event will fire.
The second line in the constructor calls out to a method that we have developed that actually checks if there is a connection available. NetworkInterface.GetIsNetworkAvailable() returns true if there is a connection and false if there is not. Our method enables the red circle and if a network connection is found it toggles to the green circle.
Finally, inside the NetworkAddressChanged event we also call out to CheckNetworkConnection() to see if the change resulted in the loss or gain of network availability. This short amount of code allows our Silverlight application to be aware of a network connection while operating outside of the browser.
1: public MainPage()
2: {
3: InitializeComponent();
4:
5: NetworkChange.NetworkAddressChanged += NetworkAddressChanged;
6:
7: CheckInternetConnection();
8: }
9:
10: public void NetworkAddressChanged(object sender,EventArgs e)
11: {
12: CheckNetworkConnection();
13: }
14:
15: private void CheckInternetConnection()
16: {
17: circleDisc.Visibility = Visibility.Visible;
18: circleConn.Visibility = Visibility.Collapsed;
19:
20: if (NetworkInterface.GetIsNetworkAvailable())
21: {
22: circleDisc.Visibility = Visibility.Collapsed;
23: circleConn.Visibility = Visibility.Visible;
24: }
25: }
Possible Issues
In some cases when running on a virtual machine, the application is not realizing that the network connection has been disabled and returns some false positives. I will be exploring what is causing this issue. Also be careful to check for other network connections that may be hidden in the operating system. A network connection does not have to be connected to the internet for Silverlight to consider it a valid network connection.
Recent Posts
- Descriptive JMX Beans in AEM/CQ
- Invisible requirements within Business requirements
- Building a better Options Predicate
- Javascript, This, and You.
- Extensionless URLs with Adobe Experience Manager
- The Life of a Tester in Adobe CQ World!
- Limitations of the CQ Parsys Model and the Implementation of a Nested Paragraph System
- Google Analytics and AEM: No JavaScript? No Problem.
- Using Apache FOP to generate a PDF document based on a form submission data
- Configuring SAML in AEM 5.6