Offline Network Detection in Microsoft Silverlight 3

Post on 08-Jun-2015

462 views 1 download

Tags:

description

Come hear how to structure Silverlight applications to support a sometimes connected scenario, and learn how to avoid the common pitfalls of implementing network detection.

Transcript of Offline Network Detection in Microsoft Silverlight 3

Offline Detection in SL 3 and .NET Framework

Peter SmithProgram Manager, .NET NetworkingMicrosoft Corporation

Why Offline Detection?

Silverlight now has the ability to run from the user’s desktop, just like a normal application

Sometimes when your code runs, the user will not be online

You have to detect and handle this case

The One Take-Away Point

How do you know if you are online?

1. Can you connect to your server?2. Have you validated the response?

Microsoft can help by telling you when to try again

Demonstration

A very simple Silverlight 3 application that does network detection

demo

Why Offline is So Hard

Is the network useful? Driver issuesVirtual PCs!Coffee shop problemMaybe you shouldn’t connect!

The One Take-Away Point

How do you know if you are online?

1. Can you connect to your server?2. Have you validated the response?

Microsoft can help by telling you when to try again

VB.NET Sample

REM function that gets called when the network changesPrivate Sub OnNetworkChange (ByVal s As Object, _ ByVal a As EventArgs)

REM Perform detection hereEnd Sub

REM set up the system to call that functionPublic Sub New() AddHandler NetworkInformation. _ NetworkChange.NetworkAddressChanged, _ AddressOf OnNetworkChangeEnd Sub

C# Sample

private void OnNetworkChange (Object sender, EventArgs Args){ // Put network detection here}

public MainPage(){ NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler (OnNetworkChange);}

Patterns For Your Code

Ask the user

Before you connect, validate

Constantly validate results

Ask the User

Add two buttons to your application

One is “retry” and tries to connect again

The other is “offline mode” and prevents the application from even trying

Before You Connect, Validate

Put a standard file on your serverDownload and check contentsNo server==really offlineRevalidate on network change

Got a file from your server, but it’s not yours? You might be have the “coffee shop” problem and must retry

Constantly Check Results

Check all downloadsNot just status code!No server==really offlineRevalidate on network change

Got a file from your server, but it’s not yours? You might be have the “coffee shop” problem and must retry

The One Take-Away Point

How do you know if you are online?

1. Can you connect to your server?2. Have you validated the response?

Microsoft can help by telling you when to try again

Thank you!

Questions?

My email is pesmith@microsoft.com

NCL (System.Net) blog is http://blogs.msdn.com/ncl/

The team forum is http://social.msdn.microsoft.com/Forums/en-US/ncl/threads/

Please Complete an Evaluation FormYour feedback is important!

Evaluation forms can be found on each chairTemp Staff at the back of the room have additional evaluation form copies

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after

the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Extra Slides!

These slides go into more detail about the advantages and disadvantages of the different code patterns for network detection

Advantages of Ask the User

The user might know that they’ve done something special (but undetectable) to go on lineThe user might also know that it’s a bad time to go online

Disadvantages to Ask the User

The user might not know that they’ve gone online (and get it wrong)They also probably think it’s your job normally

Advantages of the Before Pattern

It’s simpleYou only have to understand one file (and you can just make it XML)

Disadvantages of "Before"

It’s not always possible to drop a file on your company serverIt has an extra bit of work at startupSometimes the network changes, but you don’t detect it in time (and ruin a downloaded file)

Advantages of Constantly Check

Starts up faster – no extra file to downloadRequires no changes to your server

Disadvantages to Constantly Check

Checking is hard! You have to understand all of the different file types that your application might haveChecking is “noisy”: there are lots of reasons why your server might not have given a good reply