我有一个返回数组Dictionary []的服务。现在,我使用该服务返回MongoDb查询的结果和行的ID(ObjectId)。然后它抛出UnknownType的异常:
System.Runtime.Serialization.SerializationException: Error in line 1 position 268. Element 'http://tempuri.org/:GetDataResult' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/MongoDB.Bson:ObjectId'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver if you are using DataContractSerializer or add the type corresponding to 'ObjectId' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to the serializer.
我已将ServiceKnownType属性放在OperationContract上,但它没有用。如果我将ServiceKnownType属性直接放在“Reference.cs”上生成它可以工作,但我不想手动修改生成的ServiceReference,有没有办法在客户端ServiceReference上自动传播“ServiceKnownType”?
(我正在使用VS2010和Framework 4.0)
要测试的最小代码:
[SERVER]
[ServiceContract]
public interface IService1
{
[OperationContract]
[ServiceKnownType(typeof(MongoDB.Bson.ObjectId))]
List<Dictionary<string, object>> GetData(int value);
}
[CLIENT]
[TestMethod]
public void TestMethod1()
{
var client = new ServiceReference1.Service1Client();
var data = client.GetData(1);
Assert.IsNotNull(data);
}