在 Silverlight 中使用企业库缓存应用程序块

发布于 2024-12-10 22:40:21 字数 999 浏览 0 评论 0原文

我已经下载了图案和实践 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' ...

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' ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过度放纵 2024-12-17 22:40:21

感谢 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<上面的 /code> 是 XAML 文件的命名空间(例如项目的默认命名空间)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文