I have a Windows service that acts as a cache server and is implemented using WCF. This service mainly returns a stream of data dictionaries to it's clients. Recently there was a request for me to make this service more "debuggable". One of the things that was in the TODO list was: be able to query the cache service directly for specific data.

After a lot of thought I decided to expose a REST endpoint to do this job. And belive it or not, it was super easy to do that. And so, I dedicate this blog post about the task.

My existing service was a singleton and implemented an interface such as ICacheService interface. I simply added a new interface called ICacheLookup interface with a method like GetCustomer(int customerId).

   1: [ServiceContract(Namespace = "http://MyCompany.CacheServiceLookup")]
   2: public interface ICacheLookup
   3: {
   4:     [OperationContract()]
   5:     [WebGet(UriTemplate="GetCustomer?customerId={customerId}", ResponseFormat=WebMessageFormat.Xml)]
   6:     Customer GetCustomer(int customerId);
   7: }

Next for implementation of this method, I used my singleton CacheService implementation and extracted the customer Id from it.

   1: public class CacheLookup : ICacheLookup
   2: {
   3:     public Customer GetCustomer(int customerId)
   4:     {
   5:         Customer customer = null;
   6:         CacheService.Instance.CustomersCache.FullEntities.TryGetValue(customerId, out customer);
   7:         return customer;
   8:     }
   9: }

After this, all that was left was setting up the configuration endpoint and the rest service startup.

   1: <services>
   2:   <service behaviorConfiguration="ServiceBehavior" name="MyCompany.CacheService">
   3:     <endpoint address="net.tcp://localhost:2000/CacheService"
   4:       binding="netTcpBinding" bindingConfiguration="TcpBinding" name="TCP"
   5:       contract="MyCompany.ICacheService" />
   6:   </service>
   7:   <service behaviorConfiguration="ServiceBehavior" name="MyCompany.CacheLookup">
   8:     <endpoint address="http://localhost:2001/CacheService"
   9:       binding="webHttpBinding" bindingConfiguration="WebBinding" name="Rest"
  10:       contract="MyCompany.ICacheLookup" />
  11:   </service>
  12: </services>

Starting the REST service in the ServiceHost:

   1: private static ServiceHost service = null;
   2: private static System.ServiceModel.Web.WebServiceHost webHost = null; 
   3:  
   4: public static void StartService()
   5: {
   6:     Uri baseAddress = new Uri(ConfigurationManager.AppSettings["BaseAddress"]);
   7:     CacheService serviceInstance = CacheService.Instance;
   8:     CacheServiceHost.service = new ServiceHost(serviceInstance, baseAddress);
   9:     CacheServiceHost.service.Open();
  10:  
  11:     CacheServiceHost.webHost = new System.ServiceModel.Web.WebServiceHost(typeof(CacheLookup), baseAddress);
  12:     CacheServiceHost.webHost.Open();
  13: }

No changes were needed for deployment, the REST endpoint simply worked and was accessible using the following url:

http://localhost:2001/CacheService/GetCustomer?customerId=2000

That's all, and it was super easy to get xml serialized data out of the service without much ado. Since then I've become a great fan of REST and declarative programming model : )

--Ads by Microsoft--