ProtocolException while transfering ArrayList to WCF service

Today I created a method in my WCF service that would process an ArrayList.
There was no possibility to pass a generic list due to interoperability reasons.

In this ArrayList I would place objects of a certain type. This would then be processed server side.

When calling this simple method my client threw a ProtocolException with the following message:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:handlingunits. The InnerException message was ‘Error in line 1 position 342. Element ‘http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType’ contains data of the ‘http://schemas.mshelp.com/HandlingUnit:HandlingUnit’ data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to ‘HandlingUnit’ to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.’.  Please see InnerException for more details.

The problem here is that WCF does not know how to serialize the type of the object that is contained within the ArrayList.
The solution to this is actually very simple, you just need to make the type known to your service.

You can do this by using the ServiceKnownType attribute. You need to place this above your service interface. This looks like this:

   1: [ServiceContract]
   2: [ServiceKnownType(typeof(HandlingUnit))]
   3: public interface IMyWCFService
   4: {
   5:     //Your operations
   6: }

Simple way to generate XSD from XML via Visual Studio

Today we had to generate an XSD file from an XML file.
Of course you can use the XML Schema Definition tool (XSD.exe) or something 3rd party (XMLSpy: Best XML editor ever made IMHO) for this.
But there is also an easy way to do this via Visual Studio.

When you open an XML file in Visual Studio a new menu item (XML) will appear.
From this menu you can do the following:

  • Create schema
  • Show XSLT Output
  • Debug XSLT
  • View available Schemas

image

Feel a little stupid that I didn’t notice this earlier. Apparently this is something that
has been there for a while.

But anyway, it’s a nice feature.