Search code examples
c#wcf

Using a WCF service to share information across clients


I'm trying to develop a system to share information across 2 windows applications with different update loops.

I developed a solution that uses a WCF service to store and retrieve data. However this data is different across clients and therefore showing different values for each applications.

The service I tried to implement are similar to this

namespace TEST_Service_ServiceLibrary
{
    [ServiceContract]
    public interface TEST_ServiceInterface
    {
        [OperationContract]
        string GetData();

        [OperationContract]
        void StoreData(string data);
   }
}

namespace TEST_Service_ServiceLibrary
{
    // Core service of the application, stores and provides data: 
    public class TEST_Service : TEST_ServiceInterface
    {
        string TEST_string;

        // Used to pull stored data
        public string GetData()
        {
              return TEST_string;
        }

        // Used to store data
        public void StoreData(string data)
        {
            TEST_string = data;
        }
    }
}

Each of the applications creates a TEST_Service client.

I tested the GetData and StoreData functions and they work fine independently, however when I use StoreData on one application and test the GetData method from the other the data appears to be empty.

I have looked around but haven't found a solution to this problem, is there a work around for this? or should I change my approach? I thought of using a local data base but I'm not sure this is the best way to solve it

Thanks a lot


Solution

  • You have more than one instance of your service class. If you want to have your data in memory, you will need to run it in single instance mode:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

    Now keeping your data in memory might not be the best option anyway. You should look for a data store of some kind and then make that store a persistent instance with a single interface. Then it does not matter how many of your service instances are used.