Search code examples
c#asp.netodatat4odata-v4

Bound function call returns InvalidOperationException


I'm following this OData V4 tutorial and now have a problem with the bound function MostExpensive.

This function is bound to the Productscollection and is registered in WebApiConfig.Register() like suggested in the tutorial:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = "ProductService";
builder.EntityType<Product>().Collection.Function("MostExpensive").Returns<decimal>();

There is only described a rudimentary client for this service in part 2 of this tutorial. So I want to extend it so that I also can call the functions described in the later parts of the tutorial.

I have created the client using OData Client Code Generator as suggested in the tutorial. I then add some products and suppliers and then want to get the most expensive product:

static void Main(string[] args)
{
    const string serviceUri = "http://localhost:52542";
    Container container = new Container(new Uri(serviceUri));

    AddProducts(container, GenerateSomeProducts());
    AddSuppliers(container, GenerateSomeSuppliers());

    Console.WriteLine("Most expensive product is: {0}", container.Products.MostExpensive().GetValue());

    ...
}

When calling GetValue() I am getting an InvalidOperationException stating that http://localhost:52542/$metadata refers to a Edm.Decimal type but a Collection(Edm-Decimal) type is expected.

When calling http://localhost:52542/Products/ProductService.MostExpensive() directly in the browser I'm getting

{
    "@odata.context":"http://localhost:52542/$metadata#Edm.Decimal","value":40000.95
}

Which seems to be correct.

Do I do anything wrong? I have no idea how to fix this. So any suggestions about that?


Solution

  • I guess you are using T4 2.2.0, right?

    There is a bug in T4 2.2.0 which causes this issue. You can use the content in following link to replace your ttinclude file and regenerate your proxy to work around the issue.

    https://raw.githubusercontent.com/LaylaLiu/odata.net/T4TempFix/src/CodeGen/ODataT4CodeGenerator.ttinclude