I’m working on a project where I have to store data in a WCF service but I’m unsure how I can maintain the changes when the data is manipulated.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode =
InstanceContextMode.Single,UseSynchronizationContext = true)]
public class TestService : ITestService {
private List<TestClass> cl;
public TestService()
{
cl = new List<TestClass>();
TestClass tc = new TestClass(5);
cl.add(tc);
}
public void Diminish(int x)
{
cl[0].Value -= x;
}
public TestClass ReturnValue(int p)
{
return cl[p];
}
}
So now when a client uses this service how can I send an updated value of the collection cl?
You could use a static field. But obviously once you do that you need to synchronize the access to it because the
List<T>class is not thread safe:or you could also use the
SynchronizedCollection<T>class which is thread safe: