torsdag 29 november 2007

DataContractSerializer requires alphabetic element order

When using the DataContractSerializer to deserialize objects either from pure XML or implicit using WCF it requires the XML elements to appear in alphatic order.

Let says we have the following class:
[DataContract]public class auth
{
[DataMember]
public string username;
[DataMember]
public string password;
}

...and the following XML input:
<auth>
<username>ABC</username>
<password>DEF</password>
</auth>

Using this XML, the DataContractSerializer would deserialize the "Auth" class and Username property correctly - but the Password property would be null.

The reason is that the DataContractSerializer requires the element order in the XML to be in alphabetic order, ie:
<auth>
<password>DEF</password>
<username>ABC</username>
</auth>


The sort order can be set explicitly using the "Order" property on the "DataMember" attribute - however: it would still require the XML input to follow a specific order.
Example using the "Order" property and allowing the username element to appear before the password element:
[DataContract]
public class auth
{
[DataMember(Order=1)]
public string username;
[DataMember(Order=2)]
public string password;
}



WCF: Adding a behavior extension requires careful notation of the assembly path

When registering a behavior extension in the .config file the inialization might fail with the following error message:

An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element 'myService' cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.Parameter name: element

This might be the case even if the syntax for the assembly/class is correct and even if the service host has access to the assembly.

The reason is that for this particular configuration, .NET requires you to:
1. Add the fully qualified name for the assembly
Example:
"MyAssembly.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
instead of:
"MyAssembly.MyClass, MyAssembly" - which normally does the job

2. Use the exact syntax for the assembly
You are not allowed to use any line break of white spaces at the wrong places.
Example:
"MyAssembly.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
instead of:
"MyAssembly.MyClass,MyAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" - note the missing white spaces between each attribute.

Microsoft has acknowledged this; yet not classified it as a bug:
https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=216431