WCF:服务器没有提供有意义的回复;
WCF: The server did not provide a meaningful reply;
我有一个相当简单的 WCF 服务来回发送传感器状态。
其中一个类型包含 Type
类型的 属性
public class SensorValue
{
public string Name { get; set; }
public string Value { get; set; }
public Type ValueType { get; set; }
}
当我通过 WCF 检索传感器值列表时,出现主题中提到的错误。
为什么不允许我通过 WCF 序列化 Type
有明显的原因吗?
System.Type
是抽象的。假设您将 属性 设置为 typeof(System.Int32)
,您将得到一个具体的实现,很可能是 System.RuntimeType
。 (Un)幸运的是 that type is not public 所以 Datacontract 序列化器根本无法得到它。它失败了:
SerializationException: Type 'System.RuntimeType' with data contract name 'RuntimeType:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
尝试任何指导来解决您的问题都是没有用的。将 ValueType 属性 的类型替换为正常的东西(例如字符串)并提供足够的信息以便客户端可以根据您在 ValueType 中提供的内容重新创建类型要容易得多。
我使用 this answer from Richard Petheram 中的代码来证实我的怀疑,即根本原因是序列化程序的灾难性故障。
我有一个相当简单的 WCF 服务来回发送传感器状态。
其中一个类型包含 Type
public class SensorValue
{
public string Name { get; set; }
public string Value { get; set; }
public Type ValueType { get; set; }
}
当我通过 WCF 检索传感器值列表时,出现主题中提到的错误。
为什么不允许我通过 WCF 序列化 Type
有明显的原因吗?
System.Type
是抽象的。假设您将 属性 设置为 typeof(System.Int32)
,您将得到一个具体的实现,很可能是 System.RuntimeType
。 (Un)幸运的是 that type is not public 所以 Datacontract 序列化器根本无法得到它。它失败了:
SerializationException: Type 'System.RuntimeType' with data contract name 'RuntimeType:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
尝试任何指导来解决您的问题都是没有用的。将 ValueType 属性 的类型替换为正常的东西(例如字符串)并提供足够的信息以便客户端可以根据您在 ValueType 中提供的内容重新创建类型要容易得多。
我使用 this answer from Richard Petheram 中的代码来证实我的怀疑,即根本原因是序列化程序的灾难性故障。