Windows コミュニケーション ファンデーション

Windows Communication Foundation (WCF) は、サービス指向アプリケーションを構築するためのフレームワークです。 WCF を使用すると、あるサービス エンドポイントから別のサービス エンドポイントにデータを非同期メッセージとして送信できます。サービス エンドポイントは、IIS によってホストされる継続的に利用可能なサービスの一部にすることも、アプリケーションでホストされるサービスにすることもできます。メッセージは、XML として送信される単一の文字または単語のように単純なものにすることも、バイナリ データのストリームのように複雑にすることもできます。

# 開始サンプル

サービスは、メタデータとして公開するサービス コントラクトで実行する操作を記述します。

// Define a service contract.  
[ServiceContract(Namespace="http://StackOverflow.ServiceModel.Samples")]  
public interface ICalculator  
{  
    [OperationContract]  
    double Add(double n1, double n2);
}

次のコード例に示すように、サービス実装は適切な結果を計算して返します。

// Service class that implements the service contract.  
public class CalculatorService : ICalculator  
{  
    public double Add(double n1, double n2)  
    {  
        return n1 + n2;  
    }
}

サービスは、次のサンプル構成に示すように、構成ファイル (Web.config) を使用して定義された、サービスと通信するためのエンドポイントを公開します。

<services>  
    <service   
        name="StackOverflow.ServiceModel.Samples.CalculatorService"  
        behaviorConfiguration="CalculatorServiceBehavior">  
        <!-- ICalculator is exposed at the base address provided by  
         host: http://localhost/servicemodelsamples/service.svc.  -->  
       <endpoint address=""  
              binding="wsHttpBinding"  
              contract="StackOverflow.ServiceModel.Samples.ICalculator" />  
       ...  
    </service>  
</services>

フレームワークは、既定ではメタデータを公開しません。そのため、サービスは ServiceMetadataBehavior をオンにし、 http://localhost/servicemodelsamples/service.svc/mex でメタデータ交換 (MEX) エンドポイントを公開します。次の構成はこれを示しています。

<system.serviceModel>  
  <services>  
    <service   
        name="StackOverflow.ServiceModel.Samples.CalculatorService"  
        behaviorConfiguration="CalculatorServiceBehavior">  
      ...  
      <!-- the mex endpoint is explosed at  
       http://localhost/servicemodelsamples/service.svc/mex -->  
      <endpoint address="mex"  
                binding="mexHttpBinding"  
                contract="IMetadataExchange" />  
    </service>  
  </services>  

  <!--For debugging purposes set the includeExceptionDetailInFaults  
   attribute to true-->  
  <behaviors>  
    <serviceBehaviors>  
      <behavior name="CalculatorServiceBehavior">  
        <serviceMetadata httpGetEnabled="True"/>  
        <serviceDebug includeExceptionDetailInFaults="False" />  
      </behavior>  
    </serviceBehaviors>  
  </behaviors>  
</system.serviceModel>  

クライアントは、ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) によって生成されるクライアント クラスを使用して、特定のコントラクト タイプを使用して通信します。

クライアント ディレクトリの SDK コマンド プロンプトから次のコマンドを実行して、型付きプロキシを生成します。

svcutil.exe /n:"http://StackOverflow.ServiceModel.Samples,StackOverflow.ServiceModel.Samples" http://localhost/servicemodelsamples/service.svc/mex /out:generatedClient.cs  

サービスと同様に、クライアントは構成ファイル (App.config) を使用して、通信するエンドポイントを指定します。次の例に示すように、クライアント エンドポイント構成は、サービス エンドポイント、バインディング、およびコントラクトの絶対アドレスで構成されます。

<client>  
     <endpoint  
         address="http://localhost/servicemodelsamples/service.svc"   
         binding="wsHttpBinding"   
         contract="StackOverflow.ServiceModel.Samples.ICalculator" />  
</client>  

次のコード例に示すように、クライアント実装はクライアントをインスタンス化し、型付きインターフェースを使用してサービスとの通信を開始します。

// Create a client.  
CalculatorClient client = new CalculatorClient();  

// Call the Add service operation.  
double value1 = 100.00D;  
double value2 = 15.99D;  
double result = client.Add(value1, value2);  
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); 

//Closing the client releases all communication resources.  
client.Close();  


No