I had a WCF Service with an operation contract as
void AddQuery(IQuery Query);
My IQuery is like this
public interface IQuery
{
Guid Id { get; set; }
string QueryNo { get; set; }
string Status { get; set; }
IData data { get; set; }
}
and the implementation of IQuery is in
[Serializable]
public class Query : IQuery
{
Guid Id { get; set; }
string QueryNo { get; set; }
string Status { get; set; }
IData data { get; set; }
}
When i am trying to send my object from client as
public void AddQuery(IQuery query)
{
try
{
// I am sure that the query object is not null and it is implemented
objServiceClient.AddEnquiry(query);
}
catch (Exception ex)
{
}
}
But i am getting an exception as
There was an error while trying to serialize parameter . The InnerException message was ‘Type ‘ViewModels.Query’ with data contract name ‘Query:http://schemas.datacontract.org/2004/07/ViewModels‘ is not expected. Consider using a DataContractResolver or 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.
Could anyone suggest me what will be the resolution for this error?
Consider serializing concrete classes. You cannot serialize interfaces . Refer to this answer: https://stackoverflow.com/a/4659289/860243
Some useful links I found from bing:
http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/
The above article discusses your situation and examples of using
[KnownType]for your derived classesUpdate:
Based on this link, please check the below update:
Your Query class using Data contract serializer implementing your interface
IQueryAnd for your service contract:
The
[ServiceKnownType(typeof(Query))]will enable your operation contract to allowQueryas input. Also please note you need to specify all yourIQueryimplementations that needs to be passed as parameters to your Operation contract withServiceKnownTypeattribute .Also if you want more than one (or all) operation contract to take them as parameters, specify the
ServiceKnownTypeattribute forServiceContractinstead of each operation contract separately.Hope this helps!