none
InstanceContextMode.Single in WCF wsHttpBinding,webHttpBinding and REST

    Question

  • I have recently started development on a relatively simple WCF REST service which returns JSON formatted results. At first everything worked great, and the service was quickly up and running.

    The main function of the service is to return a large chunk of data extracted from a database. This data rarely changes, so I decided to try and setup a caching mechanism to speed things up. To do this I planned to set InstanceContextMode.Single and ConcurrencyMode.Multiple, and then with some thread locks, safely return a static cached result. Every 5 minutes or so, or whenever IIS decides to clear everything, the data would be re-fetched from the database.

    My issue is InstanceContextMode.Single does not behave as expected. My understanding is a single instance of my WCF service class should be created and maintained. However the behaviour I have is a completely new instance of my Class is created per call. This include re-initialising all static variables.

    I tried changing the web service from webHttpBinding (used for REST) to wsHttpBinding and using the service as a SOAP config, but this results in exactly the same behaviour.

    What am I doing wrong!!! Have spent way too long trying to figure this out.

    Any help would be great!.

    Friday, October 12, 2012 7:34 AM

Answers

  • Hi,

    >My understanding is a single instance of my WCF service class should be created and maintained.

    The service-side will only create one Service Instance to deal with the client calls and they will have the same Context.

    >However the behaviour I have is a completely new instance of my Class is created per call.

    If you are using ConcurrencyMode.Multiple, it will provide multiple threads for dealing with the client calls.

    I suggest you to create another method for getting the information.

    Deal with the data logical in that method also.

     

     

    Monday, October 15, 2012 2:26 AM