Search code examples
silverlightenterprise-librarycaching-application-block

Using Enterprise Library Caching Application Block in Silverlight


I have downloaded the patterns & practices Silverlight Integration Pack to use caching (Caching Application Block) in my Silverlight Application, but i tried and tried and didn't get it to work. I did not find any useful example - does anyone have an example? Just a few lines of code that are showing a simple usage? Do I need to use unity?

THANKS!

I used a default configuration that i got from the Enterprise Library Configuration - Tool which i exported as XAML:

<el:CachingSettings DefaultCache="In-Memory Cache" x:Key="cachingSilverlightConfiguration">
  <el:CachingSettings.Caches>
    <el:InMemoryCacheData ExpirationPollingInterval="00:02:00" Name="In-Memory Cache" />
  </el:CachingSettings.Caches>
</el:CachingSettings>

And when I try to access it with the following code:

ObjectCache cache = EnterpriseLibraryContainer.Current.GetInstance<ObjectCache>("In-Memory Cache");

then, i get an Exception:

{System.IO.FileNotFoundException: The system cannot find the file specified. File name: 'System.Xml.Linq, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' ...

Solution

  • Thanks to Randy Levy from the Entlib Support, I got the answer I needed, there:

    It looks like you haven't configured the container. If you don't want to call to the server to retrieve the configuration then you need to embed and load the configuration.

    string stringWithXAMLConfiguration = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <el:ConfigurationDictionary xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" 
                    xmlns:el=""http://schemas.microsoft.com/practices/2011/entlib"">
    <el:CachingSettings DefaultCache=""In-Memory Cache"" x:Key=""cachingSilverlightConfiguration"">
        <el:CachingSettings.Caches>
            <el:InMemoryCacheData ExpirationPollingInterval=""00:02:00"" Name=""In-Memory Cache"" />
            <el:IsolatedStorageCacheData MaxSizeInKilobytes=""5120"" PercentOfQuotaUsedBeforeScavenging=""50"" PercentOfQuotaUsedAfterScavenging=""20"" ExpirationPollingInterval=""00:01:00"" Name=""Isolated Storage Cache"" />
        </el:CachingSettings.Caches>
    </el:CachingSettings>
    </el:ConfigurationDictionary>";
    
    var configDictionary = (IDictionary)XamlReader.Load(stringWithXAMLConfiguration);
    var configSource = DictionaryConfigurationSource.FromDictionary(configDictionary);
    EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    

    Or if you don't want to have the string in the code but prefer in a XAML file then ensure the XAML file's (e.g. cacheConfig.xaml) Build Action is Embedded Resource and then you can use the following code:

    string xaml;
    using (Stream s = this.GetType().Assembly.GetManifestResourceStream("SilverlightApplicationCache.cacheConfig.xaml"))
        using (StreamReader sr = new StreamReader(s))
            xaml = sr.ReadToEnd();
    
    var configDictionary = (IDictionary)XamlReader.Load(xaml);
    var configSource = DictionaryConfigurationSource.FromDictionary(configDictionary);
    EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    

    SilverlightApplicationCache above is the namespace of the XAML file (eg. the default namespace of the project).