The authentication PIN generation web service must send a notification or push to the requester for each generated PIN.
However, when a multithreaded call to this web service occurs, the service fails to manage the threads and sends multiple notifications with the same PIN code. This is incorrect. The system generates distinct PIN codes, but it falters in sending the notifications.
In the case of a single-threaded call, this error does not happen, even with thousands of queued calls, it works fine.
Interface:
[ServiceContract]
public interface IGenPin
{
[OperationContract]
ResponseResult GenPin(ClientPin client);
}
Class:
public class GenPinWS : IGenPin
{
public ResponseResult GenPin(ClientPin client)
{
ResponseResult msgResponseResult = null;
string strGuidTrace = Guid.NewGuid().ToString().Replace("-", "");
try
{
Traceability.InsertTraceability(...);
ServiceGenPin _serviceGenPin = new ServiceGenPin();
msgResponseResult = _serviceGenPin.GenPin(client, strGuidTrace); //here I send the PIN notifications
Traceability.InsertTraceability(...);
return msgGpinResponseResult;
}
catch (Exception ex)
{
...
}
return msgResponseResult;
}
}
I've already tried implementing semaphore and queue, but it didn't work. The code is synchronous and tightly coupled.
I expect to send the notification only once for each generated PIN code. How can I control these multithreaded calls from within my single-threaded web service?
One simple solution would be to create a critical section like that: