Skip to content

Commit 8efef8b

Browse files
committed
chore: upgrades to latest APIs for kiota collection injection
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent cdf5ef5 commit 8efef8b

3 files changed

Lines changed: 16 additions & 19 deletions

File tree

get-started/dotnet-dependency-injection/KiotaServiceCollectionExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.Kiota.Http.HttpClientLibrary;
1+
using Microsoft.Kiota.Http.HttpClientLibrary;
22

33
/// <summary>
44
/// Service collection extensions for Kiota handlers.
@@ -14,9 +14,9 @@ public static class KiotaServiceCollectionExtensions
1414
public static IServiceCollection AddKiotaHandlers(this IServiceCollection services)
1515
{
1616
// Dynamically load the Kiota handlers from the Client Factory
17-
var kiotaHandlers = KiotaClientFactory.GetDefaultHandlerTypes();
17+
var kiotaHandlers = KiotaClientFactory.GetDefaultHandlerActivatableTypes();
1818
// And register them in the DI container
19-
foreach(var handler in kiotaHandlers)
19+
foreach (var handler in kiotaHandlers)
2020
{
2121
services.AddTransient(handler);
2222
}
@@ -36,13 +36,13 @@ public static IServiceCollection AddKiotaHandlers(this IServiceCollection servic
3636
public static IHttpClientBuilder AttachKiotaHandlers(this IHttpClientBuilder builder)
3737
{
3838
// Dynamically load the Kiota handlers from the Client Factory
39-
var kiotaHandlers = KiotaClientFactory.GetDefaultHandlerTypes();
39+
var kiotaHandlers = KiotaClientFactory.GetDefaultHandlerActivatableTypes();
4040
// And attach them to the http client builder
41-
foreach(var handler in kiotaHandlers)
41+
foreach (var handler in kiotaHandlers)
4242
{
4343
builder.AddHttpMessageHandler((sp) => (DelegatingHandler)sp.GetRequiredService(handler));
44-
}
44+
}
4545

4646
return builder;
4747
}
48-
}
48+
}

get-started/dotnet-dependency-injection/KiotaWithDependencyInjection.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.4" />
11-
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.4" />
1211
<PackageReference Include="Microsoft.Kiota.Bundle" Version="1.21.3" />
1312
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.5" />
1413
</ItemGroup>

get-started/dotnet-dependency-injection/Program.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
using GitHub;
1+
using GitHub;
22
var builder = WebApplication.CreateBuilder(args);
33

44
// Add services to the container.
5-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
65
builder.Services.AddEndpointsApiExplorer();
7-
builder.Services.AddSwaggerGen();
6+
builder.Services.AddOpenApi();
87

98
// ----------- Add this part to register the generated client -----------
109
// Add Kiota handlers to the dependency injection container
1110
builder.Services.AddKiotaHandlers();
1211

1312
// Register the factory for the GitHub client
14-
builder.Services.AddHttpClient<GitHubClientFactory>((sp, client) => {
13+
builder.Services.AddHttpClient<GitHubClientFactory>((sp, client) =>
14+
{
1515
// Set the base address and accept header
1616
// or other settings on the http client
1717
client.BaseAddress = new Uri("https://api.github.com");
@@ -27,8 +27,8 @@
2727
// Configure the HTTP request pipeline.
2828
if (app.Environment.IsDevelopment())
2929
{
30-
app.UseSwagger();
31-
app.UseSwaggerUI();
30+
app.MapOpenApi();
31+
app.UseSwaggerUI(o => o.SwaggerEndpoint("/openapi/v1.json", "My API"));
3232
}
3333

3434
app.UseHttpsRedirection();
@@ -40,7 +40,7 @@
4040

4141
app.MapGet("/weatherforecast", () =>
4242
{
43-
var forecast = Enumerable.Range(1, 5).Select(index =>
43+
var forecast = Enumerable.Range(1, 5).Select(index =>
4444
new WeatherForecast
4545
(
4646
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
@@ -50,8 +50,7 @@
5050
.ToArray();
5151
return forecast;
5252
})
53-
.WithName("GetWeatherForecast")
54-
.WithOpenApi();
53+
.WithName("GetWeatherForecast");
5554

5655
// ----------- Add this part to create a new endpoint that uses the generated client -----------
5756

@@ -60,8 +59,7 @@
6059
var releases = await client.Repos["dotnet"]["runtime"].Releases["latest"].GetAsync(cancellationToken: cancellationToken);
6160
return releases;
6261
})
63-
.WithName("GetDotnetReleases")
64-
.WithOpenApi();
62+
.WithName("GetDotnetReleases");
6563

6664
// ----------- Add this part to create a new endpoint that uses the generated client end -----------
6765

0 commit comments

Comments
 (0)