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

Viewed 5614

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?

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/

Related