CallbackBehaviorAttribute.ValidateMustUnderstand 屬性

定義

取得或設定值,這個值會指定系統或應用程式是否會強制執行 SOAP MustUnderstand 標頭處理。

C#
public bool ValidateMustUnderstand { get; set; }

屬性值

如果系統要執行 SOAP 標頭 true 處理,則為 MustUnderstand,否則為 false,表示應用程式會執行這項處理。 預設值是 true

範例

下列程式碼範例會顯示回呼物件 (它使用 CallbackBehaviorAttribute 物件判斷要封送處理的執行緒) 的 SynchronizationContext、可執行訊息驗證的 ValidateMustUnderstand 屬性,以及可將例外狀況當做 IncludeExceptionDetailInFaults 物件傳回服務以進行偵錯的 FaultException 屬性。

C#
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();
    }
  }
}

備註

使用 ValidateMustUnderstand 屬性,即可關閉對抵達之訊息標頭的強制驗證。 在正常的執行中,訊息標頭會與 UnderstoodHeaders 屬性進行比較,以確認回呼物件執行階段已明確處理到達的標頭。 將此屬性設定為 false,即可停用這項檢查。 如果是設定為 false,應用程式就必須檢查標示為 MustUnderstand="true" 的標頭,並在無法瞭解其中一或多個標頭時傳回錯誤。 當回呼應該接受任何傳入的 SOAP 訊息 (例如,使用具型別或不具型別的訊息時) 以及執行自訂標頭處理時,這種設定會十分有用。

適用於

產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1