To disable this behavior, I have disable the UseSynchonizationContext switch in the ServiceBehavior attribute of the service implementation class.
[ServiceContract]
public interface IMyService
{
[OperationContract]
void DoSomething();
}
[ServiceBehavior(UseSynchonizationContext = false)]
public class MyServiceImpl : IMyService
{
public void DoSomething()
{
...
}
}
Unfortunately, setting this flag to false also means I have to be extra careful with the golden rule of GUI and multithreading programming:
"Thou shalt only update UI using code that runs on the same thread as the UI."
Disabling UseSynchonizationContext causes WCF to process incoming client request using background thread. Thus, if we need to update the UI during the service operation execution, I'll need to use Dispatcher.BeginInvoke to marshal the call to the UI dispatcher thread to perform the UI update operations. For more information on multithreading and GUI, see this article from WPF SDK blog:
Thou Shalt Not Break the Golden Rule of Windows Multithreading. Or, Why the Dispatcher Rocks.
No comments:
Post a Comment