Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft OData in .NET CORE 5 - Adding OData to services throws up a missing using directive yet the package is there

I am developing in .net core 5.0. (There is a tutorial by Sam Xu on moving to dotnet core 5)

I have gone back to the absolute bare minimum with the most simple API project in Visual Studio.

I had this working in my project earlier in the year and it was running on .net core 5.0. See tutorial above.

In this project I have created a new project. Then I went to NuGet to get the package "Microsoft.AspNet.OData" version 7.4.1

I then added the following to the startup file.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddCors();
    services.AddControllers();

    services.AddOData();  //THIS ONE
}

I added "services.AddOData" and its throwing up the error,

Error CS1061 'IServiceCollection' does not contain a definition for 'AddOData' and no accessible extension method 'AddOData' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?) JobsLedger.API C:\Users/.../JobsLedger.API\Startup.cs 35 Active

I had already added the package required for this service. Now I had this working a couple of months ago.

Is there a new package that you need to add?

What am I doing wrong or is this a "breaking change" that I dont know about?

like image 970
si2030 Avatar asked Sep 07 '20 06:09

si2030


People also ask

What is OData in web API?

The Open Data Protocol (OData) is a data access protocol for the web. OData provides a uniform way to query and manipulate data sets through CRUD operations (create, read, update, and delete). ASP.NET Web API supports both v3 and v4 of the protocol.

Which of the following is the class to derive from for an OData service in Web API?

Creating an OData service. To define an OData service, simply derive from ODataController. This is a base class for OData controllers that support writing and reading data using the OData formats.


1 Answers

if you are using .net5.0 it required odata 8.0 preview.

In rc2, according to this article https://devblogs.microsoft.com/odata/attribute-routing-in-asp-net-core-odata-8-0-rc/, Sam Xu made a breaking change.

"AddOData is changed from extensions on ISerivceCollection to extension on IMvc(Core)Builder. The migration is easy by calling AddControllers() first, then calling AddOData()."

services.AddControllers()
        .AddOData(opt => opt.AddModel("odata", GetEdmModel()));

[UPDATE - 11/2021]

In odata 8.0 AddModel doesn't exist anymore, it was renamed with AddRouteComponents

services.AddControllers()
        .AddOData(opt => opt.AddRouteComponents("odata", GetEdmModel()));

https://devblogs.microsoft.com/odata/api-versioning-extension-with-asp-net-core-odata-8/

like image 64
Mattia Fravezzi Avatar answered Oct 20 '22 00:10

Mattia Fravezzi