TIP - Silverlight - InitParams and ApplicationLifeTimeService(s)

I have this love-hate relationship with InitParams

Like

  • Pre-download values to Silverlight, so it’s available to the client before Silverlight even starts rendering
  • Don’t have to worry about whether Silverlight can talk to any data source - if you can’t see the webpage then the problem is elsewhere

Dislike

  • Well it’s on the web page…  anyone could see it, and probably tweak it via DOM manipulation
  • If you bind to this data, you can’t really “update” it if the data changes on the server.  If say the user settings has changed, you’ll need a F5 refresh to force the Silverlight client to reload.  In a sense this is often treated like read-only data.

One thing we can fix

  • A really large App.Current with lots of different values sucked out from the initParams during App_Start

Using ApplicationLifeTimeServices

1. Write a Lifetime Service

public class CompanyApplicationLifeTimeService : IApplicationService
{
    static CompanyApplicationLifeTimeService \_current = null;

    public static CompanyApplicationLifeTimeService Current
    {
        get
        {
            return \_current;
        }
    }

    #region IApplicationService Members

    public void StartService(ApplicationServiceContext context)
    {            
        \_current = this;
        var parseThisStuff = context.ApplicationInitParams;
    }

    public void StopService()
    {
        \_current = null;
    }

    #endregion
}

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

2. Add this to your App.Xaml

<Application.ApplicationLifetimeObjects> <common:CompanyApplicationLifeTimeService x:Name=“CompanyLifeTimeService” /> </Application.ApplicationLifetimeObjects> <Application.Resources>

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

3. Now anywhere in your code you have access to the service Singleton

CompanyApplicationlifeTimeService.Current.GiveMeStuff

Discussions