0

FooBars のリストを返す次の設定があります。しかし、何らかの理由でシリアル化に失敗します:(

設定

ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.Function("Test").ReturnsCollection<FooBar>();
config.MapODataServiceRoute(routeName: "OData",
    routePrefix: "OData",
    model: builder.GetEdmModel());

バインドされていない関数

[HttpGet]
[ODataRoute("Test")]
public IEnumerable<FooBar> Test()
{
    var foobars = new List<FooBar>();
    // populate list...
    return foobars;
}

エラー

localhost/odata/Testを呼び出すと、以下のエラーが発生します

{
    "error": {
        "code": "",
        "message": "An error has occurred.",
        "innererror": {
            "message": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.",
            "type": "System.InvalidOperationException",
            "stacktrace": "",
            "internalexception": {
                "message": "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.",
                "type": "System.Runtime.Serialization.SerializationException",
                "stacktrace": "   at System.Web.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\r\n   at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
            }
        }
    }
}

$Metadata スニペット

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" 
    xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
    <edmx:DataServices>
        <Schema Namespace="MyNamespace" 
            xmlns="http://docs.oasis-open.org/odata/ns/edm">
            <EntityType Name="FooBar">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.Int32" Nullable="false" />
                <Property Name="A" Type="Edm.String" />
                <Property Name="B" Type="Edm.Int32" Nullable="false" />
                <Property Name="C" Type="Edm.Int32" Nullable="false" />
            </EntityType>
            <EntityType Name="FooBarD" BaseType="MyNamespace.FooBar">
                <Property Name="D" Type="Edm.Int32" Nullable="false" />
            </EntityType>
            <EntityType Name="FooBarE" BaseType="MyNamespace.FooBar">
                <Property Name="E" Type="Edm.Int32" Nullable="false" />
            </EntityType>
        </Schema>
        <Schema Namespace="Default" 
            xmlns="http://docs.oasis-open.org/odata/ns/edm">
            <Function Name="Test">
                <ReturnType Type="Collection(MyNamespace.FooBar)" />
            </Function>
            <EntityContainer Name="Container">
                <FunctionImport Name="Test" Function="Default.Test" IncludeInServiceDocument="true" />
            </EntityContainer>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>
4

1 に答える 1

3

builder.EntitySet<FooBar>("BAM");このセットを持っていたので、ReturnsCollectionFromEntitySet代わりに電話する必要がありました。

ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<FooBar>("BAM"); // previously omitted from example
builder.Function("Test").ReturnsCollectionFromEntitySet<FooBar>("BAM")();
config.MapODataServiceRoute(routeName: "OData",
    routePrefix: "OData",
    model: builder.GetEdmModel());
于 2015-03-20T15:17:35.497 に答える