# HostedAgentResourceBuilderExtensions Methods

- Package: [Aspire.Hosting.Foundry](/reference/api/csharp/aspire.hosting.foundry.md)
- Type: [HostedAgentResourceBuilderExtensions](/reference/api/csharp/aspire.hosting.foundry/hostedagentresourcebuilderextensions.md)
- Kind: `Methods`
- Members: `6`

Extension methods for adding hosted agent applications to the distributed application model.

## AsHostedAgent(IResourceBuilder<T>, Action<HostedAgentConfiguration>)

- Name: `AsHostedAgent(IResourceBuilder<T>, Action<HostedAgentConfiguration>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L27)

In both run and publish modes, build, deploy, and run the containerized agent as a hosted agent in Microsoft Foundry.

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> AsHostedAgent<T>(
        this IResourceBuilder<T> builder,
        Action<HostedAgentConfiguration>? configure = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
- `configure` (`Action<HostedAgentConfiguration>`) `optional`

## Remarks

This overload is not available in polyglot app hosts because run-mode hosted agents are not yet implemented.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AsHostedAgent(IResourceBuilder<T>, IResourceBuilder<AzureCognitiveServicesProjectResource>, Action<HostedAgentConfiguration>)

- Name: `AsHostedAgent(IResourceBuilder<T>, IResourceBuilder<AzureCognitiveServicesProjectResource>, Action<HostedAgentConfiguration>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L39)

In both run and publish modes, build, deploy, and run the containerized agent as a hosted agent in Microsoft Foundry.

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> AsHostedAgent<T>(
        this IResourceBuilder<T> builder,
        IResourceBuilder<AzureCognitiveServicesProjectResource>? project = null,
        Action<HostedAgentConfiguration>? configure = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
- `project` (`IResourceBuilder<AzureCognitiveServicesProjectResource>`) `optional`
- `configure` (`Action<HostedAgentConfiguration>`) `optional`

## Remarks

This overload is not available in polyglot app hosts because run-mode hosted agents are not yet implemented.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## PublishAsHostedAgent(IResourceBuilder<T>, Action<HostedAgentConfiguration>)

- Name: `PublishAsHostedAgent(IResourceBuilder<T>, Action<HostedAgentConfiguration>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L82)

Publish the containerized agent as a hosted agent in Microsoft Foundry. If a project resource is not provided, the method will attempt to find an existing Microsoft Foundry project resource in the application model. If none exists, a new project resource (and its parent account resource) will be created automatically.

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> PublishAsHostedAgent<T>(
        this IResourceBuilder<T> builder,
        Action<HostedAgentConfiguration> configure)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
- `configure` (`Action<HostedAgentConfiguration>`)

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## PublishAsHostedAgent(IResourceBuilder<T>, IResourceBuilder<AzureCognitiveServicesProjectResource>, Action<HostedAgentConfiguration>)

- Name: `PublishAsHostedAgent(IResourceBuilder<T>, IResourceBuilder<AzureCognitiveServicesProjectResource>, Action<HostedAgentConfiguration>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L102-L330)

Publish the containerized agent as a hosted agent in Microsoft Foundry. If a project resource is not provided, the method will attempt to find an existing Microsoft Foundry project resource in the application model. If none exists, a new project resource (and its parent account resource) will be created automatically.

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> PublishAsHostedAgent<T>(
        this IResourceBuilder<T> builder,
        IResourceBuilder<AzureCognitiveServicesProjectResource>? project = null,
        Action<HostedAgentConfiguration>? configure = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
- `project` (`IResourceBuilder<AzureCognitiveServicesProjectResource>`) `optional`
- `configure` (`Action<HostedAgentConfiguration>`) `optional`

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## RunAsHostedAgent(IResourceBuilder<T>, Action<HostedAgentConfiguration>)

- Name: `RunAsHostedAgent(IResourceBuilder<T>, Action<HostedAgentConfiguration>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L51)

In run mode, build, deploy, and run the containerized agent as a hosted agent in Microsoft Foundry.

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> RunAsHostedAgent<T>(
        this IResourceBuilder<T> builder,
        Action<HostedAgentConfiguration> configure)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
- `configure` (`Action<HostedAgentConfiguration>`)

## Remarks

This overload is not available in polyglot app hosts because run-mode hosted agents are not yet implemented.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## RunAsHostedAgent(IResourceBuilder<T>, IResourceBuilder<AzureCognitiveServicesProjectResource>, Action<HostedAgentConfiguration>)

- Name: `RunAsHostedAgent(IResourceBuilder<T>, IResourceBuilder<AzureCognitiveServicesProjectResource>, Action<HostedAgentConfiguration>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L67)

In run mode, build, deploy, and run the containerized agent as a hosted agent in Microsoft Foundry.

```csharp
public static class HostedAgentResourceBuilderExtensions
{
    public static IResourceBuilder<T> RunAsHostedAgent<T>(
        this IResourceBuilder<T> builder,
        IResourceBuilder<AzureCognitiveServicesProjectResource>? project = null,
        Action<HostedAgentConfiguration>? configure = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
- `project` (`IResourceBuilder<AzureCognitiveServicesProjectResource>`) `optional`
- `configure` (`Action<HostedAgentConfiguration>`) `optional`

## Remarks

This overload is not available in polyglot app hosts because run-mode hosted agents are not yet implemented.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.
