I have an assembly that runs under an STA thread within a sandbox from a third party, within this thread i have created a duplex WCF client that needs to execute methods on the original STA thread.
The current implementation works fine, within the Duplex callback i obtain the synchronization context of the STA thread as follows and use it to Post back to the STA thread:
private readonly SynchronizationContext _syncContext = AsyncOperationManager.SynchronizationContext;
This all runs within a WinForm initialised in the STA thread, great...but i need to move the WCF duplex proxy so it instead runs under a class instance within the main STA thread. When i remove the winform i end up getting a completely new thread from the above SynchronizationContext.
To clarify:
Winforms:-
- Start WCF Duplex Proxy on STA thread - ManagedThreadId = 1
- Receive duplex callback from server - ManagedThreadId = 5
- Post to callback event method using AsyncOperationManager.SynchronizationContext - ManagedThreadId = 1
Without WinForm (class instance):-
- Start WCF Duplex Proxy on STA thread - ManagedThreadId = 1
- Receive duplex callback from server - ManagedThreadId = 6
- Post to callback event method using AsyncOperationManager.SynchronizationContext - ManagedThreadId = 11
Executing on thread 11 instead of 1 means my methods fail to execute properly within the sandbox, there is no difference in the code between variants other than that one runs under a winform. Does anyone know how i can keep the duplex callback method execution in the main STA thread without using the winform?