-
Notifications
You must be signed in to change notification settings - Fork 53
Added overloads for simplified func-based activity and orchestrator registration #589
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
base: main
Are you sure you want to change the base?
Conversation
| DurableTaskAttribute? attribute = method.GetCustomAttribute<DurableTaskAttribute>(); | ||
| if (attribute?.Name.Name is not null and not "") | ||
| { | ||
| return attribute.Name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really see how it's possible for attribute to ever be null here, assuming it's not shared state that can be accessed (and mutated) by multiple threads concurrently (something we're not doing).
| DurableTaskAttribute? attribute = method.GetCustomAttribute<DurableTaskAttribute>(); | ||
| if (attribute?.Name.Name is not null and not "") | ||
| { | ||
| return attribute.Name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds simplified overloads for func-based activity and orchestrator registration, allowing developers to omit the explicit name parameter. The name is automatically inferred from either a [DurableTask] attribute on the method or the method name itself, reducing boilerplate when registering tasks.
Key changes:
- Added 8 new overloads each for
AddOrchestratorFuncandAddActivityFuncthat infer task names from delegates - Extended
DurableTaskAttributeto support method-level application for custom naming - Comprehensive test coverage for all new overload variations including lambda detection
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/Abstractions/DurableTaskRegistry.Orchestrators.cs |
Added 8 new AddOrchestratorFunc overloads with name inference and GetTaskNameFromDelegate helper method |
src/Abstractions/DurableTaskRegistry.Activities.cs |
Added 8 new AddActivityFunc overloads with name inference and GetActivityNameFromDelegate helper method |
src/Abstractions/DurableTaskAttribute.cs |
Extended AttributeUsage to support methods and updated documentation to reflect method usage |
test/Worker/Core.Tests/DurableTaskRegistryTests.Orchestrators.cs |
Added 10 new test cases covering all orchestrator overload variations and error conditions |
test/Worker/Core.Tests/DurableTaskRegistryTests.Activities.cs |
Added 10 new test cases covering all activity overload variations and error conditions |
src/Abstractions/Entities/TaskEntity.cs |
Added string type validation for entity state (unrelated to PR purpose) |
misc/misc.csproj |
Added Directory.Packages.* to project includes (unrelated to PR purpose) |
| // Throw if TState is a string, since strings are immutable and don't support dynamic creation of new instances. | ||
| if (typeof(TState) == typeof(string)) | ||
| { | ||
| throw new InvalidOperationException("Entity state cannot be a string. Use a class or struct instead."); | ||
| } | ||
|
|
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This validation for string entity state appears to be unrelated to the PR's stated purpose of adding simplified func-based activity and orchestrator registration. This change should be removed or moved to a separate PR focused on entity state validation.
| // Throw if TState is a string, since strings are immutable and don't support dynamic creation of new instances. | |
| if (typeof(TState) == typeof(string)) | |
| { | |
| throw new InvalidOperationException("Entity state cannot be a string. Use a class or struct instead."); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something I ran into when doing some unrelated prototyping and wanted to create a fix while it was on the top of my mind. I could make it into a separate PR, but not sure if it's really worth it.
| static TaskName GetTaskNameFromDelegate(Delegate @delegate) | ||
| { | ||
| MethodInfo method = @delegate.Method; | ||
|
|
||
| // Check for DurableTaskAttribute on the method | ||
| DurableTaskAttribute? attribute = method.GetCustomAttribute<DurableTaskAttribute>(); | ||
| if (attribute?.Name.Name is not null and not "") | ||
| { | ||
| return attribute.Name; | ||
| } | ||
|
|
||
| // Fall back to method name | ||
| string? methodName = method.Name; | ||
| if (string.IsNullOrEmpty(methodName) || methodName.StartsWith("<", StringComparison.Ordinal)) | ||
| { | ||
| throw new ArgumentException( | ||
| "Cannot infer orchestrator name from the delegate. The delegate must either have a " + | ||
| "[DurableTask] attribute with a name, or be a named method (not a lambda or anonymous delegate).", | ||
| nameof(@delegate)); | ||
| } | ||
|
|
||
| return new TaskName(methodName); | ||
| } |
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GetTaskNameFromDelegate and GetActivityNameFromDelegate methods have identical implementations. Consider extracting this logic into a shared private static method in a common location to reduce code duplication and improve maintainability.
| /// <see cref="TaskOrchestrator{TInput, TOutput}"/>, <see cref="TaskActivity{TInput, TOutput}"/>, | ||
| /// or TaskEntity{TState} from the Microsoft.DurableTask.Entities namespace. | ||
| /// It can also be applied to methods used with <see cref="DurableTaskRegistry.AddOrchestratorFunc{TInput, TOutput}(System.Func{TaskOrchestrationContext, TInput, System.Threading.Tasks.Task{TOutput}})"/> | ||
| /// or similar overloads to specify a custom name for the orchestrator. |
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation mentions "orchestrator" specifically but this attribute can now also be applied to activity methods. The documentation should mention both orchestrators and activities to be more accurate and helpful.
| /// or similar overloads to specify a custom name for the orchestrator. | |
| /// or similar overloads to specify a custom name for the orchestrator or activity. |
| <None Include="$(RepoRoot).gitignore" /> | ||
| <None Include="$(RepoRoot)azure-pipelines-release.yml" /> | ||
| <None Include="$(RepoRoot)Directory.Build.*" /> | ||
| <None Include="$(RepoRoot)Directory.Packages.*" /> |
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change to include Directory.Packages.* appears unrelated to the PR's stated purpose of adding simplified func-based activity and orchestrator registration. This should be removed or moved to a separate PR if it's intended to be a general maintenance improvement.
| <None Include="$(RepoRoot)Directory.Packages.*" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to keep this in here since it's not worth creating a separate PR for.
Summary
What changed?
Added new overloads for func-based activity and orchestrator registration.
For example, instead of doing this all the time:
I can now do this:
Why is this change needed?
The current methods are verbose and require writing repetitive code. I found it quite annoying when creating samples.
Issues / work items
There are no work items associated with this PR
Project checklist
release_notes.mdAI-assisted code disclosure (required)
Was an AI tool used? (select one)
If AI was used:
AI verification (required if AI was used):
Testing
Automated tests
Manual validation (only if runtime/behavior changed)
N/A
Notes for reviewers