我有以下WCF服务
namespace WcfServiceLibrary1
{
[ServiceKnownType(typeof(MyClass))]
[ServiceContract]
public interface IService1
{
[OperationContract]
Table GetData();
}
}
namespace WcfServiceLibrary1
{
public class Service1 : IService1
{
public Table GetData()
{
var table = new Table();
var record = new Record { };
record.Add("record1",new MyClass { Name ="test"});
table.Records.Add(record);
return table;
}
}
}
WCF引用其他程序集中的类库:
namespace ClassLibrary1
{
[DataContract]
[KnownType(typeof(MyClass))]
public class Table
{
public Table()
{
Records = new List<Record>();
}
[DataMember]
public List<Record> Records { get; set; }
}
[KnownType(typeof(MyClass))]
[DataContract]
public class MyClass
{
[DataMember]
public string Name { get; set; }
}
[CollectionDataContract]
public class Record : IDictionary<string, object>
{
private IDictionary<string, object> innerDictionnary = new Dictionary<string, object>();
public void Add(string key, object value)
{
innerDictionnary.Add(key, value);
}
// … here the rest of IDictionary<string, object> Implementation
}
在另一个项目中,我引用了WCF服务,并调用了GetDate方法:
[TestMethod]
public void TestMethod1()
{
Service1Client client = new Service1Client();
var x = client.GetData();
}
我有这个错误:
http // schemas.datacontract.org / 2004/07 / ClassLibrary1:Value'包含 来自映射到名称的类型的数据 'HTTP // schemas.datacontract.org / 2004/07 / ClassLibrary1的:MyClass的'。该 反序列化器不知道映射到此名称的任何类型。 考虑使用DataContractResolver或添加对应的类型 'MyClass'到已知类型列表 - 例如,通过使用 KnownTypeAttribute属性或通过将其添加到已知列表中 传递给DataContractSerializer的类型。'。
我在其他发布的问题中尝试了很多解决方案,但没有任何效果