wcf序列化
WCF新手,需要帮助了解序列化无法正常工作的原因 服务定义-我只想发布、序列化到LogDeviceCommunication对象中,然后作为一个简单的测试返回该对象wcf序列化,wcf,entity-framework,xml-serialization,Wcf,Entity Framework,Xml Serialization,WCF新手,需要帮助了解序列化无法正常工作的原因 服务定义-我只想发布、序列化到LogDeviceCommunication对象中,然后作为一个简单的测试返回该对象 [OperationContract] [WebInvoke(UriTemplate = "AddDeviceCommunicationLog", RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")
[OperationContract]
[WebInvoke(UriTemplate = "AddDeviceCommunicationLog", RequestFormat =
WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
LogDeviceCommunication AddDeviceCommunicationLog(LogDeviceCommunication
deviceCommunicationEntry);
public LogDeviceCommunication AddDeviceCommunicationLog(LogDeviceCommunication
deviceCommunicationEntry)
{
return deviceCommunicationEntry;
}
目前,我只是用Fiddler发布以下XML作为测试
<?xml version="1.0" encoding="UTF-8"?>
<LogDeviceCommunication>
<ID>1207a26e-ab59-4977-b7eb-b2776205cffe</ID>
<DeviceID>A42E8707-7C65-45AA-8E58-5D21F53DA101</DeviceID>
<Time>2012-03-14T15:38:28.379Z</Time>
<Line>0</Line>
<Tab>0</Tab>
<Info>Starting Synchronisation</Info>
</LogDeviceCommunication>
1207a26e-ab59-4977-b7eb-b2776205cffe
A42E8707-7C65-45AA-8E58-5D21F53DA101
2012-03-14T15:38:28.379Z
0
0
起动同步
从Fiddler返回的结果
<LogDeviceCommunication z:Id="i1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<ChangeTracker z:Id="i2"
xmlns:a="http://schemas.datacontract.org/2004/07/conxEntities">
<a:ExtendedProperties/>
<a:ObjectsAddedToCollectionProperties/>
<a:ObjectsRemovedFromCollectionProperties/>
<a:OriginalValues/>
<a:State>Added</a:State>
</ChangeTracker>
<DeviceID>00000000-0000-0000-0000-000000000000</DeviceID>
<ID>1207a26e-ab59-4977-b7eb-b2776205cffe</ID>
<Info i:nil="true"/>
<Line i:nil="true"/>
<Tab i:nil="true"/>
<Time>2012-03-14T15:38:28.379Z</Time>
</LogDeviceCommunication>
补充
00000000-0000-0000-0000-000000000000
1207a26e-ab59-4977-b7eb-b2776205cffe
2012-03-14T15:38:28.379Z
为什么DeviceID包含0000(我假设它是空Guid),而ID包含正确的Guid;另外,为什么Info、Line和Info元素包含nil值?
LogDeviceCommunication是使用ADO.NET自跟踪模板从EF4生成的POCO
浓缩版是
[DataContract(IsReference = true, Namespace = "")]
public partial class LogDeviceCommunication: IObjectWithChangeTracker,
INotifyPropertyChanged
[DataMember]
public System.Guid DeviceID
[DataMember]
public System.DateTime Time
[DataMember]
public Nullable<int> Line
[DataMember]
public Nullable<int> Tab
[DataMember]
public string Info
[DataMember]
public System.Guid ID
[DataContract(IsReference=true,Namespace=”“)]
公共部分类LogDevice通信:IObjectWithChangeTracker,
InotifyProperty已更改
[数据成员]
公共系统。Guid设备ID
[数据成员]
公共系统日期时间
[数据成员]
公共可空线路
[数据成员]
公共可空选项卡
[数据成员]
公共字符串信息
[数据成员]
公共系统。Guid ID
我确信我做了一些不正确的事情,因此感谢您的帮助。当WCF收到您的请求时,它的反序列化机制将创建一个新的
LogDeviceCommunication
类型实例,以填充它收到的值。您的实例的EF分部类中的代码似乎正在被触发,它会导致您在问题中发布的内容
尝试在AddDeviceCommunicationLog
方法中的return语句上设置调试器断点,以查看为您反序列化的EF&WCF。如果它和你发布的一样,那么这个问题很可能是由EF管道代码引起的。此外,您可能希望启用以查看WCF实际接收和发送的内容
编辑:刚刚跑过,显示了EF和WCF之间的一些交互。您可能想查看它,看看它是否适用于您的问题。我打赌该模板生成的类的其他部分包括您看到的元素
一般来说,从web服务返回EF实体(或任何复杂的.NET类型)不是一个好主意-它们会拖拽实现依赖项。将一个纯POCO类作为DTO返回。问题在于XML所需的顺序
我认为您遗漏了一些相关代码。ChangeTracker是如何初始化的?(你可能还想用EF4来标记你的问题,以引起EF人员的注意,因为我怀疑他们会有一些建议…)