# AzureSearchExtensions Methods

- Package: [Aspire.Hosting.Azure.Search](/reference/api/csharp/aspire.hosting.azure.search.md)
- Type: [AzureSearchExtensions](/reference/api/csharp/aspire.hosting.azure.search/azuresearchextensions.md)
- Kind: `Methods`
- Members: `2`

Provides extension methods for adding the Azure AI Search resources to the application model.

## AddAzureSearch(IDistributedApplicationBuilder, string)

- Name: `AddAzureSearch(IDistributedApplicationBuilder, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureSearchResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Search/AzureSearchExtensions.cs#L38-L47)

Adds an Azure AI Search service resource to the application model.

```csharp
public static class AzureSearchExtensions
{
    public static IResourceBuilder<AzureSearchResource> AddAzureSearch(
        this IDistributedApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The builder for the distributed application.
- `name` (`string`)
  The name of the Azure AI Search resource.

## Returns

`IResourceBuilder<AzureSearchResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

By default references to the Azure AI Search service resource will be assigned the following roles: - `SearchBuiltInRole.SearchIndexDataContributor` - `SearchBuiltInRole.SearchServiceContributor` These can be replaced by calling [AzureSearchExtensions.WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureSearchResource>, SearchBuiltInRole[])](/reference/api/csharp/aspire.hosting.azure.search/azuresearchextensions/methods.md#withroleassignments-iresourcebuilder-t-iresourcebuilder-azuresearchresource-searchbuiltinrole).

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureSearchResource>, SearchBuiltInRole[])

- Name: `WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureSearchResource>, SearchBuiltInRole[])`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Search/AzureSearchExtensions.cs)

Assigns the specified roles to the given resource, granting it the necessary permissions on the target Azure AI Search service resource. This replaces the default role assignments for the resource.

```csharp
public static class AzureSearchExtensions
{
    public static IResourceBuilder<T> WithRoleAssignments<T>(
        this IResourceBuilder<T> builder,
        IResourceBuilder<AzureSearchResource> target,
        params SearchBuiltInRole[] roles)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource to which the specified roles will be assigned.
- `target` (`IResourceBuilder<AzureSearchResource>`)
  The target Azure AI Search service resource.
- `roles` (`SearchBuiltInRole[]`)
  The built-in AI Search roles to be assigned.

## Returns

`IResourceBuilder<T>` -- The updated `ApplicationModel.IResourceBuilder`1` with the applied role assignments.

## Remarks

Assigns the SearchIndexDataReader role to the 'Projects.Api' project.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var search = builder.AddAzureSearch("search");

var api = builder.AddProject<Projects.Api>("api")
  .WithRoleAssignments(search, SearchBuiltInRole.SearchIndexDataReader)
  .WithReference(search);
```

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.
