'The smart card has been reset, so any shared state information is invalid

I am using PCSC Nuget library for accessing a smart card applets. I created a load test with JMeter, where there are 20 threads hitting web api endpoint which calls a smart card for some data. After a while and successful responses, I get this error: PCSC.Exceptions.PCSCException: 'The smart card has been reset, so any shared state information is invalid.' Exception is thrown on reader.Transmit call when I send APDU to select appropriate smart card applet. I all my interface method implementations I am using a lock object and the SmartCard class object is created as a singleton.

public class SmartCard
{
    private readonly object lockObj = new object();
    public bool SmartCardMethod1(string data)
    {
        lock (lockObj)
        {
            using (var reader = GetReader())
            {
                var responseSCard = reader.Transmit(CreateAPDUCommand1(reader, data));
            }
        }
    }
    public bool SmartCardMethod2(string data)
    {
        lock (lockObj)
        {
            using (var reader = GetReader())
            {
                var responseSCard = reader.Transmit(CreateAPDUCommand2(reader, data));
            }
        }
    }
    private IsoReader GetReader()
    {
        if (!string.IsNullOrEmpty(_readerName))
        {
            SCardContext hContext = new SCardContext();
            hContext.Establish(SCardScope.System);
            var reader = new IsoReader(hContext);

                reader.Connect(_readerName,
                                SCardShareMode.Shared,
                                SCardProtocol.T0 | SCardProtocol.T1);
            var response = reader.Transmit(CreateSelectAppletAPDUCommand(reader));
            if (response.SW1 == 0x90)
            {
                return reader;
            }
        }
    }
}

I am getting this error on this line:

var response = reader.Transmit(CreateSelectAppletAPDUCommand(reader));


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source