Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OCS getting started guide for VS 2022 #327

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 25 additions & 35 deletions Odata-docs/connectedservice/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ ms.topic: article

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](https://marketplace.visualstudio.com/items?itemName=laylaliu.ODataConnectedService) and supports Visual Studio 2019 and 2017.
The extension is available at Visual Studio Marketplace:

- [OData Connected Service 2022+](https://marketplace.visualstudio.com/items?itemName=marketplace.ODataConnectedService2022) for Visual Studio 2022
- [OData Connected Service](https://marketplace.visualstudio.com/items?itemName=marketplace.ODataConnectedService) for 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](https://www.odata.org/blog/trippin-new-odata-v4-sample-service/) OData service.

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

> [!NOTE]
> .NET Framework is also supported, but you would have to manually install Microsoft.OData.Client 7.x since the latest version of OData core libraries do no support .NET Framework.

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.
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 **OData Connected Service 2022+**.

![OData Connected Service extension](../assets/2020-07-15-OCS-0-10-0-extension-download.png)
![OData Connected Service extension](../assets/2024-09-03-ocs-vs22-extension.png)

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

Expand All @@ -41,6 +47,9 @@ After successful completion, you should see a **Connected Services** section und

![TripPin Connected Service added to project](../assets/2020-03-06-OCS-added-to-project.png)

> [!NOTE]
> OData Connected Service installs the latest versions of OData core libraries (i.e. OData.NET) if they are not installed. OData.NET 8 libraries support .NET 8 and later, they do not support older versions of .NET Core or any version of .NET Framework. If your project is targeting any of these .NET versions, then you should manually install Microsoft.OData.Client 7.x.

## 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:
Expand All @@ -51,7 +60,7 @@ 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:
Let's create a new method in `Program.cs` file called `ListPeople()` with the following code:

```c#
async Task ListPeople()
Expand All @@ -76,47 +85,28 @@ The `DataServiceQuery<TElement>` class allows you to execute LINQ-enabled querie

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`:
Now let's call the `ListPeople()` method from the top of the file:

```c#
ListPeople().Wait();
```

Don't forget to add the following using statements as well:
```c#
using System.Collections.Generic;
using System.Threading.Tasks;
await ListPeople();
```

The final code should look like:

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

namespace ODataClientExample
await ListPeople();

static async Task ListPeople()
{
class Program
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)
{
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);
}
}
Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
}
}
```
Expand Down
29 changes: 22 additions & 7 deletions Odata-docs/odatacli/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ There are 2 ways to download and install the **OData CLI**
To install the latest release of the OData CLI [Nuget Package](https://www.nuget.org/packages/Microsoft.OData.Cli/), use the [dotnet tool install](/dotnet/core/tools/dotnet-tool-install) command

```.NET CLI
dotnet tool install --global Microsoft.OData.Cli --version 0.1.0
dotnet tool install --global Microsoft.OData.Cli
```

- Direct Download:

You can download the binary files from this [GitHub Release](https://github.com/OData/ODataConnectedService/releases/tag/v0.1.0) and use them directly to generate code.
You can download the binary files from this [GitHub Release](https://github.com/OData/ODataConnectedService/releases) and use them directly to generate code.

## Synopsis

Expand Down Expand Up @@ -56,30 +56,41 @@ It generates strongly typed C# and Visual Basic client code for a specified ODat
### Synopsis

```Console
odata-cli generate [-h|--help] [-m|--metadata-uri] [-fn|--file-name] [-h|--custom-headers] [-p}--proxy] [-ns|--namespace-prefix] [-ucc|--upper-camel-case] [-i|--internal] [--multiple-files] [-eoi|--excluded-operation-imports] [-ebo|--excluded-bound-operations] [-est|--excluded-schema-types] [-o|--outputdir]
odata-cli generate [options]
```

### Options

- `--metadata-uri|-m`

The URI of the metadata document. The value must be set to a valid service document URI or a local file path. This is a required option
The URI of the metadata document. The value must be set to a
valid service document URI or a local file path. Optional if
config file is specified. If specified, it will take precedence
over the value in config file.

- `--config-file|-c`

Path to the OData Connected Service config file.

- `--file-name|-fn`

The name of the generated file. If not provided, then the default name 'Reference.cs/.vb' is used.

- `--custom-headers|-h`

Headers that will get sent along with the request when fetching the metadata document from the service. `Format: Header1:HeaderValue, Header2:HeaderValue`.
Headers that will get sent along with the request when fetching the metadata document from the service. `Format: Header1:HeaderValue, Header2:HeaderValue`.

- `--proxy|-p`

Proxy settings. `Format: domain\\user:password@SERVER:PORT`.
Proxy settings. `Format: domain\\user:password@SERVER:PORT`.

- `--namespace-prefix|-ns`

The namespace of the client code generated. Example: NorthWindService.Client or it could be a name related to the OData endpoint that you are generating code for.
The namespace of the client code generated. Example: `NorthWindService.Client` or it could be a name related to the OData endpoint that you are generating code for.

- `--enable-tracking|-et`

Enable entity and property tracking.

- `--upper-camel-case|-ucc`

Expand All @@ -89,6 +100,10 @@ odata-cli generate [-h|--help] [-m|--metadata-uri] [-fn|--file-name] [-h|--custo

Applies the `internal` class modifier on generated classes instead of `public` thereby making them invisible outside the assembly.

- `omit-versioning-info|-vi`

Omit runtime version and code generation timestamp from the generated files.

- `--multiple-files`

Split the generated classes into separate files instead of generating all the code in a single file.
Expand Down