20

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?

3
  • did you add the using Microsoft.AspNet.OData.Extensions;in your Startup.cs? Commented Sep 7, 2020 at 7: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. Commented Sep 7, 2020 at 8:37
  • Shouldn't you use Microsoft.AspNetCore.OData instead? Note: Version 8.0 Preview has some breaking changes Commented Oct 15, 2020 at 5:24

2 Answers 2

33

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/

Sign up to request clarification or add additional context in comments.

6 Comments

Nope, it's not on there either.
@BenRobinson I needed to add a using statement for Microsoft.AspNetCore.OData;
AddModel doesn't seem to exist anymore. Looks like we are supposed to use .AddOData(opt => opt.AddRouteComponents("odata", GetEdmModel())); instead.
This answer needs to be either fixed or removed. I just wasted 30 minutes trying to get this to work when @JonathanAllen's comment is the actual answer.
I just added odata 8.0 today in one of our projects and am glad the answer is updated with @JonathanAllen's comments.
|
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.