# DistributedApplicationEventingExtensions Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [DistributedApplicationEventingExtensions](/reference/api/csharp/aspire.hosting/distributedapplicationeventingextensions.md)
- Kind: `Methods`
- Members: `9`

Provides extension methods for subscribing to [IDistributedApplicationEvent](/reference/api/csharp/aspire.hosting/idistributedapplicationevent.md) and [IDistributedApplicationResourceEvent](/reference/api/csharp/aspire.hosting/idistributedapplicationresourceevent.md) events.

## OnAfterPublish(T, Func<AfterPublishEvent, CancellationToken, Task>)

- Name: `OnAfterPublish(T, Func<AfterPublishEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: `T`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L49)

Subscribes a callback to the [AfterPublishEvent](/reference/api/csharp/aspire.hosting/afterpublishevent.md) event within the AppHost.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static T OnAfterPublish<T>(
        this T builder,
        Func<AfterPublishEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`T`)
  The distributed application builder.
- `callback` (`Func<AfterPublishEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

`T` -- The `builder` for chaining.

## Remarks

If you need to ensure you only subscribe to the event once, see [IDistributedApplicationEventingSubscriber](/reference/api/csharp/aspire.hosting/idistributedapplicationeventingsubscriber.md).

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.

## OnBeforePublish(T, Func<BeforePublishEvent, CancellationToken, Task>)

- Name: `OnBeforePublish(T, Func<BeforePublishEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: `T`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L37)

Subscribes a callback to the [BeforePublishEvent](/reference/api/csharp/aspire.hosting/beforepublishevent.md) event within the AppHost.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static T OnBeforePublish<T>(
        this T builder,
        Func<BeforePublishEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`T`)
  The distributed application builder.
- `callback` (`Func<BeforePublishEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

`T` -- The `builder` for chaining.

## Remarks

If you need to ensure you only subscribe to the event once, see [IDistributedApplicationEventingSubscriber](/reference/api/csharp/aspire.hosting/idistributedapplicationeventingsubscriber.md).

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.

## OnBeforeResourceStarted(IResourceBuilder<T>, Func<T, BeforeResourceStartedEvent, CancellationToken, Task>)

- Name: `OnBeforeResourceStarted(IResourceBuilder<T>, Func<T, BeforeResourceStartedEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L62)

Subscribes a callback to the [BeforeResourceStartedEvent](/reference/api/csharp/aspire.hosting/beforeresourcestartedevent.md) event within the AppHost.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static IResourceBuilder<T> OnBeforeResourceStarted<T>(
        this IResourceBuilder<T> builder,
        Func<T, BeforeResourceStartedEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `callback` (`Func<T, BeforeResourceStartedEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

This method is not available in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.

## OnBeforeStart(T, Func<BeforeStartEvent, CancellationToken, Task>)

- Name: `OnBeforeStart(T, Func<BeforeStartEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: `T`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L25)

Subscribes a callback to the [BeforeStartEvent](/reference/api/csharp/aspire.hosting/beforestartevent.md) event within the AppHost.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static T OnBeforeStart<T>(
        this T builder,
        Func<BeforeStartEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`T`)
  The distributed application builder.
- `callback` (`Func<BeforeStartEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

`T` -- The `builder` for chaining.

## Remarks

If you need to ensure you only subscribe to the event once, see [IDistributedApplicationEventingSubscriber](/reference/api/csharp/aspire.hosting/idistributedapplicationeventingsubscriber.md).

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.

## OnConnectionStringAvailable(IResourceBuilder<T>, Func<T, ConnectionStringAvailableEvent, CancellationToken, Task>)

- Name: `OnConnectionStringAvailable(IResourceBuilder<T>, Func<T, ConnectionStringAvailableEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L88)

Subscribes a callback to the [ConnectionStringAvailableEvent](/reference/api/csharp/aspire.hosting/connectionstringavailableevent.md) event for `builder`.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static IResourceBuilder<T> OnConnectionStringAvailable<T>(
        this IResourceBuilder<T> builder,
        Func<T, ConnectionStringAvailableEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `callback` (`Func<T, ConnectionStringAvailableEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

This method is not available in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.

## OnInitializeResource(IResourceBuilder<T>, Func<T, InitializeResourceEvent, CancellationToken, Task>)

- Name: `OnInitializeResource(IResourceBuilder<T>, Func<T, InitializeResourceEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L101)

Subscribes a callback to the [InitializeResourceEvent](/reference/api/csharp/aspire.hosting/initializeresourceevent.md) event for `builder`.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static IResourceBuilder<T> OnInitializeResource<T>(
        this IResourceBuilder<T> builder,
        Func<T, InitializeResourceEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `callback` (`Func<T, InitializeResourceEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

This method is not available in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.

## OnResourceEndpointsAllocated(IResourceBuilder<T>, Func<T, ResourceEndpointsAllocatedEvent, CancellationToken, Task>)

- Name: `OnResourceEndpointsAllocated(IResourceBuilder<T>, Func<T, ResourceEndpointsAllocatedEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L114)

Subscribes a callback to the [ResourceEndpointsAllocatedEvent](/reference/api/csharp/aspire.hosting/resourceendpointsallocatedevent.md) event for `builder`.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static IResourceBuilder<T> OnResourceEndpointsAllocated<T>(
        this IResourceBuilder<T> builder,
        Func<T, ResourceEndpointsAllocatedEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `callback` (`Func<T, ResourceEndpointsAllocatedEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

This method is not available in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.

## OnResourceReady(IResourceBuilder<T>, Func<T, ResourceReadyEvent, CancellationToken, Task>)

- Name: `OnResourceReady(IResourceBuilder<T>, Func<T, ResourceReadyEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L127)

Subscribes a callback to the [ResourceReadyEvent](/reference/api/csharp/aspire.hosting/resourcereadyevent.md) event for `builder`.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static IResourceBuilder<T> OnResourceReady<T>(
        this IResourceBuilder<T> builder,
        Func<T, ResourceReadyEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `callback` (`Func<T, ResourceReadyEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

This method is not available in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.

## OnResourceStopped(IResourceBuilder<T>, Func<T, ResourceStoppedEvent, CancellationToken, Task>)

- Name: `OnResourceStopped(IResourceBuilder<T>, Func<T, ResourceStoppedEvent, CancellationToken, Task>)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs#L75)

Subscribes a callback to the [ResourceStoppedEvent](/reference/api/csharp/aspire.hosting/resourcestoppedevent.md) event for `builder`.

```csharp
public static class DistributedApplicationEventingExtensions
{
    public static IResourceBuilder<T> OnResourceStopped<T>(
        this IResourceBuilder<T> builder,
        Func<T, ResourceStoppedEvent, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `callback` (`Func<T, ResourceStoppedEvent, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

This method is not available in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Reason: Complex generic delegates with event/CancellationToken types -- not ATS-compatible.
