# DistributedApplicationEventing Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [DistributedApplicationEventing](/reference/api/csharp/aspire.hosting/distributedapplicationeventing.md)
- Kind: `Methods`
- Members: `5`

Supports publishing and subscribing to events which are executed during the AppHost lifecycle.

## PublishAsync(T, CancellationToken)

- Name: `PublishAsync(T, CancellationToken)`
- Returns: `Task`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/Eventing/DistributedApplicationEventing.cs#L19)

Publishes an event to all subscribes of the specific event type.

```csharp
public class DistributedApplicationEventing
{
    public Task PublishAsync<T>(
        T @event,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `event` (`T`)
  The event.
- `cancellationToken` (`CancellationToken`) `optional`
  A cancellation token.

## Returns

`Task` -- A task that can be awaited.

## PublishAsync(T, EventDispatchBehavior, CancellationToken)

- Name: `PublishAsync(T, EventDispatchBehavior, CancellationToken)`
- Returns: `Task`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/Eventing/DistributedApplicationEventing.cs#L19)

Publishes an event to all subscribes of the specific event type.

```csharp
public class DistributedApplicationEventing
{
    public Task PublishAsync<T>(
        T @event,
        EventDispatchBehavior dispatchBehavior,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `event` (`T`)
  The event.
- `dispatchBehavior` ([EventDispatchBehavior](/reference/api/csharp/aspire.hosting/eventdispatchbehavior.md))
  The dispatch behavior for the event.
- `cancellationToken` (`CancellationToken`) `optional`
  A cancellation token.

## Returns

`Task` -- A task that can be awaited.

## Subscribe(Func<T, CancellationToken, Task>)

- Name: `Subscribe(Func<T, CancellationToken, Task>)`
- Returns: [DistributedApplicationEventSubscription](/reference/api/csharp/aspire.hosting/distributedapplicationeventsubscription.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/Eventing/DistributedApplicationEventing.cs#L79-L102)

Subscribes a callback to a specific event type within the AppHost.

```csharp
public class DistributedApplicationEventing
{
    public DistributedApplicationEventSubscription Subscribe<T>(
        Func<T, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `callback` (`Func<T, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

[DistributedApplicationEventSubscription](/reference/api/csharp/aspire.hosting/distributedapplicationeventsubscription.md) -- A subscription instance which can be used to unsubscribe

## Subscribe(IResource, Func<T, CancellationToken, Task>)

- Name: `Subscribe(IResource, Func<T, CancellationToken, Task>)`
- Returns: [DistributedApplicationEventSubscription](/reference/api/csharp/aspire.hosting/distributedapplicationeventsubscription.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/Eventing/DistributedApplicationEventing.cs#L108-L116)

Subscribes a callback to a specific event type

```csharp
public class DistributedApplicationEventing
{
    public DistributedApplicationEventSubscription Subscribe<T>(
        IResource resource,
        Func<T, CancellationToken, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `resource` ([IResource](/reference/api/csharp/aspire.hosting/iresource.md))
  The resource instance associated with the event.
- `callback` (`Func<T, CancellationToken, Task>`)
  A callback to handle the event.

## Returns

[DistributedApplicationEventSubscription](/reference/api/csharp/aspire.hosting/distributedapplicationeventsubscription.md) -- A subscription instance which can be used to unsubscribe.

## Unsubscribe(DistributedApplicationEventSubscription)

- Name: `Unsubscribe(DistributedApplicationEventSubscription)`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/Eventing/DistributedApplicationEventing.cs#L122-L130)

Unsubscribe from an event.

```csharp
public class DistributedApplicationEventing
{
    public void Unsubscribe(
        DistributedApplicationEventSubscription subscription)
    {
        // ...
    }
}
```

## Parameters

- `subscription` ([DistributedApplicationEventSubscription](/reference/api/csharp/aspire.hosting/distributedapplicationeventsubscription.md))
  The specific subscription to unsubscribe.
