The InnerException message was "Type named data contract when passed to service W

I had a WCF service with an operational 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 IQuery implementation 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 try 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 like

An error occurred while trying to serialize the parameter. The InnerException message was' Type 'ViewModels.Query' with data contract name 'Request: http://schemas.datacontract.org/2004/07/ViewModels ' is not expected. Consider using a DataContractResolver, or add any types that are not statically known to the list of known types — for example, using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the DataContractSerializer. '. See InnerException for more information.

Can anyone suggest me what the resolution would be for this error?

+3


source to share


1 answer


Let's take a look at serializing concrete classes. You cannot serialize interfaces. Refer to this answer: fooobar.com/questions/576252 / ...

Some useful links I found from bing:

http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

The article above covers your situation and use cases [KnownType]

for your derived classes

Update:

Based on this link , please check the following update:

public interface IQuery 
{ 
    Guid Id { get; set; }
    string QueryNo{ get; set; } 
    string Status { get; set; } 
    IData data {get; set;}
}

      



Query class using a data serializer that implements your interface IQuery

[DataContract]
public class Query : IQuery
{
    [DataMember]
    public Guid Id { get; set; };
    [DataMember]
    public string QueryNo { get; set; };
    [DataMember]
    public string Status { get; set; };
    [DataMember]
    public Data data { get; set; }; //Make sure you serialize Data class as well
}

      

And for your service contract:

[ServiceContract]
public interface IMyQueryService
{
      [OperationContract]
      [ServiceKnownType(typeof(Query))]
      void AddQuery(IQuery query);
}

      

[ServiceKnownType(typeof(Query))]

will allow your operating contract to allow entry Query

. Also note that you need to specify all of your implementations IQuery

, which should be passed as parameters to your contract to work with the attribute ServiceKnownType

.

Also, if you want more than one (or all) of the operating contract to accept them as parameters, specify the attribute ServiceKnownType

for ServiceContract

instead of each individual transaction contract.

Hope this helps!

+4


source







All Articles