如何在 WCF 服务和托管该服务的进程之间共享缓存数据?

发布于 2024-11-05 04:04:35 字数 209 浏览 0 评论 0原文

我有一个普通的 Windows 服务,可以处理大型数据集并将其存储到数据库中。此 Windows 服务还充当托管 WCF 服务,该服务为一个或多个 GUI 提供处理后的数据。

目前,WCF 服务必须至少访问数据库一次才能为客户端获取数据,但数据集的大小使得速度非常慢,并且由于数据重复而占用大量内存。理想情况下,我想与 WCF 服务直接(在内存中)共享数据处理的结果。有办法做到这一点吗?

I have an ordinary windows service that processes a large data set and stores it to a DB. This windows service also acts to host a WCF service that serves the processed data up to one or more GUIs.

Currently the WCF service has to hit the DB at least once to get the data for the client, but the size of the data set is such that this is extremely slow, and eats up a lot of memory because of the duplication of data. Ideally I would like to share the results of the data processing directly (in memory) with the WCF service. Is there a way to do this?

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

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

发布评论

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

评论(3

格子衫的從容 2024-11-12 04:04:35

是的,使用分布式缓存引擎。

基本上,分布式缓存引擎是在一台或多台计算机上运行的独立进程,用于管理缓存。此缓存站点位于其自己的进程中,通常缓存引擎提供用于访问该数据的 API

主要选项是

Yes, using a distributed cache engine.

Basically, the distributed cache engine is a separate process running on one or more machines, that manages a cache. This cache sites in it's own process, and typically the cache engine provides an API for accessing that data

The main options are

念三年u 2024-11-12 04:04:35

实际上,我的一位同事发现可以通过静态方法从主机访问 WCF 服务,甚至不需要将该服务设置为单例模式。

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyWcfService : IMyWcfService
{
    private static string messageFromHost;

    public static void PassMessageFromHostToService(string message)
    {
        messageFromHost = message;
    }
// Other methods fulfilling the service contract here...
}

然后,您可以从主机进程执行此操作来调用该方法:

MyWcfService.PassMessageFromHostToService("I'm a message from your host");

我不确定这是否被认为是不好的做法,或者它是否会导致我们没有考虑到的任何问题,但它似乎对我有用:)

Actually, a colleague of mine found it is possible to access the WCF Service from the host via static methods, and you don't even need to have the service in singleton mode.

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyWcfService : IMyWcfService
{
    private static string messageFromHost;

    public static void PassMessageFromHostToService(string message)
    {
        messageFromHost = message;
    }
// Other methods fulfilling the service contract here...
}

From the host process you can then do this to call the method:

MyWcfService.PassMessageFromHostToService("I'm a message from your host");

I'm not sure if this is considered bad practice, or if it will cause any problems we haven't considered, but it seems to be working for me :)

别低头,皇冠会掉 2024-11-12 04:04:35

Yes. You'll need to create your WCF host in singleton mode. See this related question.

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