# AzureWebPubSubExtensions Methods

- Package: [Aspire.Hosting.Azure.WebPubSub](/reference/api/csharp/aspire.hosting.azure.webpubsub.md)
- Type: [AzureWebPubSubExtensions](/reference/api/csharp/aspire.hosting.azure.webpubsub/azurewebpubsubextensions.md)
- Kind: `Methods`
- Members: `6`

Provides extension methods for adding the Azure Web PubSub resources to the application model.

## AddAzureWebPubSub(IDistributedApplicationBuilder, string)

- Name: `AddAzureWebPubSub(IDistributedApplicationBuilder, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureWebPubSubResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.WebPubSub/AzureWebPubSubExtensions.cs#L39-L153)

Adds an Azure Web PubSub resource to the application model. Change sku: WithParameter("sku", "Standard_S1") Change capacity: WithParameter("capacity", 2)

```csharp
public static class AzureWebPubSubExtensions
{
    public static IResourceBuilder<AzureWebPubSubResource> AddAzureWebPubSub(
        this IDistributedApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.

## Returns

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

## Remarks

By default references to the Azure Web PubSub resource will be assigned the following roles: - `WebPubSubBuiltInRole.WebPubSubServiceOwner` These can be replaced by calling [AzureWebPubSubExtensions.WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureWebPubSubResource>, WebPubSubBuiltInRole[])](/reference/api/csharp/aspire.hosting.azure.webpubsub/azurewebpubsubextensions/methods.md#withroleassignments-iresourcebuilder-t-iresourcebuilder-azurewebpubsubresource-webpubsubbuiltinrole).

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddEventHandler(IResourceBuilder<AzureWebPubSubHubResource>, ExpressionInterpolatedStringHandler, string, string[]?, UpstreamAuthSettings?)

- Name: `AddEventHandler(IResourceBuilder<AzureWebPubSubHubResource>, ExpressionInterpolatedStringHandler, string, string[]?, UpstreamAuthSettings?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureWebPubSubHubResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.WebPubSub/AzureWebPubSubExtensions.cs#L218-L222)

Add event handler setting with expression

```csharp
public static class AzureWebPubSubExtensions
{
    public static IResourceBuilder<AzureWebPubSubHubResource> AddEventHandler(
        this IResourceBuilder<AzureWebPubSubHubResource> builder,
        ExpressionInterpolatedStringHandler urlTemplateExpression,
        string userEventPattern = "*",
        string[]? systemEvents = null,
        UpstreamAuthSettings? authSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureWebPubSubHubResource>`)
  The builder for a Web PubSub hub.
- `urlTemplateExpression` (`ExpressionInterpolatedStringHandler`)
  The expression to evaluate the URL template configured for the event handler.
- `userEventPattern` (`string`) `optional`
  The user event pattern for the event handler.
- `systemEvents` (`string[]?`) `optional`
  The system events for the event handler.
- `authSettings` (`UpstreamAuthSettings?`) `optional`
  The auth settings configured for the event handler.

## Remarks

This overload is not available in polyglot app hosts. Configure Web PubSub event handlers without auth settings in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AddEventHandler(IResourceBuilder<AzureWebPubSubHubResource>, ReferenceExpression, string, string[]?, UpstreamAuthSettings?)

- Name: `AddEventHandler(IResourceBuilder<AzureWebPubSubHubResource>, ReferenceExpression, string, string[]?, UpstreamAuthSettings?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureWebPubSubHubResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.WebPubSub/AzureWebPubSubExtensions.cs#L245-L250)

Add event handler setting with expression

```csharp
public static class AzureWebPubSubExtensions
{
    public static IResourceBuilder<AzureWebPubSubHubResource> AddEventHandler(
        this IResourceBuilder<AzureWebPubSubHubResource> builder,
        ReferenceExpression urlExpression,
        string userEventPattern = "*",
        string[]? systemEvents = null,
        UpstreamAuthSettings? authSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureWebPubSubHubResource>`)
  The builder for a Web PubSub hub.
- `urlExpression` (`ReferenceExpression`)
  The expression to evaluate the URL template configured for the event handler.
- `userEventPattern` (`string`) `optional`
  The user event pattern for the event handler.
- `systemEvents` (`string[]?`) `optional`
  The system events for the event handler.
- `authSettings` (`UpstreamAuthSettings?`) `optional`
  The auth settings configured for the event handler.

## Remarks

This overload is not available in polyglot app hosts. Configure Web PubSub event handlers without auth settings in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AddHub(IResourceBuilder<AzureWebPubSubResource>, string)

- Name: `AddHub(IResourceBuilder<AzureWebPubSubResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureWebPubSubHubResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.WebPubSub/AzureWebPubSubExtensions.cs#L166)

Add hub settings

```csharp
public static class AzureWebPubSubExtensions
{
    public static IResourceBuilder<AzureWebPubSubHubResource> AddHub(
        this IResourceBuilder<AzureWebPubSubResource> builder,
        string hubName)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureWebPubSubResource>`)
  The builder for the distributed application.
- `hubName` (`string`)
  The hub name. Hub name is case-insensitive.

## Remarks

This overload is not available in polyglot app hosts. Use the named hub overload instead.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AddHub(IResourceBuilder<AzureWebPubSubResource>, string, string?)

- Name: `AddHub(IResourceBuilder<AzureWebPubSubResource>, string, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureWebPubSubHubResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.WebPubSub/AzureWebPubSubExtensions.cs#L180-L195)

Adds an Azure Web Pub Sub hub resource to the application model.

```csharp
public static class AzureWebPubSubExtensions
{
    public static IResourceBuilder<AzureWebPubSubHubResource> AddHub(
        this IResourceBuilder<AzureWebPubSubResource> builder,
        string name,
        string? hubName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureWebPubSubResource>`)
  The Azure WebPubSub resource builder.
- `name` (`string`)
  The name of the Azure WebPubSub Hub resource.
- `hubName` (`string?`) `optional`
  The name of the Azure WebPubSub Hub. If not provided, this defaults to the same value as `name`.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureWebPubSubResource>, WebPubSubBuiltInRole[])

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

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

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

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource to which the specified roles will be assigned.
- `target` (`IResourceBuilder<AzureWebPubSubResource>`)
  The target Azure Web PubSub resource.
- `roles` (`WebPubSubBuiltInRole[]`)
  The built-in Web PubSub roles to be assigned.

## Returns

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

## Remarks

This overload is not available in polyglot app hosts. Use [AzureWebPubSubExtensions.WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureWebPubSubResource>, WebPubSubBuiltInRole[])](/reference/api/csharp/aspire.hosting.azure.webpubsub/azurewebpubsubextensions/methods.md#withroleassignments-iresourcebuilder-t-iresourcebuilder-azurewebpubsubresource-webpubsubbuiltinrole) instead. Assigns the WebPubSubServiceReader role to the 'Projects.Api' project.

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

var webPubSub = builder.AddAzureWebPubSub("webPubSub");

var api = builder.AddProject<Projects.Api>("api")
  .WithRoleAssignments(webPubSub, WebPubSubBuiltInRole.WebPubSubServiceReader)
  .WithReference(webPubSub);
```

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.
