基本示例演示如何实现数据协定。 数据协定允许你向/从服务传递结构化数据。 此示例基于 入门, 但使用复数而不是基本数值类型。
在此示例中,该服务由 Internet Information Services (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);
}
}
客户端实现还使用复数。 服务协定和数据协定在源文件generatedClient.cs中定义,这是由 ServiceModel 元数据实用工具工具(Svcutil.exe) 从服务元数据生成的。
// 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 示例中的说明进行操作。