Watch Aspire live streamsDocsTry Aspire

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)
{
// ...
}
}
builderIResourceBuilder<FoundryResource>The Microsoft Foundry resource builder.
namestringThe name of the Microsoft Foundry Deployment resource.
modelNamestringThe name of the model to deploy.
modelVersionstringThe version of the model to deploy.
formatstringThe format of the model to deploy.
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)
{
// ...
}
}
builderIResourceBuilder<FoundryResource>The Microsoft Foundry resource builder.
namestringThe name of the Microsoft Foundry Deployment resource.
modelFoundryModelThe model descriptor, using the FoundryModel class like so:
aiFoundry.AddDeployment(name: "chat", model: FoundryModel.OpenAI.Gpt5Mini)
IResourceBuilder<FoundryDeploymentResource>A reference to the ApplicationModel.IResourceBuilder`1.
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)
{
// ...
}
}
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.
namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.
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)
{
// ...
}
}
builderIResourceBuilder<FoundryResource>The Microsoft Foundry resource builder.
IResourceBuilder<FoundryResource>The configured Microsoft Foundry resource builder.
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)
{
// ...
}
}
builderIResourceBuilder<FoundryResource>The Microsoft Foundry resource builder.
endpointstring?optionalThe endpoint of an existing Foundry Local service, or null for an Aspire-managed service.
IResourceBuilder<FoundryResource>The configured Microsoft Foundry resource builder.
ArgumentNullExceptionThrown when builder is null.
ArgumentExceptionThrown when endpoint is not an absolute HTTP or HTTPS URL.
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.

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)
{
// ...
}
}
builderIResourceBuilder<FoundryDeploymentResource>The Microsoft Foundry Deployment resource builder.
configureAction<FoundryDeploymentResource>A method that can be used for customizing the FoundryDeploymentResource.
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)
{
// ...
}
}
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.
IResourceBuilder<T>The updated ApplicationModel.IResourceBuilder`1 with the applied role assignments.
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);