Introduction
In my
last post
I explained that System.Data contains a bug which can cause it to throw an exception during serialisation or
deserialisation of a DataSet which contains a column that uses System.Object as its .NET type.
The bug manifests itself in two known scenarios:
- Serialising a DataSet containing a column of type System.Object using a System.Xml.XmlTextWriter instance
created via the static Create method; this throws a System.ArgumentException.
- Deserialising a DataSet containing a column of type System.Object after having successfully passed it
across the wire via Windows Communication Foundation (WCF) using netTcp binding; this throws a
System.Data.DataException.
This post covers the second of the two scenarios – deserialising a DataSet which has been successfully
transmitted via WCF's netTcp binding.
System.Data.DataException
A couple of years ago I was asked by a client to port an existing ASMX-based Web Service to one using Windows
Communication Foundation. The brief was to change as little of the client and service as possible, and to just
replace the communications interface. In addition to passing simple and complex types across the wire, the
application would often pass both typed and untyped DataSets (clearly a bad idea, but that's another story).
One of the typed DataSets was based upon a SQL Server 2005 table containing a column of type SQL_VARIANT.
Attempting to pass an instance of this DataSet from server to client produced the following exception on the
client (i.e. during deserialisation):
System.Data.DataException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Undefined data type: 'xs:int'.
The XML fragment below represents an instance of the SQL_VARIANT column within the serialized typed DataSet.
Note the use of the xsi:type="xs:int" attribute to define the data type of this column used for this
particular row: although the column can contain data of any type, a given row must explicitly define which
data type is being used.
<Data xsi:type="xs:int" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1</Data>
Note: Much of the investigation detailed herein was carried out with the help of
.NET Reflector
to study what some of the classes in the .NET Framework were actually doing.