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

dotnet tutorial kiota dependency injection #89

Merged
merged 13 commits into from
Jul 2, 2024
2 changes: 2 additions & 0 deletions OpenAPI/kiota/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ items:
items:
- name: Add an API to search
href: add-api.md
- name: Use Kiota with Dependency Injection in .NET
href: tutorials/dotnet-dependency-injection.md
- name: Support
href: support.md
1 change: 1 addition & 0 deletions OpenAPI/kiota/tutorials/dotnet-azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ In this tutorial, you generate an API client that uses [Microsoft identity authe
## Required tools

- [.NET SDK 8.0](https://get.dot.net/8)
- [Kiota CLI](/openapi/kiota/install?tabs=bash&wt.mc_id=SEC-MVP-5004985#install-as-net-tool)

## Create a project

Expand Down
115 changes: 115 additions & 0 deletions OpenAPI/kiota/tutorials/dotnet-dependency-injection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
title: Use Kiota with dependency injection in .NET
description: Learn how you can combine Kiota with dependency injection in .NET to build API clients.
author: svrooij
ms.author: vibiret
ms.topic: tutorial
date: 05/10/2024
---

# Register a Kiota API client in .NET with dependency injection

In this tutorial, you will learn how to register a [Kiota API client](/openapi/kiota/tutorials/dotnet-azure) with [dependency injection](/dotnet/core/extensions/dependency-injection-usage?wt.mc_id=SEC-MVP-5004985).

## Required tools

- [.NET SDK 8.0](https://get.dot.net/8)
- [Kiota CLI](/openapi/kiota/install?tabs=bash#install-as-net-tool)

## Create a project

Run the following command in the directory you want to create a new project.

```dotnetcli
dotnet new webapi -o kiota-with-di
cd kiota-with-di
dotnet new gitignore
```

## Add dependencies

Before you can compile and run the generated API client, ensure the generated source files are part of a project with the required dependencies. Your project must reference the [abstraction package](https://github.com/microsoft/kiota-abstractions-dotnet) and default implementations.

- HTTP ([Kiota default HttpClient-based implementation](https://github.com/microsoft/kiota-http-dotnet)) version `1.4.2` or higher
- JSON serialization ([Kiota default](https://github.com/microsoft/kiota-serialization-json-dotnet))
- Form serialization ([Kiota default](https://github.com/microsoft/kiota-serialization-form-dotnet))
- Text serialization ([Kiota default](https://github.com/microsoft/kiota-serialization-text-dotnet))
- Multipart serialization ([Kiota default](https://github.com/microsoft/kiota-serialization-multipart-dotnet))
- HttpClient support for Dependency Injection [Microsoft.Extensions.Http](https://www.nuget.org/packages/Microsoft.Extensions.Http)

Run the following commands to get the required dependencies.

```dotnetcli
dotnet add package Microsoft.Extensions.Http
dotnet add package Microsoft.Kiota.Abstractions
dotnet add package Microsoft.Kiota.Http.HttpClientLibrary
dotnet add package Microsoft.Kiota.Serialization.Json
dotnet add package Microsoft.Kiota.Serialization.Form
dotnet add package Microsoft.Kiota.Serialization.Text
dotnet add package Microsoft.Kiota.Serialization.Multipart
```

## Generate the API client

Kiota generates API clients from OpenAPI documents. Let's download the github api specs and generate the api client.

You can then use the [Kiota](/openapi/kiota/install?tabs=bash&wt.mc_id=SEC-MVP-5004985#install-as-net-tool) command line tool to generate the API client classes.

```bash
# Download the specs to ./github-api.json
kiota download apisguru::github.com -o ./github-api.json
# Generate the client, path filter for just releases
kiota generate -l csharp -d github-api.json -c GitHubClient -n GitHub.ApiClient -o ./GitHub --include-path "/repos/{owner}/{repo}/releases/*" --clean-output
```

## Create extension methods

Create a new file named _KiotaServiceCollectionExtensions.cs_ in the root of your project and add the following code.

:::code language="csharp" source="~/code-snippets/get-started/dotnet-dependency-injection/KiotaServiceCollectionExtensions.cs":::

## Create a client factory

Create a new file named _GitHubClientFactory.cs_ in the _GitHub_ folder and add the following code.

This is what you call a factory pattern, where you create a factory class that is responsible for creating an instance of the client. This way you have a single place that is responsible for creating the client, and you can easily change the way the client is created. In this sample we will be registering the factory in the dependency injection container as well, because it will get the `HttpClient` injected in the constructor.

:::code language="csharp" source="~/code-snippets/get-started/dotnet-dependency-injection/GitHub/GitHubClientFactory.cs":::

## Register the API client

Open the _Program.cs_ file and add the following code above the `var app = builder.Build();` line. Sample [Program.cs](https://github.com/microsoft/kiota-samples/blob/main/get-started/dotnet-dependency-injection/Program.cs).

:::code language="csharp" source="~/code-snippets/get-started/dotnet-dependency-injection/Program.cs" highlight="9-23":::

## Create an endpoint

We will now create an endpoint that uses the GitHub client from dependency injection to get the latest release of `dotnet/runtime`.

Open the _Program.cs_ file and add the following code above the `app.Run();` line. Sample [Program.cs](https://github.com/microsoft/kiota-samples/blob/main/get-started/dotnet-dependency-injection/Program.cs).

:::code language="csharp" source="~/code-snippets/get-started/dotnet-dependency-injection/Program.cs" highlight="56-66":::

## Run the application

Run the following command in your project directory to start the application.

```dotnetcli
dotnet run
```

You can now open a browser and navigate to `http://localhost:{port}/dotnet/releases` to see the latest release of `dotnet/runtime`. Alternatively, you can also go to `http://localhost:{port}/swagger` to see the Swagger UI with this new endpoint.

## Authentication

The `GitHubClientFactory` class is responsible for creating the client, and it is also responsible for creating the `IAuthenticationProvider`. In this sample, we are using the `AnonymousAuthenticationProvider`, which is a simple provider that does not add any authentication headers. More details on authentication with Kiota can be found in the [Kiota authentication documentation](/openapi/kiota/authentication).

The `GitHubClientFactory` is used through dependency injection, which means you can have other types injected by the constructor that are needed for the authentication provider.

You might want to create a [BaseBearerTokenAuthenticationProvider](/openapi/kiota/authentication?tabs=csharp&wt.mc_id=SEC-MVP-5004985#base-bearer-token-authentication-provider) that is available in Kiota and expects an `IAccessTokenProvider` which is just an interface with one method "give me an access token". Access tokens usually have a lifetime of 1 hour, which is why you need to get them at runtime and not set them at registration time.

## See also

- [Sample project used in this guide](https://github.com/microsoft/kiota-samples/blob/main/get-started/dotnet-dependency-injection?wt.mc_id=SEC-MVP-5004985)
- [Build API clients for .NET with Microsoft Identity authentication](/openapi/kiota/tutorials/dotnet-azure) shows how to use Kiota with Microsoft Identity authentication.
- [ToDoItem Sample API](https://github.com/microsoft/kiota-samples/tree/main/sample-api) implements a sample OpenAPI in ASP.NET Core and sample clients in multiple languages.
3 changes: 3 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"ckigoonya",
"cligeneric",
"contoso",
"cref",
"deserializers",
"docfx",
"docset",
Expand Down Expand Up @@ -50,9 +51,11 @@
"quickstart",
"quickstarts",
"struct",
"svrooij",
"subnamespace",
"subnamespaces",
"vibiret"
"webapi",
],
"flagWords": [
"teh"
Expand Down