次の方法で共有


クライアント Run-Time 動作の指定

Windows Communication Foundation (WCF) サービスなどの Windows Communication Foundation (WCF) クライアントは、クライアント アプリケーションに合わせて実行時の動作を変更するように構成できます。 クライアントの実行時動作を指定するには、3 つの属性を使用できます。 双方向クライアント コールバック オブジェクトは、 CallbackBehaviorAttribute 属性と CallbackDebugBehavior 属性を使用して、実行時の動作を変更できます。 もう 1 つの属性 ( ClientViaBehavior) を使用して、論理宛先を即時ネットワーク宛先から分離できます。 さらに、双方向クライアント コールバックの種類では、サービス側の動作の一部を使用できます。 詳細については、「 サービス Run-Time 動作の指定」を参照してください。

CallbackBehaviorAttribute の使用

CallbackBehaviorAttribute クラスを使用して、クライアント アプリケーションでのコールバック コントラクト実装の実行動作を構成または拡張できます。 この属性は、インスタンス化動作とトランザクション設定を除き、コールバック クラスに対して ServiceBehaviorAttribute クラスと同様の関数を実行します。

CallbackBehaviorAttribute クラスは、コールバック コントラクトを実装するクラスに適用する必要があります。 非二重コントラクトの実装に適用した場合、実行時に InvalidOperationException 例外が発生します。 次のコード例は、CallbackBehaviorAttribute オブジェクトを使用してマーシャリングするスレッドを決定するコールバック オブジェクトのSynchronizationContext クラス、メッセージ検証を適用するValidateMustUnderstand プロパティ、デバッグ目的で例外をサービスにIncludeExceptionDetailInFaultsオブジェクトとして返すFaultException プロパティを示しています。

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

CallbackDebugBehavior を使用してマネージド例外情報のフローを有効にする

IncludeExceptionDetailInFaults プロパティをプログラムまたはアプリケーション構成ファイルからtrueするように設定することで、デバッグ目的でクライアント コールバック オブジェクト内のマネージド例外情報のフローをサービスに戻すことができます。

例外の詳細では、承認されていないサービスが使用できる内部クライアント実装に関する情報が公開されるため、マネージド例外情報をサービスに返すことはセキュリティ 上のリスクになる可能性があります。 また、 CallbackDebugBehavior プロパティはプログラムで設定することもできますが、デプロイ時に IncludeExceptionDetailInFaults を無効にすることを忘れやすい場合があります。

セキュリティの問題が発生するため、次の点を強くお勧めします。

  • アプリケーション構成ファイルを使用して、 IncludeExceptionDetailInFaults プロパティの値を trueに設定します。

  • これは、制御されたデバッグ シナリオでのみ行います。

次のコード例は、SOAP メッセージ内のクライアント コールバック オブジェクトからマネージド例外情報を返すように WCF に指示するクライアント構成ファイルを示しています。

  <client>
      <endpoint 
        address="http://localhost:8080/DuplexHello" 
        binding="wsDualHttpBinding"
        bindingConfiguration="WSDualHttpBinding_SampleDuplexHello"
        contract="SampleDuplexHello" 
        name="WSDualHttpBinding_SampleDuplexHello"
        behaviorConfiguration="enableCallbackDebug">
      </endpoint>
  </client>
<behaviors>
  <endpointBehaviors>
    <behavior name="enableCallbackDebug">
      <callbackDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

ClientViaBehavior 動作の使用

ClientViaBehavior動作を使用して、トランスポート チャネルを作成する必要がある Uniform Resource Identifier を指定できます。 この動作は、即時ネットワーク宛先がメッセージの意図されたプロセッサでない場合に使用します。 これにより、呼び出し元アプリケーションが必ずしも最終的な宛先を認識していない場合、または宛先 Via ヘッダーがアドレスでない場合に、複数ホップの会話が可能になります。

こちらも参照ください