Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

Summary

Adds documentation showing how to use AddAzureOpenAIClient and AddImageGenerator with dependency injection in hosted applications, addressing feedback from the text-to-image article review.

Fixes #49391 (discussion thread)

Changes

  • New section: "Use hosting integration" added before "Best practices"

    • Shows Aspire.Azure.AI.OpenAI package usage for registering Azure OpenAI client
    • Demonstrates AddImageGenerator for DI container registration
    • Includes minimal API endpoint example with IImageGenerator injection
  • Code sample: Complete ASP.NET Core web app at snippets/text-to-image/hosting/TextToImageHosting/

    • Uses connection string from configuration for Azure OpenAI endpoint
    • Registers image generator as singleton service
    • Example POST endpoint returning generated image as response

Example Usage

var openai = builder.AddAzureOpenAIClient("openai");

builder.Services.AddImageGenerator(services =>
{
    var openAiClient = services.GetRequiredService<OpenAIClient>();
    return openAiClient.GetImageClient("gpt-image-1").AsIImageGenerator();
});

app.MapPost("/generate-image", async (IImageGenerator generator, string prompt) =>
{
    var response = await generator.GenerateImagesAsync(prompt, new ImageGenerationOptions());
    return Results.File(response.Contents.OfType<DataContent>().First().Data.ToArray(), "image/png");
});

Follows pattern from ericstj/imageGeneratorSample.

Original prompt

This section details on the original issue you should resolve

<issue_title>Show how to use hosting for AddAzureOpenAIClient and AddImageGenerator</issue_title>
<issue_description>Add a section to the end of the https://github.com/dotnet/docs/blob/main/docs/ai/quickstarts/text-to-image.md article that shows how to use this stuff with hosting, by using things like AddAzureOpenAIClient and AddImageGenerator (similar to https://github.com/ericstj/imageGeneratorSample/blob/8a976997f5a762885c2fa3e76c206723263c3ded/imageGeneratorSample.Web/Program.cs#L16-L26.)

Originally posted by @ericstj in #49391 (comment)
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


Internal previews

📄 File 🔗 Preview link
docs/ai/quickstarts/text-to-image.md Generate images from text using AI

…amples

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot AI changed the title [WIP] Add hosting section for AddAzureOpenAIClient and AddImageGenerator Add hosting integration section to text-to-image quickstart Nov 14, 2025
Copilot AI requested a review from gewarren November 14, 2025 01:37
Copilot finished work on behalf of gewarren November 14, 2025 01:37
@gewarren gewarren marked this pull request as ready for review November 14, 2025 01:58
@gewarren gewarren requested a review from a team as a code owner November 14, 2025 01:58
Copilot AI review requested due to automatic review settings November 14, 2025 01:58
Copilot finished reviewing on behalf of gewarren November 14, 2025 02:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new "Use hosting integration" section to the text-to-image quickstart documentation, demonstrating how to integrate image generation capabilities into ASP.NET Core applications using dependency injection and the Aspire Azure OpenAI package. The addition addresses feedback from the original article review and follows the pattern established in a reference sample repository.

  • Adds comprehensive hosting integration documentation showing dependency injection setup with AddAzureOpenAIClient and AddImageGenerator
  • Includes a complete working ASP.NET Core web application sample demonstrating the hosting pattern
  • Documents configuration management through connection strings and the benefits of the DI approach

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
docs/ai/quickstarts/text-to-image.md Adds "Use hosting integration" section with setup instructions, code examples, and benefits explanation
docs/ai/quickstarts/snippets/text-to-image/hosting/TextToImageHosting/Program.cs Complete ASP.NET Core sample demonstrating Azure OpenAI client registration and image generator endpoint
docs/ai/quickstarts/snippets/text-to-image/hosting/TextToImageHosting/TextToImageHosting.csproj Project file with required NuGet packages for hosting integration
docs/ai/quickstarts/snippets/text-to-image/hosting/TextToImageHosting/appsettings.json Configuration file showing connection string structure for Azure OpenAI
docs/ai/quickstarts/snippets/text-to-image/hosting/TextToImageHosting/appsettings.Development.json Development-specific logging configuration
docs/ai/quickstarts/snippets/text-to-image/hosting/TextToImageHosting/Properties/launchSettings.json Launch profiles for HTTP and HTTPS debugging

<ItemGroup>
<PackageReference Include="Aspire.Azure.AI.OpenAI" Version="13.0.0-preview.1.25560.3" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.5.0-beta.1" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.0.0-preview.1.25560.10" />
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Microsoft.Extensions.AI.OpenAI package version 10.0.0-preview.1.25560.10 is newer than the version used in the non-hosting sample (9.10.1-preview.1.25521.4). Consider aligning these versions for consistency unless the hosting integration specifically requires the newer version.

Suggested change
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.0.0-preview.1.25560.10" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.10.1-preview.1.25521.4" />

Copilot uses AI. Check for mistakes.
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM @gewarren

You can decide how to handle the Copilot feedback. Some of it seems reasonable.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show how to use hosting for AddAzureOpenAIClient and AddImageGenerator

3 participants