views:

607

answers:

3

Am getting an error mentioned below when I call my WCF service?How do i get rid of it?

There was an error while trying to serialize parameter http://tempuri.org/:MyWCFSvc.svc 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.*

I tried using [ServiceKnownType(typeof(string[]))] in my WCF service interface but no luck

A: 

There's no reason for you to have to KnownType an array of strings. The serializer should already know about that, and arrays are not a problem. I'm moving Lists of things around in WCF without an issue. Could you post a representative sample of what you're doing?

Steve
A: 

Configuring service references on your client provides "Data Type" options that allow you to specify different types for Collection/Dictionary Types. What settings do you have in there?

Tanner
+2  A: 

A year late, but I had the same issue and here is what you need to do

List<SomeClass> mylist = new List<SomeClass>();

DataContractSerializer dcs = new DataContractSerializer(mylist.GetType());
XmlWriter writer = XmlWriter.Create(sb, XWS);
dcs.WriteObject(writer, query);      
writer.Close();

The problem is when you construct your serializer with the typeof your class, the serialzer does not see it as an arrray, it only sees a single object.

If found it by doing this first:

DataContractSerializer dcs = new DataContractSerializer(SomeClass.GetType());
XmlWriter writer = XmlWriter.Create(sb, XWS);
dcs.WriteObject(writer, query[0]);  // Only get the first record from linq to sql
writer.Close();
KenL
@KenL: thanks for answering, but since the OP never posted the code he was using, how do you know that your solution would have solved his problem? All you know is that you received a similar exception. It could have been caused by something quite different.
John Saunders