-
Notifications
You must be signed in to change notification settings - Fork 6
[.NET] Add a Functions-native API for invoking Durable Workflows #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Chris Gillum (cgillum)
merged 9 commits into
main
from
cgillum-microsoft-reimagined-journey
Jul 28, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9108d31
Add a Functions-native API for invoking Durable Workflows
cgillum 93bc8a8
Dispose ServiceProvider instances in AsWorkflowClient tests
cgillum 41ba086
Correct ResolveWorkflow exception documentation
cgillum ab681b8
Address review: broaden not-registered guidance and correct returns doc
cgillum 5af8c15
Fix workflow result deserialization under the Azure Functions host
cgillum d6327ac
Merge latest from main
cgillum 8dc21eb
Correct host serialization test fixture
cgillum 22ec1d8
Merge latest from main
cgillum e4c1725
Adapt workflow tests to bulk API removal
cgillum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
dotnet/samples/DurableWorkflows/AzureFunctions/01_SequentialWorkflow/OrderFunctions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Microsoft.Agents.AI.DurableTask.Workflows; | ||
| using Microsoft.Agents.AI.Hosting.AzureFunctions; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.DurableTask.Client; | ||
|
|
||
| namespace SequentialWorkflow; | ||
|
|
||
| /// <summary> | ||
| /// Demonstrates invoking a registered workflow from your own function code. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These functions are hand-written endpoints that sit in front of the <c>CancelOrder</c> workflow, | ||
| /// rather than the endpoints the framework generates. Instead of creating an <see cref="HttpClient"/> | ||
| /// and POSTing to <c>workflows/CancelOrder/run</c>, they get an <see cref="IWorkflowClient"/> from the | ||
| /// <c>[DurableClient]</c> binding and start the workflow by name. | ||
| /// </para> | ||
| /// <para> | ||
| /// The workflow is started through the durable backend rather than through the workflow's generated | ||
| /// HTTP route, so the call path does not depend on that route and the same two lines work from any | ||
| /// trigger type - queue, timer, Event Grid, Service Bus. | ||
| /// </para> | ||
| /// </remarks> | ||
| public sealed class OrderFunctions | ||
| { | ||
| private const string CancelOrderWorkflow = "CancelOrder"; | ||
|
|
||
| /// <summary> | ||
| /// Starts the <c>CancelOrder</c> workflow and returns its run ID immediately. | ||
| /// </summary> | ||
| /// <param name="request">The HTTP request.</param> | ||
| /// <param name="orderId">The order to cancel, taken from the route.</param> | ||
| /// <param name="durableClient">The Durable Task client provided by the <c>[DurableClient]</c> binding.</param> | ||
| /// <param name="context">The function invocation context.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| /// <returns>A <c>202 Accepted</c> response carrying the workflow run ID.</returns> | ||
| [Function(nameof(CancelOrderAsync))] | ||
| public async Task<IActionResult> CancelOrderAsync( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "orders/{orderId}/cancel")] HttpRequest request, | ||
| string orderId, | ||
| [DurableClient] DurableTaskClient durableClient, | ||
| FunctionContext context, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| // Get a workflow client from the durable client binding. No endpoint URI, no HttpClient. | ||
| IWorkflowClient workflows = durableClient.AsWorkflowClient(context); | ||
|
|
||
| // Start the registered workflow by name. The Workflow object built in Program.cs is not needed here. | ||
| IWorkflowRun run = await workflows.RunAsync(CancelOrderWorkflow, orderId, cancellationToken: cancellationToken); | ||
|
|
||
| return new AcceptedResult(location: null, value: $"Workflow orchestration started for {CancelOrderWorkflow}. Orchestration runId: {run.RunId}"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Starts the <c>CancelOrder</c> workflow and waits for it to finish before responding. | ||
| /// </summary> | ||
| /// <param name="request">The HTTP request.</param> | ||
| /// <param name="orderId">The order to cancel, taken from the route.</param> | ||
| /// <param name="durableClient">The Durable Task client provided by the <c>[DurableClient]</c> binding.</param> | ||
| /// <param name="context">The function invocation context.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| /// <returns>A <c>200 OK</c> response carrying the workflow result, or <c>202 Accepted</c> carrying the run ID if the run handle does not support awaiting completion.</returns> | ||
| [Function(nameof(CancelOrderAndWaitAsync))] | ||
| public async Task<IActionResult> CancelOrderAndWaitAsync( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "orders/{orderId}/cancel-and-wait")] HttpRequest request, | ||
| string orderId, | ||
| [DurableClient] DurableTaskClient durableClient, | ||
| FunctionContext context, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| IWorkflowClient workflows = durableClient.AsWorkflowClient(context); | ||
|
|
||
| IWorkflowRun run = await workflows.RunAsync(CancelOrderWorkflow, orderId, cancellationToken: cancellationToken); | ||
|
|
||
| // Durable workflow runs also implement IAwaitableWorkflowRun, so the same handle can be | ||
| // awaited for the final result. | ||
| if (run is not IAwaitableWorkflowRun awaitableRun) | ||
| { | ||
| return new AcceptedResult(location: null, value: run.RunId); | ||
| } | ||
|
|
||
| string? result = await awaitableRun.WaitForCompletionAsync<string>(cancellationToken); | ||
| return new OkObjectResult(result); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
dotnet/src/Microsoft.Agents.AI.DurableTask/WorkflowNotRegisteredException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| namespace Microsoft.Agents.AI.DurableTask; | ||
|
|
||
| /// <summary> | ||
| /// Exception thrown when a workflow with the specified name has not been registered. | ||
| /// </summary> | ||
| public sealed class WorkflowNotRegisteredException : InvalidOperationException | ||
| { | ||
| // Not used, but required by static analysis. | ||
| private WorkflowNotRegisteredException() | ||
| { | ||
| this.WorkflowName = string.Empty; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WorkflowNotRegisteredException"/> class with the workflow name. | ||
| /// </summary> | ||
| /// <param name="workflowName">The name of the workflow that was not registered.</param> | ||
| public WorkflowNotRegisteredException(string workflowName) | ||
| : base(GetMessage(workflowName)) | ||
| { | ||
| this.WorkflowName = workflowName; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WorkflowNotRegisteredException"/> class with the workflow name and an inner exception. | ||
| /// </summary> | ||
| /// <param name="workflowName">The name of the workflow that was not registered.</param> | ||
| /// <param name="innerException">The exception that is the cause of the current exception.</param> | ||
| public WorkflowNotRegisteredException(string workflowName, Exception? innerException) | ||
| : base(GetMessage(workflowName), innerException) | ||
| { | ||
| this.WorkflowName = workflowName; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the name of the workflow that was not registered. | ||
| /// </summary> | ||
| public string WorkflowName { get; } | ||
|
|
||
| private static string GetMessage(string workflowName) | ||
| { | ||
| ArgumentException.ThrowIfNullOrEmpty(workflowName); | ||
| return $"No workflow named '{workflowName}' was registered. Ensure the workflow is registered using {nameof(ServiceCollectionExtensions.ConfigureDurableWorkflows)} or {nameof(ServiceCollectionExtensions.ConfigureDurableOptions)} before invoking it by name."; | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.