FoundryExtensions Methods
ClassMethods7 members
Provides extension methods for adding the Microsoft Foundry resources to the application model.
AddDeployment(IResourceBuilder<FoundryResource>, string, string, string, string) Section titled AddDeployment(IResourceBuilder<FoundryResource>, string, string, string, string)extensionIResourceBuilder<FoundryDeploymentResource> Adds and returns a Microsoft Foundry Deployment resource (e.g. an AI model) to the application model.
public static class FoundryExtensions{ public static IResourceBuilder<FoundryDeploymentResource> AddDeployment( this IResourceBuilder<FoundryResource> builder, string name, string modelName, string modelVersion, string format) { // ... }}Parameters
builderIResourceBuilder<FoundryResource> The Microsoft Foundry resource builder.namestringThe name of the Microsoft Foundry Deployment resource.modelName stringThe name of the model to deploy.modelVersion stringThe version of the model to deploy.formatstringThe format of the model to deploy.Returns
IResourceBuilder<FoundryDeploymentResource> A reference to the ApplicationModel.IResourceBuilder`1 .AddDeployment(IResourceBuilder<FoundryResource>, string, FoundryModel) Section titled AddDeployment(IResourceBuilder<FoundryResource>, string, FoundryModel)extensionIResourceBuilder<FoundryDeploymentResource> Adds and returns a Microsoft Foundry Deployment resource to the application model using a
FoundryModel . public static class FoundryExtensions{ public static IResourceBuilder<FoundryDeploymentResource> AddDeployment( this IResourceBuilder<FoundryResource> builder, string name, FoundryModel model) { // ... }}Parameters
builderIResourceBuilder<FoundryResource> The Microsoft Foundry resource builder.namestringThe name of the Microsoft Foundry Deployment resource.modelFoundryFoundryModel class like so: aiFoundry.AddDeployment(name: "chat", model: FoundryModel.OpenAI.Gpt5Mini)Returns
IResourceBuilder<FoundryDeploymentResource> A reference to the ApplicationModel.IResourceBuilder`1 .Remarks
Create a deployment for the OpenAI GTP-5-mini model:
var builder = DistributedApplication.CreateBuilder(args);
var aiFoundry = builder.AddFoundry("aiFoundry");var gpt5mini = aiFoundry.AddDeployment("chat", FoundryModel.OpenAI.Gpt5Mini);AddFoundry(IDistributedApplicationBuilder, string) Section titled AddFoundry(IDistributedApplicationBuilder, string)extensionIResourceBuilder<FoundryResource> Adds a Microsoft Foundry resource to the application model.
public static class FoundryExtensions{ public static IResourceBuilder<FoundryResource> AddFoundry( this IDistributedApplicationBuilder builder, string name) { // ... }}Parameters
builderIDistributedApplicationBuilder The Hosting.IDistributedApplicationBuilder .namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.Returns
IResourceBuilder<FoundryResource> A reference to the ApplicationModel.IResourceBuilder`1 .RunAsFoundryLocal(IResourceBuilder<FoundryResource>) Section titled RunAsFoundryLocal(IResourceBuilder<FoundryResource>)extensionIResourceBuilder<FoundryResource> Configures a Microsoft Foundry resource to use an Aspire-managed Foundry Local service.
public static class FoundryExtensions{ public static IResourceBuilder<FoundryResource> RunAsFoundryLocal( this IResourceBuilder<FoundryResource> builder) { // ... }}Parameters
builderIResourceBuilder<FoundryResource> The Microsoft Foundry resource builder.Returns
IResourceBuilder<FoundryResource> The configured Microsoft Foundry resource builder.Exceptions
ArgumentNullExceptionThrown when builder is null.RunAsFoundryLocal(IResourceBuilder<FoundryResource>, string?) Section titled RunAsFoundryLocal(IResourceBuilder<FoundryResource>, string?)extensionIResourceBuilder<FoundryResource> Configures a Microsoft Foundry resource to use an Aspire-managed or existing Foundry Local service.
public static class FoundryExtensions{ public static IResourceBuilder<FoundryResource> RunAsFoundryLocal( this IResourceBuilder<FoundryResource> builder, string? endpoint = null) { // ... }}Parameters
builderIResourceBuilder<FoundryResource> The Microsoft Foundry resource builder.endpointstring?optionalThe endpoint of an existing Foundry Local service, or null for an Aspire-managed service.Returns
IResourceBuilder<FoundryResource> The configured Microsoft Foundry resource builder.Exceptions
ArgumentNullExceptionThrown when builder is null.ArgumentExceptionThrown when endpoint is not an absolute HTTP or HTTPS URL.Remarks
When
endpoint is provided, Aspire connects to that existing Foundry Local service without starting, stopping, downloading, or loading anything on its host. Models configured on the resource must already be loaded by the existing service. Examples
Connect to an existing Foundry Local service and identify the model that is already loaded:
var foundry = builder.AddFoundry("foundry") .RunAsFoundryLocal("http://windows-host:5273");
var chat = foundry.AddDeployment("chat", FoundryModel.Local.Phi4Mini) .WithProperties(deployment => { deployment.LocalModelId = "Phi-4-mini-instruct-generic-gpu:5"; });WithProperties(IResourceBuilder<FoundryDeploymentResource>, Action<FoundryDeploymentResource>) Section titled WithProperties(IResourceBuilder<FoundryDeploymentResource>, Action<FoundryDeploymentResource>)extensionIResourceBuilder<FoundryDeploymentResource> Allows setting the properties of a Microsoft Foundry Deployment resource.
public static class FoundryExtensions{ public static IResourceBuilder<FoundryDeploymentResource> WithProperties( this IResourceBuilder<FoundryDeploymentResource> builder, Action<FoundryDeploymentResource> configure) { // ... }}Parameters
builderIResourceBuilder<FoundryDeploymentResource> The Microsoft Foundry Deployment resource builder.configureAction<FoundryDeploymentResource> A method that can be used for customizing the FoundryDeploymentResource .Returns
IResourceBuilder<FoundryDeploymentResource> A reference to the ApplicationModel.IResourceBuilder`1 .WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<FoundryResource>, CognitiveServicesBuiltInRole[]) Section titled WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<FoundryResource>, CognitiveServicesBuiltInRole[])extensionIResourceBuilder<T> Assigns the specified roles to the given resource, granting it the necessary permissions on the target Microsoft Foundry resource. This replaces the default role assignments for the resource.
public static class FoundryExtensions{ public static IResourceBuilder<T> WithRoleAssignments<T>( this IResourceBuilder<T> builder, IResourceBuilder<FoundryResource> target, params CognitiveServicesBuiltInRole[] roles) { // ... }}Parameters
builderIResourceBuilder<T> The resource to which the specified roles will be assigned.targetIResourceBuilder<FoundryResource> The target Microsoft Foundry resource.rolesCognitiveServicesBuiltInRole[] The built-in Cognitive Services roles to be assigned.Returns
IResourceBuilder<T> The updated ApplicationModel.IResourceBuilder`1 with the applied role assignments.Remarks
Assigns the CognitiveServicesOpenAIContributor role to the 'Projects.Api' project.
var builder = DistributedApplication.CreateBuilder(args);
var aiFoundry = builder.AddFoundry("aiFoundry");
var api = builder.AddProject<Projects.Api>("api") .WithRoleAssignments( aiFoundry, CognitiveServicesBuiltInRole.CognitiveServicesOpenAIContributor) .WithReference(aiFoundry);