C# – How to stop the WCF service (database queriying) running behind

cwcf

I have a WCF service which will get an object with huge data from database in the form of collection.
I have a UI which has get and cancel buttons .

Get button : make service request and continue with data population with the returned data from service.I start it as below

Thread _workerThread = new Thread(DoExportTreeLoad);
_workerThread.Start();

Cancel : stops the above thread.It does as below

_workerThread.Abort()

My DoExportTreeLoad() has the synchronous service call which in turn hits database and gets data.

result = serivce.GetData(criteria);

Problem: When I cancel the operation after requesting the service, the DAL code is still running in the background (getting data from database) where it should not happen.

As soon as i cancel the operation from UI ,the transaction (to get the data ) from database need to be aborted.
It leads to big performance problem.

Please help me with any way to end up the backend task when thread is cancelled.
Thanks in advance.

Best Answer

There is no way to tell WCF to kill a request that is underway. You would have to roll your own solution. For example, have another method on the WCF object that you can call telling it to cancel any operation currently underway.

You may want to re-architect your solution to support this. For example, when you start the big-data-retrieval function you could return immediately with a request-id. Then have periodic events that are sent back to the client from the WCF component with data collected so far and a "cancel" argument you can set to cancel the operation. Or have a method that the client can call to get data collected so far, passing in the request-id and provide a cancel method accepting the request-id.

Here is a similar question on SO: https://stackoverflow.com/questions/15324995/cancel-a-long-running-task-over-wcf-from-client