OData Connected Service

Applies To: # OData client v7 supportedOData Client V7 OData Client V7

OData Connected Service is a Visual Studio extension that generates strongly-typed C# and Visual Basic client code for a specified OData service. It generates a DataServiceContext class to interact with the service and CLR types for each entity type and complex type in the service model.

The extension is available at Visual Studio Marketplace and supports Visual Studio 2019 and 2017.

Creating a sample client application using OData Connected Service

In this sample we are going to install the extension and use it to create a sample client application for the sample TripPin OData service.

Open Visual Studio 2019 and create a new C# .Net Core project and call the project ODataClientExample (.Net Framework is also supported).

Install the OData Connected Service extension by going to Extensions menu -> Manage Extensions. In the Exensions window, search online for "OData Connected Service" and install it.

OData Connected Service extension

Once installed, right-click your project in the Solution Explorer -> Add -> Connected Service. In the Connected Services window that appears, select OData Connected Service.

Add connected service

Select OData Connected Service

Next, OData Connected Service provides us a wizard where we can configure settings for the service we want to connect to. In the Service Name field, enter "TripPinService" as the name of the service. And in the Address field, enter the URL of the metadata endpoint of the service. In this case, we enter: https://services.odata.org/V4/TripPinServiceRW/$metadata.

The Connected Service provides many options to control how the code is generated, but for this example we are just going to stick with the defaults. Click Finish to complete the configuration and generate the client code.

Configure OData Connected Service

After successful completion, you should see a Connected Services section under your project in the Solution Explorer. Below this section, you should see a folder for the "TripPinService" which contains the generated Reference.cs file containing the generated C# client code.

TripPin Connected Service added to project

Using the generated code

Now let's use the generated classes to implement our application logic. Open your Program.cs file and add the following using statement at the top:

using Microsoft.OData.SampleService.Models.TripPin

By default, the OData Connected Service generates the relevant classes in the same namespace defined in the OData metadata document. In this case, it's Microsoft.OData.SampleService.Models.TripPin.

Let's create a new method in Program class called ListPeople() with the following code:

async Task ListPeople()
{
    var serviceRoot = "https://services.odata.org/V4/TripPinServiceRW/";
    var context = new DefaultContainer(new Uri(serviceRoot));

    IEnumerable<Person> people = await context.People.ExecuteAsync();
    foreach (var person in people)
    {
        Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
    }
}

The above method creates a new instance of DefaultContainer pointed to the root endpoint of the OData service we used to generate the client. The DefaultContainer class is generated by OData Connected Service based on the entity container in the service model. It inherits from the DataServiceContext class that's defined in the OData client library (Microsoft.OData.Client). The DataServiceContext allows you to execute queries and other operations against the service. It also keeps track of the entities created and fetched when interacting with the service.

DefaultContainer is generated based on the Trip Pin service entity container and it knows about its data model. In the code snippet above context.People is generated from the People entity set defined in the model. context.People is an instance of DataServiceQuery<Person>.

The DataServiceQuery<TElement> class allows you to execute LINQ-enabled queries against a particular endpoint of the service. context.People executes queries that return a collection of Person instances.

The Person class is in turn generated based on the Person entity in the OData model and contains the properties defined in the model. The generated code also includes enums, classes corresponding to complex types and methods corresponding to bound and unbound functions and actions.

Now let's call the ListPeople() method from the Main method by adding the following statement in Main:

ListPeople().Wait();

Don't forget to add the following using statements as well:

using System.Collections.Generic;
using System.Threading.Tasks;

The final code should look like:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.OData.SampleService.Models.TripPin;

namespace ODataClientExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // from c# 7.1 you can use async Main instead
            ListPeople().Wait();
        }

        static async Task ListPeople()
        {
            var serviceRoot = "https://services.odata.org/V4/TripPinServiceRW/";
            var context = new DefaultContainer(new Uri(serviceRoot));

            IEnumerable<Person> people = await context.People.ExecuteAsync();
            foreach (var person in people)
            {
                Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
            }
        }
    }
}

Finally, let's run the code! You should see the following output: Sample client app lists names of people fetched from OData service

As you can see, the OData Connected Service generates C# classes corresponding to your specific OData service and uses the underlying OData client library to serialize and deserialize to and from JSON, saving you a lot of work.

In this tutorial you have learned how to use the OData Connected Service to generate client code to interact with a specific OData service. Visit other sections in this documentation to learn about all the features and configuration options provided by the tool.