CallbackBehaviorAttribute.UseSynchronizationContext CallbackBehaviorAttribute.UseSynchronizationContext CallbackBehaviorAttribute.UseSynchronizationContext CallbackBehaviorAttribute.UseSynchronizationContext Property

Definizione

Ottiene o imposta un valore che specifica se utilizzare il contesto di sincronizzazione corrente per scegliere il thread di esecuzione.Gets or sets a value that specifies whether to use the current synchronization context to choose the thread of execution.

public:
 property bool UseSynchronizationContext { bool get(); void set(bool value); };
public bool UseSynchronizationContext { get; set; }
member this.UseSynchronizationContext : bool with get, set
Public Property UseSynchronizationContext As Boolean

Valore della proprietà

true se tutte le chiamate al servizio devono essere eseguite sul thread specificato da SynchronizationContext; in caso contrario, false.true if all calls to the service must run on the thread specified by the SynchronizationContext; otherwise, false. Il valore predefinito è true.The default value is true.

Esempi

Nell'esempio di codice seguente vengono illustrati CallbackBehaviorAttribute su un oggetto callback che utilizza l'oggetto SynchronizationContext per determinare il thread su cui effettuare il marshalling, la proprietà ValidateMustUnderstand per applicare la convalida del messaggio e la proprietà IncludeExceptionDetailInFaults per restituire eccezioni come oggetti FaultException al servizio a scopo di debug.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();
    }
  }
}

Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Threading

Namespace Microsoft.WCF.Documentation
  <CallbackBehaviorAttribute(IncludeExceptionDetailInFaults:= True, UseSynchronizationContext:=True, ValidateMustUnderstand:=True)> _
  Public Class Client
      Implements SampleDuplexHelloCallback
    Private waitHandle As AutoResetEvent

    Public Sub New()
      waitHandle = New AutoResetEvent(False)
    End Sub

    Public Sub Run()
      ' Picks up configuration from the configuration file.
      Dim wcfClient As New SampleDuplexHelloClient(New InstanceContext(Me), "WSDualHttpBinding_SampleDuplexHello")
      Try
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Enter a greeting to send and press ENTER: ")
        Console.Write(">>> ")
        Console.ForegroundColor = ConsoleColor.Green
        Dim greeting As String = Console.ReadLine()
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Called service with: " & Constants.vbCrLf & Constants.vbTab & greeting)
        wcfClient.Hello(greeting)
        Console.WriteLine("Execution passes service call and moves to the WaitHandle.")
        Me.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 timeProblem As TimeoutException
        Console.WriteLine("The service operation timed out. " & timeProblem.Message)
        Console.ReadLine()
      Catch commProblem As CommunicationException
        Console.WriteLine("There was a communication problem. " & commProblem.Message)
        Console.ReadLine()
      End Try
    End Sub
    Public Shared Sub Main()
      Dim client As New Client()
      client.Run()
    End Sub

    Public Sub Reply(ByVal response As String) Implements SampleDuplexHelloCallback.Reply
      Console.WriteLine("Received output.")
      Console.WriteLine(Constants.vbCrLf & Constants.vbTab & response)
      Me.waitHandle.Set()
    End Sub
  End Class
End Namespace

Commenti

Utilizzare questa proprietà per fornire l'affinità di thread dell'interfaccia utente richiesta da alcune applicazioni.Use this property to provide the user interface thread affinity that some applications require. Ad esempio, è possibile che un'applicazione Windows Forms sia registrata come oggetto servizio singleton.For example, a Windows Forms application may be registered as a singleton service object. In questo caso, tutte le chiamate nel servizio devono essere eseguite sul thread di Windows Form.In this case, all calls into the service must run on the Windows Forms thread. Nel caso predefinito, ovvero quando la proprietà UseSynchronizationContext è impostata su true, tutte le chiamate al servizio vengono sincronizzate per essere eseguite sul thread dell'interfaccia utente.The default case, in which UseSynchronizationContext is set to true, synchronizes all calls to the service to run on the user interface thread.

Si noti che il thread utilizzato è il thread di sincronizzazione corrente quando viene chiamato DuplexChannelFactory<TChannel>.CreateChannel o DuplexClientBase<TChannel>.CreateChannel.Note that the thread used is the current synchronization thread when DuplexChannelFactory<TChannel>.CreateChannel or DuplexClientBase<TChannel>.CreateChannel is called. Nel caso di un'applicazione Windows Form, questo significa che tali chiamate devono avvenire dopo una chiamata al metodo Application.Run.In the case of an Windows Forms application, this means that these calls should occur after a call to the Application.Run method.

Si applica a