OData stands for the Open Data Protocol. It was initiated by Microsoft and is now an ISO and OASIS standard. OData enables the creation and consumption of RESTful APIs, which allow resources, defined in a data model and identified by using URLs, to be published and edited by Web clients using simple HTTP requests.
RESTier is a RESTful API development framework for building standardized, OData V4 based RESTful services on .NET platform. It can be seen as a middle-ware on top of Web API OData. RESTier provides facilities to bootstrap an OData service like what WCF Data Services (which is sunset) does, beside this, it supports to add business logic in several simple steps, has flexibility and easy customization like what Web API OData do. It also supports to add additional publishers to support other protocols and additional providers to support other data sources.
For more information about OData, please refer to the following resources:
For more information about OData .Net Library, refer to OData .Net Library document.
For more information about Web API OData Library, refer to Web API OData Library document.
For more information about RESTier, refer to RESTier document.
Please be noted that currently RESTier is still a preview version.
The RESTier document has a comprehensive how-to for how to bootstrap an OData service in combination with an MS SQL database. Bootstrapping an OData service in combination with a PostgreSQL database is much the same, but on some points a little different as it is not supported out-of-the-box by Microsoft Visual Studio.
Before you start, you’ll have to install the Npgsql PostgreSQL Integration extension into Visual Studio.
-
Open Visual Studio 2017 or Visual Studio 2015. If you use Visual Studio 2017 the screens might be slightly different from the screenshots, but the procedures are essentially the same.
-
From the TOOLS menu, click Extensions and Updates….
-
In the Extensions and Updates dialog box, click Online.
-
In the search box type npgsql and press Enter.
-
Click on the Npgsql PostgreSQL Integration extension and click on Download.
-
In the Download and Install dialog box, click Install.
-
After installation Visual Studio will ask for a restart. Click Restart Now.
Continue with the step-by-step description to Create a project and a web app.
-
In the Solution Explorer window, right click the project HelloWorld and select Manage NuGet Packages….
-
In the NuGet Package Manager window, select the Include prerelease checkbox.
-
Type npgsql restier in the Search Box beside and press Enter.
-
Select Npgsql.Restier and click the Install button.
-
In the Preview dialog box, click the OK button
-
In the License Acceptance dialog box, click the I Accept button.
We need to add the data provider and connection string manually, as the connection builder crashes when trying to build a connection string to PostgreSQL. See this issue.
-
Open the project’s web.config.
-
Add the Npgsql data provider factory.
<system.data> <DbProviderFactories> <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" /> </DbProviderFactories> </system.data>
-
Add a connection string and set the correct values.
<connectionStrings> <add name="defaultConnection" connectionString="Host=[host];Port=5432;Database=[database];Username=[username];Password=***" providerName="Npgsql" /> </connectionStrings>
-
In the Solution Explorer window, right click the Models folder under the project HelloWorld and select Add > New Item.
-
In the Add New Item - HelloWorld dialog box, click C# > Data > ADO.NET Entity Data Model.
-
Name the model HelloWorldData.
-
Click the Add button.
-
In the Entity Data Model Wizard window, select the item Code First from database.
-
Click the Next button.
-
The connection string should be selected by default.
-
Uncheck the Save connection settings in Web.Config as: checkbox.
-
Click the Next button.
-
Select the Tables check box and click the Finish button.
In the Solution Explorer window, click HelloWorld > App_Start > WebApiConfig.cs. Replace the WebApiConfig
class the following code.
namespace HelloWorld
{
public static class WebApiConfig
{
public static async void Register(HttpConfiguration config)
{
// enable query options for all properties
config.Filter().Expand().Select().OrderBy().MaxTop(null).Count();
await config.MapRestierRoute<EntityFrameworkApi<HelloWorldData>>(
"defaultConnection",
"api/odata",
new RestierBatchHandler(GlobalConfiguration.DefaultServer));
}
}
}
The configuration config.Filter().Expand().Select().OrderBy().MaxTop(null).Count();
is enabling filter/expand/select/orderby/count on all properties.
Starting 1.0 release, there are more smaller granularity control on the properties which can be used in query option, and all properties are disabled to be used by default.
Users can add configured in CLR class or during model build to configure which properties are allowed to be used in filter/expand/select/orderby/count.
Refer to Model bound document for more details.
After these steps, you will have finished bootstrapping an OData service endpoint.
You can then Run the project and an OData service is started.
Then you can start by accessing the URL http://localhost:<ISS Express port>/api/odata
to view all available entity sets, and try with other basic OData CRUD operations.
For instance, you may try querying any of the entity sets using the $select
, $filter
, $orderby
, $top
, $skip
or $apply
query string parameters.
Please visit the RESTier pages. It has detailed descriptions on each feature provided by RESTier.
Refer to sample service github for end to end sample service. The source code also contains end to end service for end to end test purpose. All the sample service can be run with visual studio 2015.
-
Issues:: Report issues on Github issues.
-
Feedback:: Please send mails to software@ogd.nl