With the official RTM release of ASP .NET Web API, Microsoft took away the inbuilt support for OData that had been present on the earlier beta and release candidates. The official message is that there will be a far more complete release in the future, which will include additional query terms like the much-requested support for $select, $expand and $inlinecount.
In the interim there is a NuGet package that lets you bring back the
[Queryable] attribute, which is quite neat indeed, as it's all a matter of
PM> Install-Package Microsoft.AspNet.WebApi.OData -Pre
Nevertheless, this package has not been updated since it was originally released last August. Ultimately meaning that it misses plenty of enhancements and bugfixes that have been released since then.
For instance, in my case, this is the output that I was receiving with a Web API controller whenever I used an OData query.
<Message>An error has occurred.</Message> <ExceptionMessage>An item with the same key has already been added.</ExceptionMessage> <ExceptionType>System.ArgumentException</ExceptionType>
The resolution of this issue requires the removal of the Web API OData 0.1.0-alpha-120815 package and importing the OData dll from a daily build of the ASP .NET Web Stack.
Here's the step-by-step process that I've followed:
First of all, uninstall the Microsoft.AspNet.WebApi.OData package if you already had it installed
Then ensure that the package manager is set to use pre-release software and install/update the ODataLib Contrib to its latest version (18.104.22.168016-rc2 at the time of writing) This will also take care of the rest of dependencies, i.e. ODataLib, EdmLib and System.Spatial
Clone the source for the full ASP .NET Web Stack with your favourite git client. Git Extensions is my weapon of choice.
git clone https://git01.codeplex.com/aspnetwebstack.git
Open the "Extensions.sln" solution within the root folder, and get building. Note that you will have to either remove the "code signing" options in the project properties or supply your own signing key.
Alternatively, you could also download a nightly build and avoid this step altogether. To finish up, you only need to add a reference to the resulting dll under this path
And there you go, you now have a Web API project the includes the freshest OData updates.
I really enjoy those moments in life when "things just work" (TM)