18

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?

si2030
  • 3,895
  • 8
  • 38
  • 87
  • did you add the `using Microsoft.AspNet.OData.Extensions;`in your Startup.cs? – J.Loscos Sep 07 '20 at 07:34
  • I solved this by going back to a point where my original project actually built and then upgrading the packages again. This time it now builds and yes I note I did have the suggestion already in the using statements as suggested just above. – si2030 Sep 07 '20 at 08:37
  • Shouldn't you use `Microsoft.AspNetCore.OData` instead? Note: Version 8.0 Preview has some breaking changes – Rahul Oct 15 '20 at 05:24

2 Answers2

29

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/

Mattia Fravezzi
  • 403
  • 5
  • 9
0

For .Net 7.0, AddOData works with IMvcBuilder, as shown below

services.AddMvc().AddOData();

Reference: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.odata.odatamvcbuilderextensions.addodata?view=odata-aspnetcore-8.0

enter image description here

Binoy
  • 31
  • 1
  • 6