I am trying to pass a class with a Dictionary property over WCF and it is failing for one method but works for another. When the class is returned inside a List
, it works. But when the class is returned inside a DataTable
, the client just says the connection was disconnected and no error shows up.
Here is the class causing issues:
[DataContract]
public class DetailLog
{
[DataMember]
public string Name
{
get;
set;
}
[DataMember]
public string SubAction
{
get;
set;
}
[DataMember]
public Dictionary<string, string> Fields
{
get;
set;
}
[DataMember]
public string UserName
{
get;
set;
}
}
I started out creating a method that works without issue:
public List<DetailLog> GetDetailLog(List<int> IDs, List<int> actionTypeIds, List<int> userIds, DateTime beginDate, DateTime endDate)
Then we needed to create some very dynamic reports so I used a DataTable which we have used previously for other dynamic reports.
But I needed to pass along the DetailLog class so I created a DataTable column of that type:
public DataTable GetCustomDetailReport(int CustomReportID, List<CustomReportFilter> reportFilters)
{
DataTable data = new DataTable();
...
data.Columns.Add("DetailLog", typeof(DetailLog));
...
}
This method would would exit fine on the WCF host side but the client side would error about the connection being lost. I tried adding ServiceKnownType for the OperationContract in the interface but it did not fix it:
[OperationContract]
[ServiceKnownType(typeof(DetailLog))]
DataTable GetCustomUserAuditReport(int CustomReportID, List<CustomReportFilter> reportFilters);
I cannot really debug the serialization when the method returns the DataTable so I added this code to the end of the GetCustomDetailReport() to catch the error.
DataContractSerializer ser = new DataContractSerializer(typeof(DataTable), new List<Type> { typeof(DetailLog) });
ser.WriteObject(Stream.Null, data);
When I did, I saw an exception
Cannot serialize member ... of type System.Collections.Generic.Dictionary`2 because it implements IDictionary.