Monday, October 24, 2005 12:36 AM
srinathv
Seattle Code Camp
Today at Seattle Code Camp day 2 -- Peter & Brad gave the attendees a taste of Dependency Injection via ObjectBuilder (which is part our CAB deliverable), TDD & Pair Programming, Model-View-Presenter, Monad and CAB. The slides for the presentation will be posted at Peter's site. Both Peter & Brad fielded a lot of questions on all of the above topics.
One of the attendees asked about our the Offline App Block & how would he replace the out of the box ConnectionDetectionStrategy (because the default implementation relies on wininet api's used by Internet Explorer). I couldn't find the original article & code that came with below implementation (I think this is from David), it illustrates how to ping a web service to check for connectivity.
using System;
using System.Runtime.InteropServices;
using System.Xml.XPath;
using System.IO;
using System.Configuration;
using Microsoft.ApplicationBlocks.Common;
using Microsoft.ApplicationBlocks.SmartClient.Offline;
using System.Web;
using System.Net;
namespace Microsoft.Samples.OfflineSampleClient
{
/// <summary>
/// This is a simple implementation of a ConnectionDetectionStrategy.
/// It periodically pings the web server to see if is available.
/// </summary>
public class PingWebServerStrategy : IConnectionDetectionStrategy, IProvider
{
private int pollInterval;
public const int MinimumPollingIntervalInSeconds = 1;
public PingWebServerStrategy()
{
}
/// <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()
{
bool connected = true;
try
{
HttpWebRequest _webRequest2 = (HttpWebRequest)WebRequest.Create( "http://localhost/OfflineSampleWebService/OfflineSampleWebService.asmx" );
_webRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;
using ( HttpWebResponse myHttpWebResponse=(HttpWebResponse)_webRequest2.GetResponse() )
{
if ( !_webRequest2.HaveResponse )
{
connected = false;
}
}
}
catch( Exception ex )
{
connected = false;
}
return connected;
}
/// <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 );
}
}
}
}