例如: 我的服务合同
[ServiceContract]
public interface IProvider
{
[OperationContract]
DataSet CreateDataSetFromSQL(string command, params object[] par);
}
Evrything正常工作,直到其中一个参数是一个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);
}
知道如何解决它吗?
答案 0 :(得分:2)
以防您实际上没有阅读错误消息:
将任何静态未知的类型添加到已知类型列表中 - 例如,使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。
由于您的类型是“对象”,因此不是“仅仅是对象”的任何内容都不是静态知道的,需要通过KnownType-Attribute添加。如果您想传递List<Whatever>
,则需要在服务之上添加KnownType
类型List<Whatever>
。
由于您没有发布服务而只发布界面,因此您也可以使用界面上的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);
}