I have a WCF service installed for PerCall
... the problem is that every time a call comes to the server, it is processed as a new request.
Yes, thatβs exactly what you are saying. If you can, just change to InstanceContextMode.PerSession ; then you can do what you are trying to do (assuming that you own your own hosting).
If you cannot do this, you will have to develop a more complex solution, for example, @PeterRitchie. Firstly, your host: IIS is not designed for long-term operations that are independent of requests, so I assume that you own your own hosting. Next, you will need a token form (for example, a GUID) that will act as an identifier for a long-term operation. Your Start method will allocate the GUID and CancellationTokenSource and start the operation, and your Stop method will accept the GUID and use it to look up the CancellationTokenSource and cancel the operation. You will need a general (static, thread safe) dictionary to perform the search.
If your host is IIS, your decision becomes more complex ... :)
First you need a backend that is not hosted on IIS. A common choice is the Azure production agent role or the Win32 service. Then you need a reliable communication mechanism: Azure Queue, MSMQ, WebSphere, etc. You can then create your WCF-over-IIS service so that the Start method generates a GUID and deletes the message in the queue to begin processing. The Stop method accepts a GUID and discards the message in the queue to cancel processing. All other logic moves to the backend service.
source share