Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting this WCF Error Message?

I am getting the error below when I call my WCF service. What am I missing here?

'System.String[]' with data contract name
'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays'
is not expected. Add any types not known statically to the list of known
types - for example, by using the KnownTypeAttribute attribute or by adding
them to the list of known types passed to DataContractSerializer.'.  Please
see InnerException for more details.

{"There was an error while trying to serialize parameter
http://tempuri.org/:myEntity. The InnerException message was
'Type 'System.String[]' with data contract name
'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays'
is not expected. Add any types not known statically to the list of known
types - for example, by using the KnownTypeAttribute attribute or by adding
them to the list of known types passed to DataContractSerializer.'.  

Please see InnerException for more details."}
like image 963
Steve Chapman Avatar asked Apr 29 '09 00:04

Steve Chapman


Video Answer


2 Answers

From what I gather, you have a WCF function that has a parameter named 'myEntity'. I'm assuming that the type of myEntity is a user-defined class and is adorned with the DataContract attribute, as it should be. I'm also assuming that the type of myEntity has a member field that is a string array. Let's assume that all of this is true (again, it'd be really helpful if you could post your code).

Ordinarily, string arrays, i.e., string[], will serialize just fine. But, in some cases (see here and here), you may have to add it to the list of known types in order for WCF to serialize everything correctly.

To do this, add the following:

[DataContract]
[KnownType(typeof(string[]))]
public class YourClassNameHere
{
}
like image 183
Matt Davis Avatar answered Dec 23 '22 05:12

Matt Davis


You haven't posted the code, so my answer is based on the assumption that you have a class myEntity which you are trying to serialize. Try using a KnownTypeAttribute for the class

e.g.

[KnownType(typeof(myEntity))]

You can refer to the following MSDN link: KnownTypeAttribute

like image 31
SO User Avatar answered Dec 23 '22 05:12

SO User