# DurableTaskContext Methods

[Type overview](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/)

## CancelAttempt(DurableTaskAttemptCancellationReason) {#cancelattempt-orleans-durable-tasks-durabletaskattemptcancellationreason-0153037f}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/cancelattempt-orleans-durable-tasks-durabletaskattemptcancellationreason-0153037f/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L438-L453)

```csharp
protected internal void CancelAttempt(DurableTaskAttemptCancellationReason reason)
```

Cancels this attempt for `reason`.

### Parameters

- `reason` (`DurableTaskAttemptCancellationReason`): Why the attempt is canceled.

## CreateAttemptCanceledException(Exception?) {#createattemptcanceledexception-system-exception-nullable-687340b1}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/createattemptcanceledexception-system-exception-nullable-687340b1/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L287-L292)

```csharp
protected internal DurableTaskAttemptCanceledException CreateAttemptCanceledException(Exception? innerException)
```

Creates the exception which this canceled attempt's durable operations throw.

### Parameters

- `innerException` (`Exception?`): The exception which the canceled operation threw, if any.

### Exceptions

- `System.InvalidOperationException`: The attempt hasn't been canceled.

## GetVersion(string, int, int) {#getversion-string-int-int-6367a786}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/getversion-string-int-int-6367a786/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L879)

```csharp
protected internal virtual int GetVersion(string name, int minSupported, int maxSupported)
```

Returns the version of the change `name` which this execution follows, deciding and recording it at the execution's first call for the name.

### Parameters

- `name` (`string`): The change's validated name.
- `minSupported` (`int`): The oldest version which the calling code supports, at least `0`.
- `maxSupported` (`int`): The newest version, at least `minSupported`.

### Returns

The execution's version, from `minSupported` to `maxSupported`.

## OnBodyCompleted(DurableTaskResponse) {#onbodycompleted-orleans-durable-tasks-durabletaskresponse-2e7b5aed}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/onbodycompleted-orleans-durable-tasks-durabletaskresponse-2e7b5aed/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L563)

```csharp
protected internal virtual void OnBodyCompleted(DurableTaskResponse response)
```

Receives this execution's terminal response when its body completes, synchronously with the body's final code.

### Parameters

- `response` (`DurableTaskResponse`): The body's terminal response.

## PostTask(TaskId, DurableTask, DurableTaskStartOptions) {#posttask-orleans-durable-tasks-taskid-orleans-durable-tasks-durabletask-orleans-280313ff}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/posttask-orleans-durable-tasks-taskid-orleans-durable-tasks-durabletask-orleans-280313ff/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L535)

```csharp
protected internal virtual IScheduledTaskHandle PostTask(TaskId taskId, DurableTask taskDefinition, DurableTaskStartOptions options)
```

Stages the start of `taskDefinition` under `taskId` and records it in this execution, synchronously.

### Parameters

- `taskId` (`TaskId`): A child of [DurableTaskContext](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/) for an attached task, or the root identifier of a [DurableTaskStartOptions](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskstartoptions/) task.
- `taskDefinition` (`DurableTask`): The definition to start.
- `options` (`DurableTaskStartOptions`): [DurableTaskStartOptions](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskstartoptions/) or [DurableTaskStartOptions](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskstartoptions/).

### Returns

A handle through which this execution observes the task.

## RequestTaskCancellation(string?) {#requesttaskcancellation-string-nullable-e5ed6a21}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/requesttaskcancellation-string-nullable-e5ed6a21/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L416-L425)

```csharp
protected internal void RequestTaskCancellation(string? reason)
```

Requests durable cancellation of the task which this context runs, carrying `reason`.

### Parameters

- `reason` (`string?`): The reason which [DurableTaskContext](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/) reports, or `null` for none. The first request determines the reason, so a host which replays a canceled task passes the reason it recorded with the task's cancellation.

## RunEffect(TaskId, TState, Func&lt;TState, DurableTaskResponse&gt;) {#runeffect-1-orleans-durable-tasks-taskid-tstate-system-func-tstate-orleans-durab-75beacc0}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/runeffect-1-orleans-durable-tasks-taskid-tstate-system-func-tstate-orleans-durab-75beacc0/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L853)

```csharp
protected internal virtual DurableTaskResponse RunEffect<TState>(TaskId effectId, TState state, Func<TState, DurableTaskResponse> effect)
```

Returns the outcome recorded for the effect `effectId`, or runs the effect and records its outcome.

### Parameters

- `effectId` (`TaskId`): The effect's identifier, a child of this execution.
- `state` (`TState`): The state which `effect` receives.
- `effect` (`Func<TState, DurableTaskResponse>`): Runs the effect and returns its outcome: its result, or the exception which it threw. It doesn't throw.

### Returns

The effect's outcome, which the host recorded.

## ScheduleDelayAsync(TaskId, DateTimeOffset, CancellationToken) {#scheduledelayasync-orleans-durable-tasks-taskid-system-datetimeoffset-system-thr-8b3723ce}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/scheduledelayasync-orleans-durable-tasks-taskid-system-datetimeoffset-system-thr-8b3723ce/)

```csharp
protected internal abstract ValueTask<DurableTaskResponse> ScheduleDelayAsync(TaskId taskId, DateTimeOffset dueTime, CancellationToken cancellationToken)
```

Schedules a durable delay at `dueTime`.

### Parameters

- `taskId` (`TaskId`)
- `dueTime` (`DateTimeOffset`)
- `cancellationToken` (`CancellationToken`)

## SelectCompletionAsync(TaskId, IReadOnlyList&lt;TaskId&gt;, CancellationToken) {#selectcompletionasync-orleans-durable-tasks-taskid-system-collections-generic-ir-fe215518}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/selectcompletionasync-orleans-durable-tasks-taskid-system-collections-generic-ir-fe215518/)

```csharp
protected internal abstract ValueTask<TaskId> SelectCompletionAsync(TaskId decisionId, IReadOnlyList<TaskId> candidates, CancellationToken cancellationToken)
```

Returns a completed candidate, and returns the same candidate for every call with the same `decisionId`, including calls during replay.

### Parameters

- `decisionId` (`TaskId`)
- `candidates` (`IReadOnlyList<TaskId>`)
- `cancellationToken` (`CancellationToken`)

## StartTaskAsync(TaskId, DurableTask, DurableTaskStartOptions, CancellationToken) {#starttaskasync-orleans-durable-tasks-taskid-orleans-durable-tasks-durabletask-or-f1b3e8f2}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/starttaskasync-orleans-durable-tasks-taskid-orleans-durable-tasks-durabletask-or-f1b3e8f2/)

```csharp
protected internal abstract ValueTask<IScheduledTaskHandle> StartTaskAsync(TaskId taskId, DurableTask taskDefinition, DurableTaskStartOptions options, CancellationToken cancellationToken)
```

Starts, or reattaches to, `taskDefinition` under `taskId`, and records the start in this execution.

### Parameters

- `taskId` (`TaskId`): A child of [DurableTaskContext](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/) for an attached task, or the root identifier of a [DurableTaskStartOptions](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskstartoptions/) task.
- `taskDefinition` (`DurableTask`): The definition to start.
- `options` (`DurableTaskStartOptions`): How the task relates to this execution, and when the start completes.
- `cancellationToken` (`CancellationToken`): A token which abandons the start. It doesn't cancel the task.

### Returns

A handle through which this execution observes the task.

## TryGetCurrent(DurableTaskContext?) {#trygetcurrent-out-orleans-durable-tasks-durabletaskcontext-nullable-e2be543b}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/trygetcurrent-out-orleans-durable-tasks-durabletaskcontext-nullable-e2be543b/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L84-L85)

```csharp
public static bool TryGetCurrent(out DurableTaskContext? context)
```

Gets the context of the durable task which the calling code runs in, if any.

### Parameters

- `context` (`DurableTaskContext?`): The running task's context, or `null` outside a durable task.

### Returns

`true` when the calling code runs in a durable task.

## WaitForSignalAsync(string, CancellationToken) {#waitforsignalasync-string-system-threading-cancellationtoken-4117bac4}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/methods/waitforsignalasync-string-system-threading-cancellationtoken-4117bac4/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/DurableTaskContext.cs#L578)

```csharp
protected internal virtual ValueTask<DurableTaskResponse> WaitForSignalAsync(string name, CancellationToken cancellationToken)
```

Waits for the signal named `name` of the invoked task whose execution contains this execution, and returns a successful response holding the signal's recorded value.

### Parameters

- `name` (`string`)
- `cancellationToken` (`CancellationToken`)
