我已经下载了图案&实践Silverlight集成包在我的Silverlight应用程序中使用缓存(缓存应用程序块),但我试了又试,还是没有成功。我没有找到任何有用的例子,有人能举个例子吗?只是几行显示简单用法的代码?我需要使用unity吗?
谢谢!
我使用了从企业库配置工具中获得的默认配置,并将其导出为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>
当我尝试使用以下代码访问它时:
ObjectCache cache = EnterpriseLibraryContainer.Current.GetInstance<ObjectCache>("In-Memory Cache");
然后,我得到一个异常:
{System.IO.FileNotFoundException: The system cannot find the file specified. File name: 'System.Xml.Linq, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' ...
感谢Entlib Support的Randy Levy,我在那里得到了我需要的答案:
看起来您还没有配置容器。如果你不想调用服务器来检索配置,那么你需要嵌入和加载配置。
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);
或者如果你不想在代码中有字符串,但更喜欢在XAML文件中,然后确保XAML文件(例如cacheconc . XAML)的构建动作是嵌入式资源,然后你可以使用以下代码:
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
是XAML文件的名称空间(例如:项目的默认名称空间)。