Search code examples
odatawcf-data-servicesasp.net-web-apiasp.net-web-api-odata

SingleResult<T> not serializable in Web API when querying by key


Trying to find single record using primary key CourseID against odata web.api using this:

var editedcourse = container.Courses.Where(c => c.CourseID == ID).SingleOrDefault();

This is error:

    <m:innererror>
    <m:message>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/atom+xml; charset=utf-8'.</m:message>
    <m:type>System.InvalidOperationException</m:type>
    <m:stacktrace></m:stacktrace>
    <m:internalexception>
      <m:message>'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter.</m:message>
      <m:type>System.Runtime.Serialization.SerializationException</m:type>

Solution

  • The web.api controller method by default was not queriable, thus client failed. Added annotation to fix: [Queryable(AllowedOrderByProperties = "Id")]