WCF web service singleton - odd behaviour
I've created a singleton WCF web service that runs a background thread for entire time while it's hosted. First method starts a function in a background thread that checks shared data and the other one updates that data. It was working fine and suddenly started acting strange. Meanwhile there was no major changes in code. WCF web service is hosted in Visual Studio Development Server, VS2008, 3.5 framework, Win XP SP3, and the same happens if it's hosted in Vista on IIS 7.
Here is the simplified code
Service:
[ServiceContract]
public interface IService1
{
[OperationContract]
void Configure(XElement configuration);
[OperationContract]
void UpdateData(string data);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public partial class Service1 : IService1
{
List<string> stringCollection = new List<string>();
bool running;
Thread workerThread;
public void Configure(XElement configuration)
{
//add string elements to the stringCollection based on configuration
ParseConfiguration(); //implementation is irrelevant
//start background thread
workerThread = new Thread(WorkerFunction);
running = true;
workerThread.Start();
}
public void UpdateData(string data)
{
//adds string data to stringCollection
stringCollection.Add(data);
}
}
public partial class Service1 : IService1
{
private void WorkerFunction()
{
while(running)
{
//check stringCollection
Thread.Sleep(500);
}
}
}
Client:
//Configure() is called first and only once from client
Xelement configuration = Xelement.Load("configuration.xml");
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.Configure(configuration);
client.Close();
//UpdateData is called repeatedly from client
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.UpdateData("some string");
client.Close();
While debugging I noticed several things. After Configure() starts WorkerFunction() in new thread, the thread is alive for a second or so, while WorkerFunction() has access to stringCollection that was configured in Configure(). When client calls UpdateData() method for the first time the method has empty stringCollection (not containing data added from Configure() method) as if its not shared while stringCollection preserve its data between each UpdateData() call. For example:
//stringCollection after Configure()
{"aaa","bbb","ccc"}
//stringCollection after UpdateData("xxx")
{"xxx"}
//stringCollection after UpdateData("yyy")
{"xxx", "yyy"}
//after I run Client application again and call Configure()
//the data is preserved only here
{"xxx", "yyy","aaa", "bbb", "ccc"}
If I susspend Thread with highest priority in Threads window while 开发者_高级运维debugging, than the background thread stays alive like it supposed to do but I get the same result as above. WorkerFunction() has its own instance of stringCollection and UpdateData() has another one. I don't know what that thread with highest priority has to do with my background thread but it appears it has bad affect on it. Service is supposed to be singleton but it doesn't act like one.
Cheers
Good to see that your problem is fixed.
However, I think that you should look a bit at your code, it seems more complicated than it needs to be.
You are using a Singleton, but each call to the singleton is creating a new thread.
Why not do it without a singlton and let IIS handle the threading?
精彩评论