Basic サンプルでは、データ コントラクトを実装する方法を示します。 データ コントラクトを使用すると、サービスとの間で構造化データを渡すことができます。 このサンプルは 作業の開始 に基づいていますが、基本的な数値型の代わりに複素数を使用します。
このサンプルでは、サービスはインターネット インフォメーション サービス (IIS) によってホストされ、クライアントはコンソール アプリケーション (.exe) です。
注
このサンプルのセットアップ手順とビルド手順は、このトピックの最後にあります。
このサービスのサービス コントラクトでは、次のサンプル コードに示すように、複素数が使用されます。
// Define a service contract.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
ComplexNumber Add(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2);
}
次のサンプル コードに示すように、 DataContractAttribute 属性と DataMemberAttribute 属性が ComplexNumber
クラスの定義に適用され、クライアントとサービスの間でワイヤ経由で渡すことができるクラスのフィールドが示されています。
[DataContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public class ComplexNumber
{
[DataMember]
public double Real = 0.0D;
[DataMember]
public double Imaginary = 0.0D;
public ComplexNumber(double real, double imaginary)
{
this.Real = real;
this.Imaginary = imaginary;
}
}
サービス実装は、適切な結果を計算して返し、 ComplexNumber
型の数値を受け入れて返します。
// This is the service class that implements the service contract.
public class CalculatorService : ICalculator
{
public ComplexNumber Add(ComplexNumber n1, ComplexNumber n2)
{
return new ComplexNumber(n1.Real + n2.Real, n1.Imaginary +
n2.Imaginary);
}
public ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2)
{
return new ComplexNumber(n1.Real - n2.Real, n1.Imaginary -
n2.Imaginary);
}
public ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2)
{
double real1 = n1.Real * n2.Real;
double imaginary1 = n1.Real * n2.Imaginary;
double imaginary2 = n2.Real * n1.Imaginary;
double real2 = n1.Imaginary * n2.Imaginary * -1;
return new ComplexNumber(real1 + real2, imaginary1 +
imaginary2);
}
public ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2)
{
ComplexNumber conjugate =
new ComplexNumber(n2.Real, -1*n2.Imaginary);
ComplexNumber numerator = Multiply(n1, conjugate);
ComplexNumber denominator = Multiply(n2, conjugate);
return new ComplexNumber(numerator.Real / denominator.Real,
numerator.Imaginary);
}
}
クライアント実装では、複素数も使用されます。 サービス コントラクトとデータ コントラクトの両方が、サービス メタデータから ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) によって生成されるソース ファイル generatedClient.csで定義されます。
// Create a client.
DataContractCalculatorClient client = new DataContractCalculatorClient();
// Call the Add service operation.
ComplexNumber value1 = new ComplexNumber()
{
Real = 1,
Imaginary = 2
};
ComplexNumber value2 = new ComplexNumber()
{
Real = 3,
Imaginary = 4
};
ComplexNumber result = proxy.Add(value1, value2);
Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i",
value1.Real, value1.Imaginary, value2.Real, value2.Imaginary,
result.Real, result.Imaginary);
…
}
サンプルを実行すると、操作の要求と応答がクライアント コンソール ウィンドウに表示されます。 クライアント ウィンドウで Enter キーを押して、クライアントをシャットダウンします。
Add(1 + 2i, 3 + 4i) = 4 + 6i
Subtract(1 + 2i, 3 + 4i) = -2 + -2i
Multiply(2 + 3i, 4 + 7i) = -13 + 26i
Divide(3 + 7i, 5 + -2i) = 0.0344827586206897 + 41i
Press <ENTER> to terminate client.
サンプルを設定、ビルド、実行するには
Windows Communication Foundation サンプル のOne-Time セットアップ手順を実行していることを確認します。
ソリューションの C# または Visual Basic .NET エディションをビルドするには、「Windows Communication Foundation サンプルのビルド」の手順に従います。
単一または複数のコンピューター間の構成でサンプルを実行するには、「Windows Communication Foundation Samplesの実行」の手順に従います。