I have the following WCF Service
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;
}
}
}
The WCF reference a Class library in other assembly :
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
}
In another project I referenced the WCF Service and I called the GetDate Method :
[TestMethod]
public void TestMethod1()
{
Service1Client client = new Service1Client();
var x = client.GetData();
}
and I have this error:
http//schemas.datacontract.org/2004/07/ClassLibrary1:Value' contains data from a type that maps to the name 'http//schemas.datacontract.org/2004/07/ClassLibrary1:MyClass'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'MyClass' 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 DataContractSerializer.'.
I tried many solutions found in other posted question, but nothing works