in DeviceBridge/Services/ConnectionManager.cs [778:811]
public async Task RemoveMessageCallbackAsync(string deviceId)
{
_logger.Info("Attempting remove C2DMessage handler for device {deviceId}", deviceId);
// We need to synchronize this with client creation/close so a race condition doesn't cause us to add the
// callback to a client that is being currently created but not yet in the clients list.
var mutex = _clientSemaphores.GetOrAdd(deviceId, new SemaphoreSlim(1, 1));
await mutex.WaitAsync();
try
{
_logger.Info("Acquired connection lock for device {deviceId}", deviceId);
// Remove the callback so it's not registered in new clients
if (_messageCallbacks.TryRemove(deviceId, out _))
{
if (!_clients.TryGetValue(deviceId, out DeviceClient client))
{
_logger.Info("Connection for device {deviceId} not found while trying to remove C2DMessage callback", deviceId);
return;
}
await client.SetReceiveMessageHandlerAsync(null, null);
}
else
{
_logger.Info("Tried to remove C2DMessage handler for device {deviceId}, but a handler was not registered", deviceId);
}
}
finally
{
mutex.Release();
}
}