We are trying to develop a WCF service for executing a long running task. The method implementing the long running task spawns a new task and immediately returns to the client(which in our case is an .aspx page) if the task has been successfully queued. The service is running on its own application pool with no recycling and single InstanceContextMode.
WCF Service
[OperationContract]
public bool testThreadAbortException()
{
Task.Factory.StartNew
(
() =>
{
try
{
//long operation
int i = 0;
while (i < 250)
{
int j = 0;
while (j < 2000000) j++;
i++;
}
ThreadState state = Thread.CurrentThread.ThreadState;
string dummy = "finished ";
}
catch(Exception exception)
{
ThreadState state = Thread.CurrentThread.ThreadState;
string msg = exception.Message;
Exception inner = exception.InnerException;
}
}
);
return true;
}
Client
protected void btnRun_Click(object sender, EventArgs e)
{
_default.IISHOST_ETLSchedulerServiceReference.ETLSchedulerServiceClient client = new _default.IISHOST_ETLSchedulerServiceReference.ETLSchedulerServiceClient();
bool ret = client.testThreadAbortException();
}
Now the problem is that while the testThreadAbortException method is being executed i catch the Thread was being aborted exception ( this always happends after the client has exited the event handler method ). The weird thing is that this exception is only thrown the first time (ie if i press the run button again the code executes fine). I have to restart my local IIS to replicate the error again.
- Does anyone have a clue know why this happens??
- Is there a better way to implement what i am trying to archive besides switching to a windows service??