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: }

No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word