Skip to content

Add ASP.NET Core example and update README #499

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

claudiogodoy99
Copy link

Hi team,

This PR adds a concrete example showing how to properly register and use the OpenAIClient with dependency injection in an ASP.NET Core application. It also includes a short highlighted section in the main README.md under the heading "How to use dependency injection".

PR for issue #498

@jsquire
Copy link
Collaborator

jsquire commented Jul 2, 2025

Thanks for your contribution, @claudiogodoy99, and your interest in improving the OpenAI developer experience. We'll take a look and see if we can get this merged in!

@jsquire
Copy link
Collaborator

jsquire commented Jul 2, 2025

@scottaddie, @achandmsft: Would appreciate your thoughts on sample content and docs organization.

@jsquire jsquire self-requested a review July 2, 2025 00:35
@@ -138,6 +138,46 @@ AudioClient ttsClient = client.GetAudioClient("tts-1");
AudioClient whisperClient = client.GetAudioClient("whisper-1");
```

## How to use dependency injection
Copy link
Collaborator

Choose a reason for hiding this comment

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

Location seems a good choice to me and fits the taxonomy. Can we please add an entry to the Table of Contents for this?

@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably rename the folder and solution to something like aspnet-core to avoid spaces and make use across different OSes easier.

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we please rename the project to match the solution/directory?


1. **Set your OpenAI API key** using one of these methods:

**Environment Variable (Recommended):**
Copy link
Collaborator

Choose a reason for hiding this comment

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

silly as it may seem, we should consider using Windows conventions here by default (or PowerShell) and linking out to docs for *nix shells. For some reason, we've seen a number of folks struggle with using environment variables in the .NET ecosystem.

@@ -0,0 +1,118 @@
# OpenAI ASP.NET Core Example

This example demonstrates how to use the OpenAI .NET client library with ASP.NET Core's dependency injection container, registering a ChatClient as a singleton for optimal performance and resource usage.
Copy link
Collaborator

Choose a reason for hiding this comment

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

We may want to consider linking to things for DI and ASP.NET Core and such. Probably better to go with an "Additional Resources" area at the bottom than inline links, but your call.

**Chat Completion:**

```bash
curl -X POST "https://localhost:7071/chat/complete" \
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we should demonstrate PowerShell or similar as well since cURL isn't baked into Windows.


app.Run();

record ChatRequest(string Message);
Copy link
Collaborator

Choose a reason for hiding this comment

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

probably worth a comment here, as record types are fairly new and many .NET developers aren't yet familair.

@claudiogodoy99
Copy link
Author

hey @jsquire, thanks for reviewing, I've made some commits addressing the comments.

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.

2 participants