1

例如:我的服务合同

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

在参数之一是 Array/List/ArrayList 之前,一切都可以正常工作。我得到一个序列化异常:

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.

正如我所提到的,当字符串数组作为参数之一时,我得到了同样的错误。

客户

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

知道如何解决它吗?

4

1 回答 1

2

以防您实际上没有阅读错误消息:

将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。

由于您的类型是“对象”,因此任何不是“只是对象”的东西都不是静态已知的,需要通过 KnownType-Attribute 添加。如果要传递 a List<Whatever>,则需要将KnownType类型为 的属性List<Whatever>放在服务之上。

由于您没有发布服务,而只是发布了您的interface,因此您也可以在您的 interface 上使用ServiceKnownType属性:

[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);
}
于 2013-09-23T09:28:37.607 回答