I guess your issue is that you didn't see lock
keyword inside events like OnAppServiceRequestReceived
, OnAppServicesCanceled
and so on, which is to do thread synchronization, and you're not sure if you should do this by yourself.
I think the answer should be no.lock
inside OnBackgroundActivated
is ensured to set correct desktopBridgeConnectionIndex
or connectionIndex
. Without the keyword lock
inside these event handles not means that the event handle must be triggered only one time at a time. For one app service, if client A is connecting the app service, at the same time, another client B asks for the same app service, for this scenario the app service will spin up another instance of the same background task. So that for client A, its app service connection there is no side effect on client App B. In another words, each app service connection has its own instance, messages sending based on one app service connection have no influence with others. You may reference this video to look more details about app service, app service relative is about starting from 25th minute.
If you check the code snippet inside the event, you may see there are code lines to judge the request is from which app service connection, for example this.desktopBridgeConnection = desktopBridgeConnections[this.currentConnectionIndex]
.You will send message to correct AppServiceConnection
, and this should be thread safe. If you met actual thread save issue when performing this, you could ask issue with testing details.