我的 WCF 客户端出现以下错误:
"Error in line 1 position 1910. Element 'http://schemas.datacontract.org/2004/07/SubSonic:_currentValue' contains data of the 'http://schemas.datacontract.org/2004/07/System:DBNull' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'DBNull' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer."
我正在使用 Subsonic 2.0 来生成我的类对象。
我还使用DataContract
and装饰了我的班级和成员DataMember
。
已尝试添加[KnownType(typeof(System.DBNull))]
到我的 Generated 类。
已尝试添加[ServiceKnownType(typeof(System.DBNull))]
到我的服务接口。
我生成的类看起来像:(这个类是为 SQL View 生成的)
/// <summary>
/// This is Read-only wrapper class for the view.
/// </summary>
[Serializable]
[KnownType(typeof(System.DBNull))]
[DataContract]
public partial class VwPatientRt : ReadOnlyRecord<VwPatientRt>
{ ... ...
服务接口:
[ServiceContract]
[ServiceKnownType(typeof(System.DBNull))]
public interface IPatientService
{
[WebInvoke(Method = "GET", UriTemplate = "GetPatientAppointments")]
[OperationContract]
IList<VwPatientRt> GetPatientAppointments();
.....
服务等级:
public class PatientService : IPatientService
{
public IList<VwPatientRt> GetPatientAppointments()
{
... .
.....
}
...
在客户端,一切似乎都很好。因为对于其他方法和类,它工作正常。
在查看详细异常 DBNull 和 _currentValue 用于 Subsonic TableColumnSetting 类中。
我应该怎么办?
也用谷歌搜索并浏览了类似的帖子,但没有一个对我有帮助,并且在这方面已经挣扎了将近 2 天。
感谢您对代码片段的任何帮助或建议。
:解决了:
我的 WCF 服务是使用框架 4.5 构建的,而我的消费客户端是使用框架 3.5 构建的。由于这个添加服务引用向导没有为使用声明的 KnownTypes 生成类属性
[ServiceKnownType(typeof(System.DBNull))]
所以,当反序列化客户端没有得到 System.DBNull 类型时。我们必须在客户端配置文件中添加 knowntypes。
客户端需要的此配置解决了我的问题:
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="NameSpace.ServiceClientName.ClassNameForWhichKnownTypeIsToBeGiven, AssemblyName">
<knownType type="System.DBNull"></knownType>
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
希望这对某人有用。