简体   繁体   中英

Caching objects in a RESTful WCF service

I'm looking for a way to cache objects in memory with a RESTful WCF service. The service is completely stateless and is hosted outside of an IIS. I want to implement the caching by myself, so memcached isn't an option.

Right now I'm thinking of hosting a separate stateful System.ServiceModel.ServiceHost that does all the caching. It'll communicate with the rest of the WCF methods through a separate port or by some other means. However I'm not sure if this is the ideal solution to my problem. Has anyone got any tips?

I understand your confusion between stateless service and a stateful host and how the two can interact.

In this code sample I demonstrate conceptually how an in-memory singleton (Caching mechanism, I refer to as CachingProvider henceforth) can be referenced by both the service class (the service instance more precisely during the lifecycle of the request) and the service host (in this case I chose it to be a Console Application )

I assume here, the service interface and class are both located within the console applicaiton project that hosts the service.

In this simple example, my primitive CachingProvider class basically acts as a counter of how many service calls are made to the GetData method, and the service host will poll the CachingProvider every 5 seconds to get the count of service calls made so far.

note: you can use the WCFTestClient utility to test this quickly.

Disclaimer: I by no means suggest that a complex Caching mechanism be implemented as simply as in this sample, this code is merely for demosntration purposes.

namespace ServiceHostConsole
{
    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        string GetData(int value);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class TestService : ITestService
    {
        public TestService()
        {
            CachingProvider.CallCount++;
        }

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }

    //For demonstration purposes only
    static class CachingProvider
    {
        static CachingProvider()
        {
            //private static constructor can initialize 
            //static cacheable resources
            _callCounter = 0; //Trivial example of initialization
        }

        private static int _callCounter;
        public static int CallCount
        {
            set { _callCounter = value; }
            get { return _callCounter; }
        }
    }

    class Program
    {
        static void Main()
        {
            using (var host = new ServiceHost(typeof(TestService), new Uri("http://localhost/TestService")))
            {
                host.Open();

                //Example how the ServiceHost can report on a persistent in-memory object that is being
                //updated each time the service is called.
                new Timer(state => Console.WriteLine("# of service calls: {0}", CachingProvider.CallCount), null, 0, 5000);

                Console.Read();
                host.Close();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM