How do I share cached data between a WCF service a

2019-09-15 11:00发布

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?

标签: wcf caching host
3条回答
Animai°情兽
2楼-- · 2019-09-15 11:17

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

查看更多
Viruses.
3楼-- · 2019-09-15 11:26

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

查看更多
Lonely孤独者°
4楼-- · 2019-09-15 11:26

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

查看更多
登录 后发表回答