MSDN Magazine - October 2008 - (Page 40) Internet connectivity. To make sure you also have Internet access, the only reliable method is to attempt to ping a host (see Figure 6). The only problem in Figure 6 is that the code isn’t supported in Silverlight 2. (And likewise, it isn’t supported by the NetworkInterface object.) You need to isolate this code (and any other code you spot with potential compatibility problems) in a replaceable class. (For more on this restriction, see the sidebar “CoreCLR in Silverlight” available at msdn.microsoft.com/magazine/cc895632.) In the companion Figure 6 Ping for Internet Connection private bool IsConnectedToInternet() { string host = " "; bool result = false; Ping p = new Ping(); try { PingReply reply = p.Send(host, 3000); if (reply.Status == IPStatus.Success) return true; } catch { } } return result; source code, I create a utility interface for these kinds of potentially problematic methods and then create strategy classes that implement the interface for each platform, as shown in Figure 7. The interface here decouples the platform-specific strategy class from the hosting application. By then hiding the code that instantiates the concrete strategy class behind a factory method, the following code can be used safely in both WPF and Silverlight: private bool IsConnectedToInternet() { ICompatLib layer = ServiceResolver.ResolveCompatLayer(); return layer.IsConnectedToInternet(); } Figure 7 Component for Checking Internet Connection public partial class SilverCompatLayer : ICompatLib { public bool IsConnectedToInternet() { string host = " "; bool result = false; Ping p = new Ping(); try { PingReply reply = p.Send(host, 3000); if (reply.Status == IPStatus.Success) return true; } catch { } } return result; The SilverCompatLayer class lives in a separate assembly. This assembly is all that you have to change when porting your WPF code to Silverlight, or vice versa. After creating the necessary platform-specific strategy classes, all that remains is to create a Silverlight version of the application. The Silverlight project that is derived from the original WPF application contains exact copies of all files except for the compatibility assembly. You’ll notice in the code download associated with this article that I determined which strategy implementation to use by instantiating the concrete class explicitly in a factory method. You can instantiate classes directly like this or read their names from a configuration file and use reflection to get instances. These are implementation details that mostly depend on the type of system you’re building. As far as the WPF-to-Silverlight compatibility is concerned, strategies and layering are the key concepts to understand. Final Considerations public string GetRawQuoteInfo(string symbol) { string output = String.Empty; StreamReader reader = null; // Set the URL to invoke string url = String.Format(UrlBase, symbol); // Connect and get response WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); // Read the response using (reader = new StreamReader(response.GetResponseStream())) { output = reader.ReadToEnd(); reader.Close(); } } return output; In the sample application, I make a network call. In the original WPF application, the call is synchronous. In Silverlight 2, however, there’s no support at all for synchronous network calls. The only way you can make synchronous calls is by using the aforementioned trick based on calling the browser’s implementation of XMLHttpRequest. (See the source code for details.) In the sample code, I’ve successfully ported my original WPF application to the Web. When porting your code, you might also want to consider the native features of the Silverlight environment and modify the structure of your application when fitting. Note that in my simple example, rewriting the application using the Silverlight programming model would have taken less code and less effort than adapting it from a WPF application. So WPF and Silverlight have a lot in common when it comes to purely visual things such as XAML. The underlying programming model, however, is a bit different, and solutions that work in WPF cannot always be applied as is to Silverlight and vice versa. This said, sharing XAML and code between WPF and Silverlight applications can definitely be done. } // A few other methods that require a different implementation // (See source code) DINO ESPOSITO is currently an architect at IDesign and the author of Programming ASP.NET 3.5 Core References. Based in Italy, Dino is a frequent speaker at industry events worldwide. You can join his blog at weblogs.asp.net/despos. 40 msdn magazine Cutting Edge http://msdn.microsoft.com/magazine/cc895632 http://weblogs.asp.net/despos
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.