diff --git a/src/Abstractions/Abstractions.csproj b/src/Abstractions/Abstractions.csproj index 9c20cb8a..ef9bb8c2 100644 --- a/src/Abstractions/Abstractions.csproj +++ b/src/Abstractions/Abstractions.csproj @@ -9,6 +9,7 @@ + diff --git a/src/Abstractions/TaskName.cs b/src/Abstractions/TaskName.cs index 62d36f56..c39e912c 100644 --- a/src/Abstractions/TaskName.cs +++ b/src/Abstractions/TaskName.cs @@ -29,6 +29,41 @@ public TaskName(string name) } } + /// + /// Initializes a new instance of the struct. + /// + /// The name of the task. Providing null will yield the default struct. + /// The version of the task. + public TaskName(string name, string version) + { + if (name is null) + { + if (version is null) + { + // Force the default struct when null is passed in. + this.Name = null!; + this.Version = null!; + } + else + { + throw new ArgumentException("name must not be null when version is non-null"); + } + } + else + { + if (version is null) + { + this.Name = name; + this.Version = string.Empty; // fallback to the constructor without version parameter + } + else + { + this.Name = name; + this.Version = version; + } + } + } + /// /// Gets the name of the task without the version. /// @@ -40,23 +75,22 @@ public TaskName(string name) /// /// Gets the version of the task. /// - /// - /// Task versions is currently locked to as it is not yet integrated into task - /// identification. This is being left here as we intend to support it soon. - /// + /// + /// The version of the activity task. + /// public string Version { get; } /// /// Implicitly converts a into a of the property value. /// /// The to be converted into a string. - public static implicit operator string(TaskName value) => value.Name; + public static implicit operator string(TaskName value) => value.ToString(); /// /// Implicitly converts a into a value. /// /// The string to convert into a . - public static implicit operator TaskName(string value) => string.IsNullOrEmpty(value) ? default : new(value); + public static implicit operator TaskName(string value) => FromString(value); /// /// Compares two objects for equality. @@ -80,6 +114,32 @@ public TaskName(string name) return !a.Equals(b); } + /// + /// Parses the taskname string and initializes a new instance of the struct. + /// + /// The taskname string in format of "Name:Version" or "Name". + /// New instance parsed from taskname string. + public static TaskName FromString(string taskname) + { + if (string.IsNullOrEmpty(taskname)) + { + return default; + } + + string[] parts = taskname.Split(':'); + if (parts.Length == 1) + { + return new TaskName(parts[0]); + } + + if (parts.Length == 2) + { + return new TaskName(parts[0], parts[1]); + } + + throw new ArgumentException("Invalid task name format: taskname=" + taskname); + } + /// /// Gets a value indicating whether to objects /// are equal using value semantics. @@ -88,7 +148,8 @@ public TaskName(string name) /// true if the two objects are equal using value semantics; otherwise false. public bool Equals(TaskName other) { - return string.Equals(this.Name, other.Name, StringComparison.OrdinalIgnoreCase); + return string.Equals(this.Name, other.Name, StringComparison.OrdinalIgnoreCase) + && string.Equals(this.Version, other.Version, StringComparison.OrdinalIgnoreCase); } /// @@ -113,7 +174,7 @@ public override bool Equals(object? obj) /// A 32-bit hash code value. public override int GetHashCode() { - return StringComparer.OrdinalIgnoreCase.GetHashCode(this.Name); + return HashCode.Combine(this.Name, this.Version); } /// diff --git a/src/Abstractions/TaskOrchestrationContext.cs b/src/Abstractions/TaskOrchestrationContext.cs index b37b46d3..242c36e9 100644 --- a/src/Abstractions/TaskOrchestrationContext.cs +++ b/src/Abstractions/TaskOrchestrationContext.cs @@ -22,6 +22,11 @@ public abstract class TaskOrchestrationContext /// public abstract string InstanceId { get; } + /// + /// Gets the version of the current orchestration instance. + /// + public virtual string? InstanceVersion => null; + /// /// Gets the parent instance or null if there is no parent orchestration. /// diff --git a/src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs b/src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs index 7151a4e0..02426f76 100644 --- a/src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs +++ b/src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs @@ -51,6 +51,9 @@ public TaskOrchestrationContextWrapper( /// public override string InstanceId => this.innerContext.OrchestrationInstance.InstanceId; + /// + public override string InstanceVersion => this.innerContext.Version; + /// public override ParentOrchestrationInstance? Parent => this.invocationContext.Parent;