次の方法で共有


クライアント動作の構成

Windows Communication Foundation (WCF) は、動作構成 (クライアント アプリケーション構成ファイルの <behavior> セクションで定義されている動作構成) を参照するか、呼び出し元のアプリケーションでプログラムによって、2 つの方法で動作を構成します。 このトピックでは、両方の方法について説明します。

構成ファイルを使用する場合、動作構成は構成設定の名前付きコレクションです。 各動作構成の名前は一意である必要があります。 この文字列は、エンドポイントを動作にリンクするために、エンドポイント構成の behaviorConfiguration 属性で使用されます。

例 1

次の構成コードは、 myBehaviorと呼ばれる動作を定義します。 クライアント エンドポイントは、 behaviorConfiguration 属性でこの動作を参照します。

<configuration>  
    <system.serviceModel>  
        <behaviors>  
            <endpointBehaviors>  
                <behavior name="myBehavior">  
                    <clientVia />  
                </behavior>  
            </endpointBehaviors>  
        </behaviors>  
        <bindings>  
            <basicHttpBinding>  
                <binding name="myBinding" maxReceivedMessageSize="10000" />  
            </basicHttpBinding>  
        </bindings>  
        <client>  
            <endpoint address="myAddress" binding="basicHttpBinding" bindingConfiguration="myBinding" behaviorConfiguration="myBehavior" contract="myContract" />  
        </client>  
    </system.serviceModel>  
</configuration>  

プログラムによる動作の使用

また、クライアントを開く前に、Windows Communication Foundation (WCF) クライアント オブジェクトまたはクライアント チャネル ファクトリ オブジェクトで適切な Behaviors プロパティを見つけることで、プログラムによって動作を構成または挿入することもできます。

例 2

次のコード例では、チャネル オブジェクトを作成する前に、Behaviors プロパティから返されたServiceEndpointEndpoint プロパティにアクセスして、プログラムによって動作を挿入する方法を示します。

public class Client
{
  public static void Main()
  {
    try
    {
      // Picks up configuration from the config file.
      ChannelFactory<ISampleServiceChannel> factory
        = new ChannelFactory<ISampleServiceChannel>("WSHttpBinding_ISampleService");

      // Add the client side behavior programmatically to all created channels.
      factory.Endpoint.Behaviors.Add(new EndpointBehaviorMessageInspector());

      ISampleServiceChannel wcfClientChannel = factory.CreateChannel();

      // Making calls.
      Console.WriteLine("Enter the greeting to send: ");
      string greeting = Console.ReadLine();
      Console.WriteLine("The service responded: " + wcfClientChannel.SampleMethod(greeting));

      Console.WriteLine("Press ENTER to exit:");
      Console.ReadLine();

      // Done with service.
      wcfClientChannel.Close();
      Console.WriteLine("Done!");
    }
    catch (TimeoutException timeProblem)
    {
      Console.WriteLine("The service operation timed out. " + timeProblem.Message);
      Console.Read();
    }
    catch (FaultException<SampleFault> fault)
    {
      Console.WriteLine($"SampleFault fault occurred: {fault.Detail.FaultMessage}");
      Console.Read();
    }
    catch (CommunicationException commProblem)
    {
      Console.WriteLine("There was a communication problem. " + commProblem.Message);
      Console.Read();
    }
  }
Public Class Client
    Public Shared Sub Main()
        Try
            ' Picks up configuration from the config file.
            Dim factory As New ChannelFactory(Of ISampleServiceChannel)("WSHttpBinding_ISampleService")

            ' Add the client side behavior programmatically to all created channels.
            factory.Endpoint.Behaviors.Add(New EndpointBehaviorMessageInspector())

            Dim wcfClientChannel As ISampleServiceChannel = factory.CreateChannel()

            ' Making calls.
            Console.WriteLine("Enter the greeting to send: ")
            Dim greeting As String = Console.ReadLine()
            Console.WriteLine("The service responded: " & wcfClientChannel.SampleMethod(greeting))

            Console.WriteLine("Press ENTER to exit:")
            Console.ReadLine()

            ' Done with service. 
            wcfClientChannel.Close()
            Console.WriteLine("Done!")
        Catch timeProblem As TimeoutException
            Console.WriteLine("The service operation timed out. " & timeProblem.Message)
            Console.Read()
        Catch fault As FaultException(Of SampleFault)
            Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage)
            Console.Read()
        Catch commProblem As CommunicationException
            Console.WriteLine("There was a communication problem. " & commProblem.Message)
            Console.Read()
        End Try
    End Sub

こちらも参照ください