0

Fairly new to WCF and need help with understanding why serialisation is not working correctly.

Service definition - I just want to post, serialise into a LogDeviceCommunication object and then just return the object as a simple test

[OperationContract]
[WebInvoke(UriTemplate = "AddDeviceCommunicationLog", RequestFormat = 
WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
LogDeviceCommunication AddDeviceCommunicationLog(LogDeviceCommunication
deviceCommunicationEntry);

public LogDeviceCommunication AddDeviceCommunicationLog(LogDeviceCommunication 
deviceCommunicationEntry)
   {
   return deviceCommunicationEntry;
   }

At the moment I am just posting the following XML with Fiddler as a test.

<?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>

Results returned from 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>

Why does the DeviceID contain the 0000's (I assume it's a null Guid) while the ID contains the correct Guid; also why do the Info, Line and Info elements contain nil values? The LogDeviceCommunication is a POCO generated from EF4 using the ADO.NET Self Tracking Template

Condensed version is

[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

I am sure I am doing something incorrectly so any help appreciated.

Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
Kern
  • 447
  • 1
  • 6
  • 16
  • 1
    I think you've left out some relevant code. How is ChangeTracker being initialised? (You also might want to tag your question with EF4 to get some attention from the EF guys as I suspect they will have some pointers...) – Phil Degenhardt Mar 15 '12 at 11:20

3 Answers3

1

The problem lies in the required ordering of the XML.

WCF Datacontract, some fields do not deserialize

http://neimke.blogspot.co.nz/2012/03/serialization-ordering-causes-problems.html

Community
  • 1
  • 1
Kern
  • 447
  • 1
  • 6
  • 16
0

When WCF receives your request, its deserialization machinery will create a new instance of the LogDeviceCommunication type to populate with the values it receives. It seems that the code from the EF partial class of your instance is being triggered and it results in what you post in your question.

Try setting a debugger break point on the return statement in your AddDeviceCommunicationLog method to see what EF & WCF deserialized for you. If it's just as what you posted then the issue is likely caused by the EF plumbing code. Also, you may want to enable WCF message tracing to see what WCF is actually receiving and sending back.

EDIT: Just ran across this blog post that shows some of the interaction between EF & WCF. You may want to review it to see if it's applicable to your issue.

Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
0

I bet the other parts of the class, generated by that template, include the elements you're seeing.

In general, it's not a good idea to return EF entities (or any complex .NET type) from a web service - they drag along implementation dependencies. Return a purely POCO class as a DTO instead.

John Saunders
  • 160,644
  • 26
  • 247
  • 397