1

我已经下载了模式和实践Silverlight 集成包以在我的 Silverlight 应用程序中使用缓存(缓存应用程序块),但我尝试了又尝试并没有让它工作。我没有找到任何有用的例子——有人有例子吗?只有几行代码显示了一个简单的用法?我需要使用统一吗?

谢谢!

我使用了从企业库配置 - 我导出为 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' ...
4

2 回答 2

1

感谢Entlib 支持的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 文件(例如 cacheConfig.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 文件的命名空间(例如项目的默认命名空间)。

于 2011-10-31T09:47:23.537 回答
0

这是一个示例:http ://entlib.codeplex.com/releases/view/64923

另请参阅 Channel9 上的此视频:http: //channel9.msdn.com/posts/Enterprise-Library-for-Silverlight-Data-Caching-demo

于 2011-10-20T17:51:54.910 回答