.NET & Funky Fresh

Syndication

News

  • <script type="text/javascript" src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&amp;MarketPlace=US&amp;ID=V20070822/US/bluspiconinc-20/8001/8b68bf4b-6724-40e7-99a5-a6decf6d8648"> </script>
Handling Network Outage in Silverlight with Caliburn

If you are building an OOB application with Silverlight that relies on web services, you may have some interesting challenges on you hands.  You probably want to have some fallback for certain operations when the network is down.  You can do some fun stuff with caches and message queuing, but there still may be certain operations that you just don’t want to be available when the network is down.  Here’s a solution you can build using Caliburn’s filters that will automatically disable UI attached to decorated actions when the network becomes unavailable:

public interface INetwork : INotifyPropertyChanged
{
    bool IsAvailable { get; }
}

public class Network : PropertyChangedBase, INetwork
{
    private bool _isAvailable;

    public Network()
    {
        IsAvailable = NetworkInterface.GetIsNetworkAvailable();
        NetworkChange.NetworkAddressChanged += NetworkAddressChanged;
    }

    public bool IsAvailable
    {
        get { return _isAvailable; }
        private set
        {
            _isAvailable = value;
            NotifyOfPropertyChange("IsAvailable");
        }
    }

    private void NetworkAddressChanged(object sender, EventArgs e)
    {
        IsAvailable = NetworkInterface.GetIsNetworkAvailable();
    }
}

public class NotAvailableOfflineAttribute : Attribute, IPreProcessor, IHandlerAware
{
    private static readonly INetwork _network = ServiceLocator.Current.GetInstance<INetwork>();
    private IMessageTrigger _trigger;

    public int Priority
    {
        get { return 100; }
    }

    public bool AffectsTriggers
    {
        get { return true; }
    }

    public bool Execute(IRoutedMessage message, IInteractionNode handlingNode, object[] parameters)
    {
        return _network.IsAvailable;
    }

    public void MakeAwareOf(IRoutedMessageHandler messageHandler)
    {
        _network.PropertyChanged += (s, e) =>{
            if(e.PropertyName == "IsAvailable" && _trigger != null)
                messageHandler.UpdateAvailability(_trigger);
        };
    }

    public void MakeAwareOf(IRoutedMessageHandler messageHandler, IMessageTrigger trigger)
    {
        _trigger = trigger;
    }
}

And you use it like this:

public class MyViewModel
{
    [NotAvailableOffline]
    public void AccessNetwork()
    {
        //Do something networky here...
    }
}

Posted 11-24-2009 11:11 PM by Rob Eisenberg
Filed under: , , , ,

[Advertisement]

Comments

uberVU - social comments wrote Social comments and analytics for this post
on 11-26-2009 5:07 AM

This post was mentioned on Twitter by EisenbergEffect: Blogged Handling Network Outage in #Silverlight with #Caliburn http://bit.ly/6GhrRP

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)