已知类型

此示例演示如何在数据协定中指定有关派生类型的信息。数据协定允许您在服务中传入和传出结构化数据。在面向对象的编程中,可以用从另一个类型继承的类型来代替原始类型。在面向服务的编程中,传递的是架构(而不是类型),因此,类型之间的关系将不保留。KnownTypeAttribute 属性允许在数据协定中包括有关派生类型的信息。如果不使用此机制,则不能在应当使用基类型的情况下发送或接收派生类型。

提示

本主题的末尾介绍了此示例的设置过程和生成说明。

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

// 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 类,这将指示在客户端和服务之间可以传递类的哪些字段。可以用 ComplexNumberWithMagnitude 派生类来代替 ComplexNumberComplexNumber 类型的 KnownTypeAttribute 属性说明了这一点。

[DataContract(Namespace="http://Microsoft.ServiceModel.Samples")]
[KnownType(typeof(ComplexNumberWithMagnitude))]
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;
    }
}

ComplexNumberWithMagnitude 类型派生自 ComplexNumber,但是额外添加了一个数据成员 Magnitude

[DataContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public class ComplexNumberWithMagnitude : ComplexNumber
{
    public ComplexNumberWithMagnitude(double real, double imaginary) :        base(real, imaginary) { }

    [DataMember]
    public double Magnitude
    {
        get { return Math.Sqrt(Imaginary*Imaginary  + Real*Real); }
        set { throw new NotImplementedException(); }
    }
}

为了演示已知类型功能,此服务按照只针对加法和减法返回 ComplexNumberWithMagnitude 的方式来实现。(由于 KnownTypeAttribute 属性的存在,即使协定中指定了 ComplexNumber,这也是允许的。)乘法和除法仍将返回 ComplexNumber 基类型。

public class DataContractCalculatorService : IDataContractCalculator
{
    public ComplexNumber Add(ComplexNumber n1, ComplexNumber n2)
    {
        //Return the derived type.
        return new ComplexNumberWithMagnitude(n1.Real + n2.Real,
                                      n1.Imaginary + n2.Imaginary);
    }

    public ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2)
    {
        //Return the derived type.
        return new ComplexNumberWithMagnitude(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 the base type.
        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 the base type.
        return new ComplexNumber(numerator.Real / denominator.Real,
                                             numerator.Imaginary);
    }
}

在客户端上,服务协定和数据协定是在源文件 generatedClient.cs 中定义的,该文件是由Service Metadata Utility Tool (Svcutil.exe) 根据服务元数据生成的。由于在服务的数据协定中指定了 KnownTypeAttribute 属性,因此在使用服务时,客户端既能够接收 ComplexNumber 类又能够接收 ComplexNumberWithMagnitude 类。客户端检测它是否获得了 ComplexNumberWithMagnitude 并生成相应的输出:

// 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 = client.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);
if (result is ComplexNumberWithMagnitude)
{
    Console.WriteLine("Magnitude: {0}",         ((ComplexNumberWithMagnitude)result).Magnitude);
}
else
{
    Console.WriteLine("No magnitude was sent from the service");
}

运行示例时,操作的请求和响应将显示在客户端控制台窗口中。请注意,对加法和减法列显了数量级,而对乘法和除法却没有列显,这是由服务的实现方式确定的。在客户端窗口中按 Enter 可以关闭客户端。

Add(1 + 2i, 3 + 4i) = 4 + 6i
Magnitude: 7.21110255092798
Subtract(1 + 2i, 3 + 4i) = -2 + -2i
Magnitude: 2.82842712474619
Multiply(2 + 3i, 4 + 7i) = -13 + 26i
No magnitude was sent from the service
Divide(3 + 7i, 5 + -2i) = 0.0344827586206897 + 41i
No magnitude was sent from the service

    Press <ENTER> to terminate client.

设置、生成和运行示例

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

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

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

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.