サービスの説明

ServiceDescription のサンプルでは、サービスが実行時にそのサービスの説明情報を取得する方法を示します。 このサンプルは、「入門サンプル」に基づいており、サービスに関する説明情報を返す追加のサービス操作を定義しています。 返される情報には、サービスのベース アドレスとエンドポイントが示されます。 サービスは、OperationContextServiceHost、および ServiceDescription クラスを使用してこの情報を提供します。

この例では、クライアントはコンソール アプリケーション (.exe) であり、サービスはインターネット インフォメーション サービス (IIS) によってホストされます。

Note

このサンプルのセットアップ手順とビルド手順については、このトピックの最後を参照してください。

このサンプルには、IServiceDescriptionCalculator という電卓コントラクトの修正バージョンがあります。 このコントラクトでは、GetServiceDescriptionInfo という名前の追加サービス操作が定義されています。このサービス操作は、サービスのベース アドレス (1 つまたは複数) とサービス エンドポイント (1 つまたは複数) を説明する、複数行の文字列をクライアントに返します。

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface IServiceDescriptionCalculator
{
    [OperationContract]
    int Add(int n1, int n2);
    [OperationContract]
    int Subtract(int n1, int n2);
    [OperationContract]
    int Multiply(int n1, int n2);
    [OperationContract]
    int Divide(int n1, int n2);
    [OperationContract]
    string GetServiceDescriptionInfo();
}

GetServiceDescriptionInfo の実装コードには、サービス エンドポイントを示す ServiceDescription が使用されています。 サービス エンドポイントには相対アドレスを指定できるので、最初にサービスのベース アドレスが示されます。 この情報をすべて取得するには、コードで Current を使用して操作コンテキストを取得します。 その操作コンテキストから、ServiceHost とその ServiceDescription オブジェクトが取得されます。 サービスのベース エンドポイント一覧を示すには、コードにより、サービス ホストの BaseAddresses コレクションを反復処理します。 サービスのサービス エンドポイント一覧を示すには、コードにより、サービス説明のエンドポイント コレクションを反復処理します。

public string GetServiceDescriptionInfo()
{
    string info = "";
    OperationContext operationContext = OperationContext.Current;
    ServiceHost host = (ServiceHost)operationContext.Host;
    ServiceDescription desc = host.Description;
    // Enumerate the base addresses in the service host.
    info += "Base addresses:\n";
    foreach (Uri uri in host.BaseAddresses)
    {
        info += "    " + uri + "\n";
    }
    // Enumerate the service endpoints in the service description.
    info += "Service endpoints:\n";
    foreach (ServiceEndpoint endpoint in desc.Endpoints)
    {
        info += "    Address:  " + endpoint.Address + "\n";
        info += "    Binding:  " + endpoint.Binding.Name + "\n";
        info += "    Contract: " + endpoint.Contract.Name + "\n";
    }
     return info;
}

サンプルを実行すると、電卓操作が表示され、次に GetServiceDescriptionInfo 操作によって返されたサービス情報が表示されます。 クライアントをシャットダウンするには、クライアント ウィンドウで Enter キーを押します。

Add(15,3) = 18
Subtract(145,76) = 69
Multiply(9,81) = 729
Divide(22,7) = 3
GetServiceDescriptionInfo
Base addresses:
    http://<machine-name>/ServiceModelSamples/service.svc
    https://<machine-name>/ServiceModelSamples/service.svc
Service endpoints:
    Address:  http://<machine-name>/ServiceModelSamples/service.svc
    Binding:  WSHttpBinding
    Contract: IServiceDescriptionCalculator
    Address:  http://<machine-name>/ServiceModelSamples/service.svc/mex
    Binding:  MetadataExchangeHttpBinding
    Contract: IMetadataExchange

Press <ENTER> to terminate client.

サンプルをセットアップ、ビルド、および実行するには

  1. Windows Communication Foundation サンプルの 1 回限りのセットアップの手順を実行したことを確認します。

  2. ソリューションの C# 版または Visual Basic .NET 版をビルドするには、「 Building the Windows Communication Foundation Samples」の手順に従います。

  3. 単一または複数コンピューター構成でサンプルを実行するには、「Windows Communication Foundation サンプルの実行」の手順に従います。