在 Web 服务调用之间共享对象实例

发布于 2024-08-23 05:48:26 字数 211 浏览 2 评论 0原文

我有一个初始化成本相对较高的对象,它提供了处理Web服务请求所需的线程安全计算方法。

我正在寻找在请求之间保持初始化实例可用的最佳方法。

一种方法是将其声明为静态变量。然后它将保持可用,直到 AppDomain 被回收。

这是一个不使用 WCF 的较旧的 Web 服务,但如果转换可以提供更好的解决方案,那么转换也是一种选择。

有更好的方法吗?

I have an object that has relatively high initialization cost that provides a thread-safe calculation method needed to process web service requests.

I'm looking for the best way to keep an initialized instance available between requests.

One method is to declare it as a static variable. It would then remain available until the AppDomain is recycled.

This is an older web service that does not use WCF, but converting is an option if that would provide a better solution.

Is there a better approach?

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

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

发布评论

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

评论(1

治碍 2024-08-30 05:48:26

HttpRuntime.Cache 中缓存对象怎么样?

MyObject val = (MyObject)HttpRuntime.Cache["MyCacheKey"];
if (val == null)
{
    val = // create your expensive object here
    HttpRuntime.Cache.Insert("MyCacheKey", val, null, 
      DateTime.Now.AddSeconds(3600), 
      System.Web.Caching.Cache.NoSlidingExpiration);
}

在这里,我将其在缓存中保留最多一个小时,但您可以根据需要更改此设置。

What about caching the object in HttpRuntime.Cache?

MyObject val = (MyObject)HttpRuntime.Cache["MyCacheKey"];
if (val == null)
{
    val = // create your expensive object here
    HttpRuntime.Cache.Insert("MyCacheKey", val, null, 
      DateTime.Now.AddSeconds(3600), 
      System.Web.Caching.Cache.NoSlidingExpiration);
}

Here I leave it in the cache for up to an hour, but you can vary this as needed.

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