How can I share cached data between a WCF service and the process serving that service?

I have a regular Windows service that processes a large dataset and stores it in a DB. This Windows service also acts to host a WCF service that serves up the processed data to one or more GUIs.

Currently, a WCF service must hit the DB at least once to get the data for the client, but the dataset size is such that it is extremely slow and consumes a lot of memory due to duplicate data. Ideally, I would like to share the results of processing the data directly (in memory) using a WCF service. Is there a way to do this?

0


source to share


3 answers


Yes, using the distributed cache mechanism.

Basically, a distributed cache engine is a separate process running on one or more machines that manage the cache. These sites cache in it's own process, and usually the caching mechanism provides an API to access this data.



main parameters

+2


source


In fact, a colleague of mine discovered that it is possible to access a WCF service from the host using static methods, and you don't even need to have the service in single user 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...
}

      

In the host process, you can 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 that we haven't covered, but it seems to work for me :)

+1


source


Yes. You need to create WCF host in singleton mode . See this question .

0


source







All Articles