KnownTypes 示例演示如何在数据协定中指定有关派生类型的信息。 数据协定允许你向/从服务传递结构化数据。 在面向对象的编程中,继承自其他类型的类型可用于代替原始类型。 在面向服务的编程中,将传达架构而不是类型,因此,不会保留类型之间的关系。 该 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);
}
DataContractAttribute和DataMemberAttribute应用于ComplexNumber
类,以指示该类中哪些字段可以在客户端和服务之间传递。 派生 ComplexNumberWithMagnitude
类可用于代替 ComplexNumber
。 类型 KnownTypeAttribute 上的 ComplexNumber
属性指示这一点。
[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
。 (尽管协定指定 ComplexNumber
,但由于 KnownTypeAttribute
属性,允许这样做)。 乘法和除法仍返回基 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中定义,这是由 ServiceModel 元数据实用工具工具(Svcutil.exe)从服务 元数据生成的。 由于属性 KnownTypeAttribute 是在服务的数据协定中指定的,因此客户端在使用服务时能够接收 ComplexNumber
和 ComplexNumberWithMagnitude
类。 客户端检测它是否获得了 ComplexNumberWithMagnitude
并生成相应的输出:
// 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 = 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.
设置、生成和运行示例
确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。
若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。