CallbackBehaviorAttribute.UseSynchronizationContext Property
Gets or sets a value that specifies whether to use the current synchronization context to choose the thread of execution.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Property Value
Type: System.Booleantrue if all calls to the service must run on the thread specified by the SynchronizationContext; otherwise, false. The default value is true.
Use this property to provide the user interface thread affinity that some applications require. For example, a Windows Forms application may be registered as a singleton service object. In this case, all calls into the service must run on the Windows Forms thread. The default case, in which UseSynchronizationContext is set to true, synchronizes all calls to the service to run on the user interface thread.
Note that the thread used is the current synchronization thread when DuplexChannelFactory<TChannel>.CreateChannel or DuplexClientBase<TChannel>.CreateChannel is called. In the case of an Windows Forms application, this means that these calls should occur after a call to the Application.Run method.
The following code example shows a CallbackBehaviorAttribute on a callback object that uses the SynchronizationContext object to determine which thread to marshal to, the ValidateMustUnderstand property to enforce message validation, and the IncludeExceptionDetailInFaults property to return exceptions as FaultException objects to the service for debugging purposes.
using System; using System.ServiceModel; using System.ServiceModel.Channels; using System.Threading; namespace Microsoft.WCF.Documentation { [CallbackBehaviorAttribute( IncludeExceptionDetailInFaults= true, UseSynchronizationContext=true, ValidateMustUnderstand=true )] public class Client : SampleDuplexHelloCallback { AutoResetEvent waitHandle; public Client() { waitHandle = new AutoResetEvent(false); } public void Run() { // Picks up configuration from the configuration file. SampleDuplexHelloClient wcfClient = new SampleDuplexHelloClient(new InstanceContext(this), "WSDualHttpBinding_SampleDuplexHello"); try { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Enter a greeting to send and press ENTER: "); Console.Write(">>> "); Console.ForegroundColor = ConsoleColor.Green; string greeting = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Called service with: \r\n\t" + greeting); wcfClient.Hello(greeting); Console.WriteLine("Execution passes service call and moves to the WaitHandle."); this.waitHandle.WaitOne(); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Set was called."); Console.Write("Press "); Console.ForegroundColor = ConsoleColor.Red; Console.Write("ENTER"); Console.ForegroundColor = ConsoleColor.Blue; Console.Write(" to exit..."); Console.ReadLine(); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); Console.ReadLine(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message); Console.ReadLine(); } } public static void Main() { Client client = new Client(); client.Run(); } public void Reply(string response) { Console.WriteLine("Received output."); Console.WriteLine("\r\n\t" + response); this.waitHandle.Set(); } } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.