views:

1695

answers:

1

I've created a Windows Communication Foundation service (the appDomain in this case is a Windows Forms application) that initializes its serviceType class as a singleton:

Starting the service works. Making a call from a client works. But if the service makes a call to itself with the above code ("//Make the 1st call to the service?"), i get a System.TimeoutException.

private static myDataService.DataProvider.CustomSingletonClass obs;

public DataProviderServiceType()
{

    //Create the object if needed.  This should only be required first time.
    if (obs == null)
    {
        obs = new myDataService.DataProvider.CustomSingletonClass();
        //Instruct the class to read its configuration and initialize.                       

        obs.initializeSingletonClass(null);

    }

}

The singleton class has a timer object that will start upon its initialization. Thus once a client makes a call is made to the service, the class is instantiated, the timer is started and the object will continue its existence with it's timer object periodically firing.

For context, the timer's event will update the singleton's properties. The aim is for the singleton class to periodically perform calculations, hold the data in its properties and return whatever's in the properties to calling client. This way, calls to perform periodic (time-based) calculations are limited and all the clients receive the same updated data.

The problem is that once the service is hosted, it requires just one client to make a call for the singleton instance to be created and the timer started.

SetListText("Starting Service...");
host_DataService = new ServiceHost(serviceType_Data);
host_DataService.Open();
SetListText("Service is now available.");

I would like the service to initialize the singleton class with one call so that the 1st client to make a request doesn't have to wait for the configuration and initialization to take place; the data to be refreshed upon the program's startup. Right after "SetListText("Service is now available.");" i have the following:

//Make the 1st call to the service?
EndpointAddress endpointAddress = new     EndpointAddress("http://localhost:8000/myDataService/DataProvider/TimedCalculator");
BasicHttpBinding serviceBinding = new BasicHttpBinding();

serviceBinding.CloseTimeout = new TimeSpan(0, 1, 0);
serviceBinding.OpenTimeout = new TimeSpan(0, 1, 0);
serviceBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
serviceBinding.SendTimeout = new TimeSpan(0, 1, 0);

DataProviderClient client = new DataProviderClient (serviceBinding, endpointAddress);
String[][] ArrStr = client.retrieveList();

Is there a way to have a WCF service make a call to itself in the same appDomain or is this reasonably undesirable? Are there reasons to abandon this self-calling code and just live with the 1st client's service call?

+1  A: 

I would do this a little different. Try this:

  • Drop using a singleton WCF service
  • Use Enterprise Library for caching
  • In your WCF service check if the data in the cache is new enough, if so return it, if not get / recalculate the data.
  • Create a windows service that polls the WCF service at spesific time intervals.

If the time intervals are short enough it should be usual that the windows service is doing the job of regenerating.

There are also variations of this pattern:

  • The windows service call a re-cache data service
  • Instead of a windows service you use a caching proxy product
Shiraz Bhaiji