# LifecycleExtensions Methods

[Type overview](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.lifecycleextensions/)

## Subscribe(ILifecycleObservable, int, ILifecycleObserver) {#subscribe-this-orleans-ilifecycleobservable-int-orleans-ilifecycleobserver-38299ba9}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.lifecycleextensions/methods/subscribe-this-orleans-ilifecycleobservable-int-orleans-ilifecycleobserver-38299ba9/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Lifecycle/LifecycleExtensions.cs)

```csharp
public static IDisposable Subscribe(this ILifecycleObservable observable, int stage, ILifecycleObserver observer)
```

Creates a disposable subscription to the lifecycle.

### Parameters

- `observable` (`ILifecycleObservable`): The lifecycle observable.
- `stage` (`int`): The stage to participate in.
- `observer` (`ILifecycleObserver`): The observer.

### Returns

A `System.IDisposable` instance which can be disposed to unsubscribe the observer from the lifecycle.

## Subscribe(ILifecycleObservable, string, int, Func&lt;CancellationToken, Task&gt;) {#subscribe-this-orleans-ilifecycleobservable-string-int-system-func-system-thread-d525f4d6}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.lifecycleextensions/methods/subscribe-this-orleans-ilifecycleobservable-string-int-system-func-system-thread-d525f4d6/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Lifecycle/LifecycleExtensions.cs#L51)

```csharp
public static IDisposable Subscribe(this ILifecycleObservable observable, string observerName, int stage, Func<CancellationToken, Task> onStart)
```

Creates a disposable subscription to the lifecycle.

### Parameters

- `observable` (`ILifecycleObservable`): The lifecycle observable.
- `observerName` (`string`): The name of the observer.
- `stage` (`int`): The stage to participate in.
- `onStart` (`Func<CancellationToken, Task>`): The delegate called when starting the specified lifecycle stage.

### Returns

A `System.IDisposable` instance which can be disposed to unsubscribe the observer from the lifecycle.

## Subscribe(ILifecycleObservable, string, int, Func&lt;CancellationToken, Task&gt;, Func&lt;CancellationToken, Task&gt;) {#subscribe-this-orleans-ilifecycleobservable-string-int-system-func-system-thread-1f67a09b}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.lifecycleextensions/methods/subscribe-this-orleans-ilifecycleobservable-string-int-system-func-system-thread-1f67a09b/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Lifecycle/LifecycleExtensions.cs#L23-L38)

```csharp
public static IDisposable Subscribe(this ILifecycleObservable observable, string observerName, int stage, Func<CancellationToken, Task> onStart, Func<CancellationToken, Task>? onStop)
```

Creates a disposable subscription to the lifecycle.

### Parameters

- `observable` (`ILifecycleObservable`): The lifecycle observable.
- `observerName` (`string`): The name of the observer.
- `stage` (`int`): The stage to participate in.
- `onStart` (`Func<CancellationToken, Task>`): The delegate called when starting the specified lifecycle stage.
- `onStop` (`Func<CancellationToken, Task>`): The delegate to be called when stopping the specified lifecycle stage.

### Returns

A `System.IDisposable` instance which can be disposed to unsubscribe the observer from the lifecycle.

## Subscribe(ILifecycleObservable, int, Func&lt;CancellationToken, Task&gt;) {#subscribe-1-this-orleans-ilifecycleobservable-int-system-func-system-threading-c-bfe7d6ae}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.lifecycleextensions/methods/subscribe-1-this-orleans-ilifecycleobservable-int-system-func-system-threading-c-bfe7d6ae/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Lifecycle/LifecycleExtensions.cs#L97)

```csharp
public static IDisposable Subscribe<TObserver>(this ILifecycleObservable observable, int stage, Func<CancellationToken, Task> onStart)
```

Creates a disposable subscription to the lifecycle.

### Parameters

- `observable` (`ILifecycleObservable`): The lifecycle observable.
- `stage` (`int`): The stage to participate in.
- `onStart` (`Func<CancellationToken, Task>`): The delegate called when starting the specified lifecycle stage.

### Returns

A `System.IDisposable` instance which can be disposed to unsubscribe the observer from the lifecycle.

## Subscribe(ILifecycleObservable, int, Func&lt;CancellationToken, Task&gt;, Func&lt;CancellationToken, Task&gt;) {#subscribe-1-this-orleans-ilifecycleobservable-int-system-func-system-threading-c-d612e78a}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.lifecycleextensions/methods/subscribe-1-this-orleans-ilifecycleobservable-int-system-func-system-threading-c-d612e78a/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Lifecycle/LifecycleExtensions.cs#L82)

```csharp
public static IDisposable Subscribe<TObserver>(this ILifecycleObservable observable, int stage, Func<CancellationToken, Task> onStart, Func<CancellationToken, Task> onStop)
```

Creates a disposable subscription to the lifecycle.

### Parameters

- `observable` (`ILifecycleObservable`): The lifecycle observable.
- `stage` (`int`): The stage to participate in.
- `onStart` (`Func<CancellationToken, Task>`): The delegate called when starting the specified lifecycle stage.
- `onStop` (`Func<CancellationToken, Task>`): Teh delegate to be called when stopping the specified lifecycle stage.

### Returns

A `System.IDisposable` instance which can be disposed to unsubscribe the observer from the lifecycle.

## Subscribe(ILifecycleObservable, int, ILifecycleObserver) {#subscribe-1-this-orleans-ilifecycleobservable-int-orleans-ilifecycleobserver-9924b5af}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.lifecycleextensions/methods/subscribe-1-this-orleans-ilifecycleobservable-int-orleans-ilifecycleobserver-9924b5af/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Lifecycle/LifecycleExtensions.cs)

```csharp
public static IDisposable Subscribe<TObserver>(this ILifecycleObservable observable, int stage, ILifecycleObserver observer)
```

Creates a disposable subscription to the lifecycle.

### Parameters

- `observable` (`ILifecycleObservable`): The lifecycle observable.
- `stage` (`int`): The stage to participate in.
- `observer` (`ILifecycleObserver`): The observer.

### Returns

A `System.IDisposable` instance which can be disposed to unsubscribe the observer from the lifecycle.
