如何在 Microsoft Foundry 部署 gpt-5.4-mini 並透過 .NET 呼叫
Deploy an Azure OpenAI Model in Microsoft Foundry and Call It from .NET
Lukas Walter 示範在 Microsoft Foundry 部署 gpt-5.4-mini,並以單一 C# 檔案透過 .NET 呼叫模型,將支援工單摘要成一句話。
This walkthrough deploys gpt-5.4-mini in Microsoft Foundry and calls it from a single C# file. Azure hosts the model, while the .NET app runs locally and sends a support ticket to the deployment for a one-sentence summary.
I used North Europe and an API key for this example. The call uses the OpenAI v1 Chat Completions API through IChatClient.
What you need
You need an Azure subscription with permission to create a Foundry resource, deploy a model, and retrieve its resource key. Install the .NET 11 SDK on the machine where you will run the C# file.
Check the SDK in your terminal:
dotnet --version
The sample targets SDK 11.0.100 and pins OpenAI 2.15.0 and Microsoft.Extensions.AI.OpenAI 11.0.1 in the file. There is no need to create a .csproj.
Both the playground and the local app make billable requests to the model. Use fictional text as you follow the steps.
Create the Foundry resource and project
In the Azure portal, open Resource groups and select Create. Choose your subscription, enter the name below, select a location for the resource group, then review and create it:
rg-foundry-dotnet-demo
Then create the Foundry resource:
- Search for Microsoft Foundry in the Azure portal and open its resource creation form.
- On Basics, select your subscription and
rg-foundry-dotnet-demo. - Give the resource a unique name using a pattern such as
foundry-dotnet-<unique-suffix>. Mine isfoundry-dotnet-lw2026. - Select North Europe as the resource’s region, or another region that supports your chosen model and API.
- If the form includes a default project name, enter
dotnet-model-demo. - Select Review + create, then Create after validation succeeds.
When deployment completes, open the resource and follow its link to the Foundry portal. Enable New Foundry if the toggle appears. Select dotnet-model-demo in the project selector. If the creation flow did not create a default project, create it within this Foundry resource.
The names refer to different things:
| Name | Purpose |
|---|---|
rg-foundry-dotnet-demo |
Resource group for the demo's Azure resources |
foundry-dotnet-lw2026 |
Foundry resource with the inference endpoint and resource key |
dotnet-model-demo |
Project for organizing the work in Foundry |
dotnet-demo |
Model deployment name, created in the next step |
The C# app uses the resource endpoint and deployment name. Use dotnet-model-demo in the project selector and dotnet-demo in the model call.
Deploy the model
In Foundry, open Catalog and search for gpt-5.4-mini, then open its model card. Check the version and supported APIs before deploying.
Select Deploy, then Custom settings. Name the deployment dotnet-demo and choose Global Standard. I used model version 2026-03-17.
Choose a small capacity allocation permitted by your subscription. My deployment had rate limits of 150,000 tokens per minute and 150 requests per minute. You do not need the same allocation to reproduce this single request. These limits govern throughput, not spending.
Global Standard charges for token usage and allows Azure to process inference globally. Locating the resource in North Europe does not make the deployment EU-only. If processing location is a requirement, select an appropriate deployment type and check model and region support before deploying.
Select Deploy. When deployment finishes, open Models, select dotnet-demo, and check the Details tab. The provisioning state should be Succeeded.
(Hint: Don't try my endpoint. It doesn't exist anymore :D)
The details also show the version upgrade policy. Mine is OnceNewDefaultVersionAvailable, which means the deployment can move to a newer default model version later. The version shown in the screenshot is the one I tested.
Test the deployment in the playground
Open the Playground tab for dotnet-demo. Keep the default instruction and submit this prompt:
Summarize this support ticket in one sentence:
After resetting my password, I can sign in on the website,
but the desktop app still rejects my credentials.
The deployment should now answer in the playground. Next, copy its connection details and send a request from your own process.
Copy the endpoint and key
Return to the deployment's Details tab and use the copy buttons beside Endpoint and Key.
Copy your resource's OpenAI v1 base URL. It has this form:
https://<your-resource>.services.ai.azure.com/openai/v1/
Use the full URL shown in the portal, including /openai/v1/. Azure also supports the openai.azure.com hostname for this API. The project endpoint, which ends in /api/projects/..., is for a different integration path.
The resource endpoint can serve multiple deployments. This request uses dotnet-demo.
For a Bash or zsh terminal, set the non-secret values first:
export AZURE_OPENAI_BASE_URL="https://<your-resource>.services.ai.azure.com/openai/v1/"
export AZURE_OPENAI_DEPLOYMENT="dotnet-demo"
On macOS with zsh, paste the API key into a hidden prompt to keep the literal key out of command history:
read -rs 'AZURE_OPENAI_API_KEY?Paste your API key and press Enter: '
printf '\n'
export AZURE_OPENAI_API_KEY
If you use PowerShell, run these commands instead:
$env:AZURE_OPENAI_BASE_URL = "https://<your-resource>.services.ai.azure.com/openai/v1/"
$env:AZURE_OPENAI_DEPLOYMENT = "dotnet-demo"
$key = Read-Host "Paste your API key" -AsSecureString
$env:AZURE_OPENAI_API_KEY = [System.Net.NetworkCredential]::new("", $key).Password
Remove-Variable key
In Bash, read and export the key with:
read -rs -p "Paste your API key and press Enter: " AZURE_OPENAI_API_KEY
printf '\n'
export AZURE_OPENAI_API_KEY
The key remains outside the C# file. These environment variables apply only to the current terminal session, so run the app from that same terminal. For an Azure-hosted application, consider managed identity when designing its authentication.
Call the deployment from one C# file
Create a file named foundry-chat.cs and paste in the following code:
#:property TargetFramework=net11.0
#:package OpenAI@2.15.0
#:package Microsoft.Extensions.AI.OpenAI@11.0.1
using Microsoft.Extensions.AI;
using OpenAI;
using ChatClient = OpenAI.Chat.ChatClient;
using System.ClientModel;
var endpoint = new Uri(RequiredEnvironmentVariable("AZURE_OPENAI_BASE_URL"));
var deployment = RequiredEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT");
var apiKey = RequiredEnvironmentVariable("AZURE_OPENAI_API_KEY");
var providerClient = new ChatClient(
deployment,
new ApiKeyCredential(apiKey),
new OpenAIClientOptions { Endpoint = endpoint });
using IChatClient chatClient = providerClient.AsIChatClient();
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(60));
ChatResponse response = await chatClient.GetResponseAsync(
[
new ChatMessage(ChatRole.System,
"You are an AI assistant that helps people find information."),
new ChatMessage(ChatRole.User, """
Summarize this support ticket in one sentence:
After resetting my password, I can sign in on the website,
but the desktop app still rejects my credentials.
""")
],
cancellationToken: timeout.Token);
Console.WriteLine(response.Text);
static string RequiredEnvironmentVariable(string name)
{
var value = Environment.GetEnvironmentVariable(name);
return !string.IsNullOrWhiteSpace(value)
? value
: throw new InvalidOperationException($"Set {name} before running this app.");
}
The #: directives configure the file-based app. TargetFramework selects .NET 11, while the package directives pin the dependencies.
ChatClient sends the request to the provider. AsIChatClient() lets the rest of the example use the Microsoft.Extensions.AI interface. The using alias resolves ChatClient to OpenAI.Chat.ChatClient, while ChatMessage comes from Microsoft.Extensions.AI. Both libraries define a ChatMessage type, so keeping OpenAI.Chat out of the imported namespaces avoids an ambiguous reference. Provider Independence from Day One explains where to put this provider wiring in a larger application.
Pass dotnet-demo as the client constructor's model argument. Azure expects the deployment name here, even though the underlying model is gpt-5.4-mini.
The helper checks for missing settings before sending a request. The request uses a 60-second cancellation token and the same support ticket as the playground. response.Text contains the output to print.
Run it:
dotnet run --file foundry-chat.cs
On the first run, the SDK restores the packages. My call returned:
Your summary may be worded differently. A successful response confirms that the endpoint, resource key, and deployment name work together.
If the first request fails
Start with the checks below. HTTP 401 and 403 errors can have overlapping causes, so consider the service's error message as well as the status code.
| Failure | What to check |
|---|---|
| Missing environment variable | Set all three values in the terminal that runs the app |
| HTTP 401 | Copy a current resource key and confirm it belongs to the resource in the endpoint |
| HTTP 403 | Check the resource's networking rules and whether key authentication is allowed |
| HTTP 404 | Check the /openai/v1/ base URL and the deployment name, including spelling |
| HTTP 429 | Check the deployment's request and token limits, then reduce request volume |
The SDK retries some transient failures automatically. Check the error before adding another retry loop. Configuration errors require a configuration change.
Clean up when you are finished
If you created rg-foundry-dotnet-demo only for this walkthrough, delete it in the Azure portal when you finish experimenting. Check its contents before confirming deletion, since Azure will also delete all resources in the group.
If you plan to reuse the deployment, keep it and manage access to its key. When you finish the local session, clear the key from the shell by running unset AZURE_OPENAI_API_KEY in Bash or zsh, or Remove-Item Env:AZURE_OPENAI_API_KEY in PowerShell.
When to use this setup
Use this setup for a first model integration, a deployment check, or a small local experiment. The single file makes it easy to trace how the endpoint and deployment name reach the provider client and how the response returns through IChatClient.
When this call becomes part of an ASP.NET Core application, Building the .NET AI App Foundation shows how to add validated configuration and an application service around it.
When not to use this setup
If you need to expose this call to other users, use a service hosted under your control. That service needs an authentication strategy, request limits, diagnostics, and failure handling. Keep the resource key out of browser code and distributed client applications.
Create a managed agent if you need that additional runtime and lifecycle. A model deployment is sufficient for the request shown here. Microsoft Foundry: When Agent Framework Agents Become Managed Resources explains the agent hosting options.
Sources
- Create a Foundry resource in the Azure portal
- Set up Microsoft Foundry resources and projects
- Deploy models in the Foundry portal
- Regional availability for Foundry models
- Deployment types and processing locations
- Azure OpenAI v1 API and endpoint formats
- Azure OpenAI SDK examples and error handling
- Troubleshoot Azure OpenAI HTTP errors
- Microsoft.Extensions.AI.OpenAI package and dependencies
- OpenAI client adapters for IChatClient
- File-based .NET apps
- Download the .NET 11 SDK
來源:DEV Community · dev.to



