April 6th, 2020

Move OData to .NET 5

Sam Xu
Senior Software Engineer

Introduction

Along with the Announcing .NET 5 preview 1, it’s time to move OData to .NET 5. This blog is intended to describe how easy to move the BookStore sample introduced in ASP.NET Core OData now Available  onto .NET 5.

Let’s get started.

Install .NET 5

.NET 5 SDK is required to build the .NET 5 application. So, Let’s follow up the instructions in Announcing .NET 5 preview 1 to install the .NET 5 SDK.

Meanwhile, I also install the Visual Studio 2019 Preview to edit and compile the .NET 5 project. It’s easy to download Visual Studio 2019 Preview from here. The required VS version supporting .NET 5 is 16.6.

Updating BookStore Project

It’s easy to target the BookStore project to .NET 5 when we finish the installation of .NET 5.

Just open the BookStore solution, double click the project, then edit the “BookStore.csproj” contents as below:

Updating the codes

 

In order to compile the project, we have to change some codes in the Startup.cs.

First, in ConfigureServices() method, change its content as below:

Then, in Configure() method, change its content as below:

Be noted, the parameter ‘IHostingEnvironment’ of Configure() should change to use “IWebHostEnvironment’.

 

Query the Resources

That’s all. Now, we can build and run the book store application.

For example:

We can file a GET request as : http://localhost:5001/odata/Books(2)

And the response returns the second book as follows:

The application also supports the advanced OData query options like:

http://localhost:5001/odata/Books?$filter=Price le 50&$expand=Press($select=Name)&$select=ISBN,Location($select=Street)

The response payload should look like:

Summary

Thanks for reading. We encourage you to download the latest ASP.NET Core OData package from Nuget.org and start building amazing OData service running on any .NET 5 platforms, such as Windows, MacOS and Linux.  Enjoy it!

You can refer to here for the sample project created in this blog. Any questions or concerns, feel free email to saxu@microsoft.com

 

Author

Sam Xu
Senior Software Engineer

Sam is a Senior software engineer at Microsoft with over than 10 years of software developement experience. He's worked on a wide variety of platforms such as (C++, C#, etc.) and currently works on the OData team to design and implement features in the .NET stack of Microsoft's OData libraries. OData (Open Data Protocol) is an ISO/IEC approved, OASIS standard that defines a set of best practices for building and consuming RESTful APIs. You can find more information about OData at ...

More about author

2 comments

Discussion is closed. Login to edit/delete existing comments.

  • Shimmy Weitzhandler

    Hi Sam and thanks for your post!
    I was wondering how Restier is going to advance with these great news.
    I’m so glad the OData is coming back to life, especially seeing a significant effort in improving Microsoft.OData.Client and ODataConnectedService. For all of that, I’m very grateful!

    • Sam XuMicrosoft employee Author

      @Shimmy, Thanks for your comments. For Restier, OData team is working tightly with .NET Foundation to move the Restier forward.