RcMan
RcMan

Reputation: 913

WCF Serialization exception on OperationContract with params object[] which contains an array

For the example: My Service contract

[ServiceContract]
public interface IProvider
{
    [OperationContract]
    DataSet CreateDataSetFromSQL(string command, params object[] par);
}

Evrything works fine until one of the params is an Array/List/ArrayList. I get a serialization exception:

data contract name 'ArrayOfanyType: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.

As I mentiond I get the same error for string array when its one of the parameters.

Client

    private static ChannelFactory<IProvider> _channel;
    private static IProvider _proxy;
    private static DataTransferClient _client;

    public DataSet CreateDataSetFromSQL(string commandCode, params object[] par)
    { 
       return _proxy.CreateDataSetFromSQL(commandCode, par);
    }

Any idea how to workaround it ?

Upvotes: 1

Views: 2078

Answers (1)

nvoigt
nvoigt

Reputation: 77285

Just in case you did not actually read the error message:

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.

As your type is "object", anything that is not "just an object" is not known statically and needs to be added via KnownType-Attribute. If you want to pass a List<Whatever>, you need to put the KnownType attribute with the type of List<Whatever> on top of your service.

As you did not post your service but only your interface, you can also use the ServiceKnownType attribute on your interface instead:

[ServiceContract]
[ServiceKnownType(typeof(List<string>))] // <== this will enable the serializer to send and receive List<string> objects
public interface IProvider
{
    [OperationContract]
    DataSet CreateDataSetFromSQL(string command, params object[] par);
}

Upvotes: 2

Related Questions