CallbackBehaviorAttribute.UseSynchronizationContext Eigenschaft

Definition

Ruft einen Wert ab bzw. legt einen Wert fest, der angibt, ob der aktuelle Synchronisierungskontext zum Auswählen des Ausführungsthreads verwendet wird.

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

Eigenschaftswert

Boolean

true, wenn alle Aufrufe des Diensts in dem vom SynchronizationContext angegebenen Thread ausgeführt werden müssen, andernfalls false. Der Standardwert ist true.

Beispiele

Das folgende Codebeispiel zeigt ein CallbackBehaviorAttribute für ein Rückrufobjekt, das das SynchronizationContext-Objekt zur Bestimmung des Threads für das Marshallen, die ValidateMustUnderstand-Eigenschaft zum Erzwingen der Nachrichtenvalidierung und die IncludeExceptionDetailInFaults-Eigenschaft zum Zurückgeben von Ausnahmen als FaultException-Objekte zu Debuggingzwecken an den Dienst verwendet.

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

Hinweise

Verwenden Sie diese Eigenschaft, um die Threadaffinität der Benutzeroberfläche bereitzustellen, die einige Anwendungen erfordern. Zum Beispiel wird eine Windows Forms-Anwendung möglicherweise als Singleton-Dienstobjekt registriert. In diesem Fall müssen alle Aufrufe in den Dienst auf dem Windows Forms-Thread ausgeführt werden. Der Standardfall, in dem UseSynchronizationContext auf true festgelegt wird, synchronisiert alle Aufrufe an den Dienst für die Ausführung im Benutzeroberflächenthread.

Beachten Sie, dass der verwendete Thread der aktuelle Synchronisierungsthread ist, wenn DuplexChannelFactory<TChannel>.CreateChannel oder DuplexClientBase<TChannel>.CreateChannel aufgerufen wird. Bei einer Windows Forms-Anwendung bedeutet das, dass diese Aufrufe nach einem Aufruf der Application.Run-Methode erfolgen sollten.

Gilt für