Jimmy
Jimmy

Reputation: 2106

WCF Serialization error with "ArrayOfstring" Data Contract

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

Upvotes: 0

Views: 5740

Answers (4)

Gihan Senanayake
Gihan Senanayake

Reputation: 21

I too had the same issues but after qualifiying the OperationContract with [ServiceKnownType(typeof(string[]))] and [ServiceKnownType(typeof(int[]))] fixed the issue.

Eg:

[ServiceContract]
    public interface IReportService
    {
        [OperationContract]
        [ServiceKnownType(typeof(string[]))]
        [ServiceKnownType(typeof(int[]))]
        bool GenerateReport(int clientId, int masterId, string reportType, int[] vtIds, DateTime initialDate, DateTime finalDate,
                            bool descending, string userName, string timeZoneId, bool embedMap,
                            object[] vtExtraParameters, object[] vtScheduleParameters, string selectedCriteria,
                            out long reportID, out int scheduleID, out string message);

Upvotes: 2

KenL
KenL

Reputation: 875

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();

Upvotes: 2

Tanner
Tanner

Reputation: 22753

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?

Upvotes: 0

Steve
Steve

Reputation: 12034

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?

Upvotes: 0

Related Questions