標準エンドポイントの使用

StandardEndpoints のサンプルでは、サービスの構成ファイルでの標準エンドポイントの使用方法を示します。 標準エンドポイントでは、単一のプロパティを使用してアドレス、バインディング、およびコントラクトの組み合わせをそのエンドポイントに関連付けられている追加のプロパティで記述することで、エンドポイントの定義を簡単に行うことができます。 このサンプルでは、カスタムの標準エンドポイントを定義して実装する方法、およびエンドポイントの特定のプロパティを定義する方法を示します。

サンプルの詳細

サービス エンドポイントは、アドレス、バインディング、およびコントラクトの 3 つのパラメーターを指定して指定できます。 指定できるその他のパラメーターには、動作構成、ヘッダー、リッスン URI などがあります。 アドレス、バインディング、およびコントラクトのいずれかまたはすべてに、変更できない値が含まれている場合があります。 このような理由から、標準エンドポイントを使用できます。 このようなエンドポイントの例として、メタデータ交換エンドポイントや探索エンドポイントがあります。 また、標準エンドポイントでは、固定された性質の情報を提供したり、独自の標準エンドポイントを作成したりしなくても、サービス エンドポイントを構成できるため、ユーザビリティが向上します。たとえば、適切な既定の値のセットを指定し、構成ファイルの詳細を削減してユーザビリティを向上させることができます。

このサンプルは、2 つの標準エンドポイントを定義するサービスおよびサービスと通信するクライアントの 2 つのプロジェクトで構成されています。 構成ファイルでサービスの標準エンドポイントを定義する方法を次の例に示します。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <extensions>
      <endpointExtensions>
        <add name="customEndpoint" type="Microsoft.Samples.StandardEndpoints.CustomEndpointCollectionElement, service" />
      </endpointExtensions>
    </extensions>
    <services>
      <service name="Microsoft.Samples.StandardEndpoints.CalculatorService">
        <endpoint address="http://localhost:8000/Samples/Service.svc/customEndpoint" contract="Microsoft.Samples.StandardEndpoints.ICalculator" kind="customEndpoint" />
        <endpoint kind="mexEndpoint" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <standardEndpoints>
      <customEndpoint>
        <standardEndpoint property="True" />
      </customEndpoint>
    </standardEndpoints>
  </system.serviceModel>
</configuration>

サービスに定義されている最初のエンドポイントは、customEndpoint の一種で、その定義については、<standardEndpoints> セクションで確認できます。このセクションでは、プロパティ propertytrue の値が指定されています。 これは、新しいプロパティでカスタマイズされたエンドポイントの例です。 2 つ目のエンドポイントはメタデータ エンドポイントを表します。このエンドポイントでは、アドレス、バインディング、およびコントラクトの値が固定されています。

標準エンドポイント要素を定義するには、StandardEndpointElement から派生するクラスを作成する必要があります。 このサンプルでは、CustomEndpointElement クラスは、次の例のように定義されています。

public class CustomEndpointElement : StandardEndpointElement
{
    public bool Property
    {
        get { return (bool)base["property"]; }
        set { base["property"] = value; }
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            ConfigurationPropertyCollection properties = base.Properties;
            properties.Add(new ConfigurationProperty("property", typeof(bool), false, ConfigurationPropertyOptions.None));
            return properties;
        }
    }

    protected override Type EndpointType
    {
        get { return typeof(CustomEndpoint); }
    }

    protected override ServiceEndpoint CreateServiceEndpoint(ContractDescription contract)
    {
        return new CustomEndpoint();
    }

    protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
    {
        CustomEndpoint customEndpoint = (CustomEndpoint)endpoint;
        customEndpoint.Property = this.Property;
    }

    protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ChannelEndpointElement channelEndpointElement)
    {
        CustomEndpoint customEndpoint = (CustomEndpoint)endpoint;
        customEndpoint.Property = this.Property;
    }

    protected override void OnInitializeAndValidate(ServiceEndpointElement serviceEndpointElement)
    {
    }

    protected override void OnInitializeAndValidate(ChannelEndpointElement channelEndpointElement)
    {
    }
}

CreateServiceEndpoint 関数で、CustomEndpoint オブジェクトが作成されます。 その定義については、以下の例を参照してください。

public class CustomEndpoint : ServiceEndpoint
{
    public CustomEndpoint()
        : this(string.Empty)
    {
    }

    public CustomEndpoint(string address)
        : this(address, ContractDescription.GetContract(typeof(ICalculator)))
    {
    }

    public CustomEndpoint(string address, ContractDescription contract)
        : base(contract)
    {
        this.Binding = new BasicHttpBinding();
        this.IsSystemEndpoint = false;
    }

    public bool Property
    {
        get;
        set;
    }
}

サービスとクライアントとの間で通信を実行するために、クライアントでサービスに対するサービス参照が作成されます。 このサンプルをビルドして実行した場合、サービスが実行されて、クライアントはそのサービスと通信します。 サービスが変更されるたびに、サービス参照を更新する必要があります。

このサンプルを使用するには

  1. Visual Studio を使用して StandardEndpoints.sln ファイルを開きます。

  2. 複数のプロジェクトを起動できるようにします。

    1. ソリューション エクスプローラーで、標準エンドポイント ソリューションを右クリックしてから、 [プロパティ] を選択します。

    2. [共通プロパティ] で、 [スタートアップ プロジェクト] を選択してから、 [マルチ スタートアップ プロジェクト] をクリックします。

    3. Service プロジェクトを一覧の先頭に移動し、 [アクション][開始] に設定します。

    4. Client プロジェクトを Service プロジェクトの後ろに移動し、やはり [アクション][開始] に設定します。

      これで、Client プロジェクトは Service プロジェクトの後に実行されます。

  3. ソリューションを実行するには、F5 キーを押します。

Note

この手順が機能しない場合は、次の手順に従って、環境が適切に設定されていることを確認してください。

  1. Windows Communication Foundation サンプルの 1 回限りのセットアップの手順を実行したことを確認します。
  2. ソリューションをビルドするには、「Windows Communication Foundation サンプルのビルド」の手順に従います。
  3. 単一または複数コンピューター構成でサンプルを実行するには、「Windows Communication Foundation サンプルの実行」の手順に従います。