Search code examples
functionodataasp.net-web-api2

webAPI OData v4 Function Registration


Having setup my WebAPI OData endpoint by doing this ...

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

I then tried adding a function as per the "Example:Adding a function" section at the bottom of this ...

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions

I hit a bit of a problem as the method call "builder.EntityType()" does not exist but everything else is up and running fine.

Is there some special dependency I don't know about? I'm using version 5.6.0 (latest stable) of the OData v4 package from nuget and compiling against .Net 4.6.

EDIT: I tried updating the reference to the 5.7.0-rc build of the OData package, but that did solve it either, here's the bit we care about from my packages.config ...

<packages>
  <package id="EntityFramework" version="6.1.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.OData" version="5.7.0-rc" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Cors" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net46" />
  <package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net46" />
  <package id="Microsoft.OData.Core" version="6.13.0" targetFramework="net46" />
  <package id="Microsoft.OData.Edm" version="6.13.0" targetFramework="net46" />
  <package id="Microsoft.Spatial" version="6.13.0" targetFramework="net46" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net46" />
</packages>

I'm using a few other things like ninject to do some DI stuff but I have not included those here to keep the question short.


Solution

  • Ok it turns out for some reason there was version 5.5.1.0 of the package in my bin folder which it was choosing to pick up despite an assembly binding to tell it to use the later 5.7.0.0 version.

    Also to really mess with my head Microsoft have moved a bunch of stuff around and some using statements had to be changed in my class (possibly why my code was using an older version) ...

    Old usings:

    using Microsoft.Data.Edm;
    using System.Web.Http.OData.Builder;
    

    New usings:

    using Microsoft.OData.Edm;
    using System.Web.OData.Builder;
    using System.Web.OData.Extensions;
    

    It's probably worth noting that as the walkthrough makes no mention of usings or version numbers, it just says "we used OData v4" ... helpful as ever!

    I had to dig through the source on codeplex to find the answer but essentially that walkthrough is based on 5.7.0.0 or later of the package.