基本数据协定

本示例演示如何实现数据协定。数据协定允许您在服务中传入和传出结构化数据。本示例基于入门示例,但使用复数代替基本数字类型。

在本示例中,服务是由 Internet 信息服务 (IIS) 承载的,客户端是一个控制台应用程序 (.exe)。

ms752104.note(zh-cn,VS.100).gif注意:
本主题的最后介绍了此示例的设置过程和生成说明。

此服务的服务协定使用复数,如下面的示例代码所示。

// 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);
}

DataContractAttributeDataMemberAttribute 属性已应用于 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(); 
value1.Real = 1; 
value1.Imaginary = 2;
ComplexNumber value2 = new ComplexNumber(); 
value2.Real = 3;
value2.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.

设置、生成和运行示例

  1. 请确保已经执行了 Windows Communication Foundation 示例的一次性安装过程

  2. 若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。

  3. 若要用单机配置或跨计算机配置来运行示例,请按照Running the Windows Communication Foundation Samples中的说明进行操作。

ms752104.Important(zh-cn,VS.100).gif 注意:
您的计算机上可能已安装这些示例。在继续操作之前,请先检查以下(默认)目录。

<安装驱动器>:\WF_WCF_Samples

如果此目录不存在,请访问针对 .NET Framework 4 的 Windows Communication Foundation (WCF) 和 Windows Workflow Foundation (WF) 示例(可能为英文网页),下载所有 Windows Communication Foundation (WCF) 和 WF 示例。此示例位于以下目录。

<安装驱动器>:\WF_WCF_Samples\WCF\Basic\Contract\Data\Basic