使用Expand on List属性:参数必须是'IEnumerable'类型。的OData

时间:2013-05-12 09:01:41

标签: serialization asp.net-web-api odata

我使用Asp.Net Web Api和OData(5.5.0-alpha2)。 我有一个模特:

public class Model
{
    public List<Event> Events { get; set; }
    public List<User> Subscribers { get; set; }
    public User Owner { get; set; }
    ....
}

控制器中的动作:

[Queryable(AllowedQueryOptions = AllowedQueryOptions.All, MaxAnyAllExpressionDepth = 10, MaxExpansionDepth = 10)]
public override IQueryable<EventsChannel> Get()
{
    return Repository.Models.AsQueryable();
}

Repository.Models为我提供了包含属性的所有模型。

当我尝试使用所有者的扩展来获取数据时:

http://site.com/odata/Models()?$expand=Owner

一切都很好。但是,如果我尝试获取事件:

http://site.com/odata/Models()?$expand=Events

它引发了我异常:

The argument must be of type 'IEnumerable'.&#xD; Parameter name: graph

我无法将List更改为IEnumerable,为什么当List为IEnumerable时它会告诉我这个?

<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <m:code />
  <m:message xml:lang="en-US">An error has occurred.</m:message>
  <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>The argument must be of type 'IEnumerable'.&#xD;
Parameter name: graph</m:message>
      <m:type>System.ArgumentException</m:type>
      <m:stacktrace>   at System.Web.Http.OData.Formatter.Serialization.ODataCollectionSerializer.CreateODataValue(Object graph, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEdmTypeSerializer.CreateProperty(Object graph, String elementName, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.CreateStructuralProperty(IEdmStructuralProperty structuralProperty, EntityInstanceContext entityInstanceContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.CreateStructuralPropertyBag(IEnumerable`1 structuralProperties, EntityInstanceContext entityInstanceContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteEntry(Object graph, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteObjectInline(Object graph, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteFeed(IEnumerable enumerable, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObjectInline(Object graph, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteExpandedNavigationProperty(KeyValuePair`2 navigationPropertyToExpand, EntityInstanceContext entityInstanceContext, ODataWriter writer)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteExpandedNavigationProperties(IDictionary`2 navigationPropertiesToExpand, EntityInstanceContext entityInstanceContext, ODataWriter writer)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteEntry(Object graph, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteObjectInline(Object graph, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteFeed(IEnumerable enumerable, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObjectInline(Object graph, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)&#xD;
   at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.&lt;&gt;c__DisplayClassa.&lt;WriteToStreamAsync&gt;b__9()&#xD;
   at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)&#xD;
--- End of stack trace from previous location where exception was thrown ---&#xD;
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)&#xD;
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&#xD;
   at System.Web.Http.WebHost.HttpControllerHandler.&lt;WriteBufferedResponseContentAsync&gt;d__10.MoveNext()</m:stacktrace>
    </m:internalexception>
  </m:innererror>
</m:error>

0 个答案:

没有答案