【发布时间】:2012-03-16 11:00:13
【问题描述】:
全部
基本上,我的 WCF 服务需要连接到 DCOM 服务器并获取包含“VARIANT”字段的结构,然后将其传递给我的 WCF 服务客户端。
这是我从本地 DCOM 服务获得的这个结构的 WCF 数据协定
[DataContract]
[ComVisible(true)]
[StructLayout(LayoutKind.Sequential)]
public struct tagProcReadAns
{
[DataMember]
[MarshalAs(UnmanagedType.Struct)]
public object vItemValue; //VARIANT: marshalled as object in C#
[DataMember]
public ushort wQuality;
[DataMember]
public Int32 Error;}
因为我希望这个数据契约可以注册为 COM 对象并再次编组回 C++ 代码,这就是为什么我有 [ComVisible] 和 [MarshalAs] 部分
无论如何,vItemValue 是我从 DCOM Server 成功获得的 VARIANT 对象,(我已经验证了内容)。但是,当我尝试通过 WCF 传递异常时遇到异常。
我预计 vItemValue VARIANT 中包含的数据是“字节数组类型”,因为我看到 VARIANT.vt 值是“VTUI1|VT_ARRAY”,所以我将 DataContract 修改如下
[DataContract]
[KnownType(TypeOf(byte[])] //new
[ComVisible(true)]
[StructLayout(LayoutKind.Sequential)]
public struct tagProcReadAns
{
[DataMember]
[MarshalAs(UnmanagedType.Struct)]
public object vItemValue; //VARIANT: marshalled as object in C#
[DataMember]
public ushort wQuality;
[DataMember]
public Int32 Error;}
但是,当我再次运行代码时,我仍然遇到异常,通过添加跟踪,我有以下错误消息
There was an error while trying to serialize parameter http://tempuri.org/:aryAns. The InnerException message was 'Type 'System.Byte[*]' with data contract name 'ArrayOfunsignedByte:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver 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 DataContractSerializer.'. Please see InnerException for more details.
好像数据类型不是byte[]而是byte[*]??我做错了什么吗? 正如异常所暗示的那样,我可以使用 DataContractResolver 以某种方式解决这个问题,你能提出任何解决方案吗?
谢谢
【问题讨论】:
-
我想出了如何避免上面提到的序列化问题..
标签: .net wcf marshalling com-interop datacontractserializer