diff --git a/Caspian.WorkflowFramework.Package/Caspian.WorkflowFramework.Package.csproj b/Caspian.WorkflowFramework.Package/Caspian.WorkflowFramework.Package.csproj
index bdd5b34..462fa75 100644
--- a/Caspian.WorkflowFramework.Package/Caspian.WorkflowFramework.Package.csproj
+++ b/Caspian.WorkflowFramework.Package/Caspian.WorkflowFramework.Package.csproj
@@ -5,7 +5,7 @@
enable
enable
Caspian.WorkflowFramework
- 1.1.0-preview.20
+ 1.1.0-preview.21
Behzad Alizadeh
Caspian
Caspian.WorkflowFramework
@@ -17,58 +17,61 @@
LICENSE.txt
true
caspian-pkg-key.snk
+
+ $(MSBuildThisFileDirectory)packages
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Caspian.WorkflowFramework.Package/Models/PublishEventModel.cs b/Caspian.WorkflowFramework.Package/Models/PublishEventModel.cs
index cd8674a..390c2e5 100644
--- a/Caspian.WorkflowFramework.Package/Models/PublishEventModel.cs
+++ b/Caspian.WorkflowFramework.Package/Models/PublishEventModel.cs
@@ -14,4 +14,6 @@ public class PublishEventModel
[Required]
public JsonElement EventData { get; set; }
+
+ public string? SessionId { get; set; }
}
diff --git a/Caspian.WorkflowFramework.Package/Services/WorkflowService.cs b/Caspian.WorkflowFramework.Package/Services/WorkflowService.cs
index 475aba0..15ed277 100644
--- a/Caspian.WorkflowFramework.Package/Services/WorkflowService.cs
+++ b/Caspian.WorkflowFramework.Package/Services/WorkflowService.cs
@@ -14,6 +14,7 @@ using Elsa.Workflows.Runtime.Messages;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
+using Newtonsoft.Json.Linq;
using San.MongoLogger;
using System.Diagnostics;
using System.Dynamic;
@@ -292,22 +293,34 @@ public class WorkflowService : IWorkflowService, ILoggable
return response;
}
- dynamic payload = null;
+ ExpandoObject payload;
- if (input.EventData.ValueKind != JsonValueKind.Undefined &&
- input.EventData.ValueKind != JsonValueKind.Null)
+ var payloadObject = new JObject();
+
+ if (input.EventData.ValueKind is not JsonValueKind.Undefined
+ and not JsonValueKind.Null)
{
- var json = input.EventData.GetRawText();
+ if (input.EventData.ValueKind != JsonValueKind.Object)
+ throw new InvalidOperationException("EventData must be a JSON object.");
- var settings = new JsonSerializerSettings
- {
- Converters = { new ExpandoObjectConverter() }
- };
-
- // تبدیل JSON به object داینامیک
- payload = JsonConvert.DeserializeObject(json, settings);
+ payloadObject = JObject.Parse(input.EventData.GetRawText());
}
+ if (!string.IsNullOrWhiteSpace(input.SessionId))
+ {
+ payloadObject["SessionId"] = input.SessionId;
+ }
+
+ var settings = new JsonSerializerSettings
+ {
+ Converters = { new ExpandoObjectConverter() }
+ };
+
+ payload = JsonConvert.DeserializeObject(
+ payloadObject.ToString(Formatting.None),
+ settings
+ ) ?? new ExpandoObject();
+
//await _eventPublisher.PublishAsync(input.EventName, null, input.WorkflowInstanceId, null, input.EventData, asynchronous: true);
await _eventPublisher.PublishAsync(input.EventName, null, input.WorkflowInstanceId, null, payload);
diff --git a/Caspian.WorkflowFramework.Package/Utility/ConversionExtensions.cs b/Caspian.WorkflowFramework.Package/Utility/ConversionExtensions.cs
index 1dfd83e..a230827 100644
--- a/Caspian.WorkflowFramework.Package/Utility/ConversionExtensions.cs
+++ b/Caspian.WorkflowFramework.Package/Utility/ConversionExtensions.cs
@@ -18,6 +18,9 @@ namespace Caspian.WorkflowFramework.Utility
if (dictionary == null || dictionary.Count == 0)
return workflowModel;
+ // قبل از تبدیل به مدل خروجی، sessionId حذف میشود.
+ dictionary.RemoveSessionId();
+
//Map dictionary values to WorkflowModel properties
workflowModel.InstanceId = dictionary.ContainsKey(nameof(workflowModel.InstanceId))
? dictionary[nameof(workflowModel.InstanceId)]?.ToString()
diff --git a/Caspian.WorkflowFramework.Package/Utility/WorkflowVariableExtensions.cs b/Caspian.WorkflowFramework.Package/Utility/WorkflowVariableExtensions.cs
new file mode 100644
index 0000000..eb19fd4
--- /dev/null
+++ b/Caspian.WorkflowFramework.Package/Utility/WorkflowVariableExtensions.cs
@@ -0,0 +1,226 @@
+using System.Collections;
+using System.Text.Json;
+using System.Text.Json.Nodes;
+using Newtonsoft.Json.Linq;
+
+namespace Caspian.WorkflowFramework.Utility;
+
+public static class WorkflowVariableExtensions
+{
+ private const string SessionIdKey = "SessionId";
+
+ ///
+ /// sessionId را از دیکشنری اصلی و تمام مقادیر تو در تو حذف میکند.
+ ///
+ public static Dictionary RemoveSessionId(
+ this Dictionary variables)
+ {
+ if (variables == null || variables.Count == 0)
+ return variables;
+
+ RemoveSessionIdKeys(variables);
+
+ foreach (var key in variables.Keys.ToList())
+ variables[key] = RemoveSessionIdRecursive(variables[key]);
+
+ return variables;
+ }
+
+ private static object? RemoveSessionIdRecursive(object? value)
+ {
+ if (value == null)
+ return null;
+
+ switch (value)
+ {
+ // Dictionary و ExpandoObject
+ case IDictionary dictionary:
+ {
+ RemoveSessionIdKeys(dictionary);
+
+ foreach (var key in dictionary.Keys.ToList())
+ dictionary[key] = RemoveSessionIdRecursive(dictionary[key]);
+
+ return value;
+ }
+
+ // System.Text.Json.Nodes
+ case JsonObject jsonObject:
+ {
+ RemoveSessionIdKeys(jsonObject);
+
+ foreach (var property in jsonObject.ToList())
+ jsonObject[property.Key] =
+ RemoveSessionIdFromJsonNode(property.Value);
+
+ return jsonObject;
+ }
+
+ case JsonArray jsonArray:
+ {
+ for (var i = 0; i < jsonArray.Count; i++)
+ {
+ jsonArray[i] =
+ RemoveSessionIdFromJsonNode(jsonArray[i]);
+ }
+
+ return jsonArray;
+ }
+
+ // JsonElement تغییرپذیر نیست؛ پس نسخه پاکسازیشده میسازیم.
+ case JsonElement jsonElement:
+ {
+ var node = JsonNode.Parse(jsonElement.GetRawText());
+
+ if (node == null)
+ return jsonElement;
+
+ var cleanedNode = RemoveSessionIdFromJsonNode(node);
+
+ return cleanedNode == null
+ ? jsonElement
+ : JsonSerializer.SerializeToElement(cleanedNode);
+ }
+
+ // Newtonsoft.Json
+ case JObject jObject:
+ {
+ RemoveSessionIdKeys(jObject);
+
+ foreach (var property in jObject.Properties().ToList())
+ property.Value = RemoveSessionIdFromJToken(property.Value);
+
+ return jObject;
+ }
+
+ case JArray jArray:
+ {
+ for (var i = 0; i < jArray.Count; i++)
+ jArray[i] = RemoveSessionIdFromJToken(jArray[i]);
+
+ return jArray;
+ }
+
+ // سایر لیستها و آرایهها
+ case IList list:
+ {
+ for (var i = 0; i < list.Count; i++)
+ list[i] = RemoveSessionIdRecursive(list[i]);
+
+ return list;
+ }
+
+ case IEnumerable enumerable when value is not string:
+ {
+ foreach (var child in enumerable)
+ RemoveSessionIdRecursive(child);
+
+ return value;
+ }
+
+ default:
+ return value;
+ }
+ }
+
+ private static JsonNode? RemoveSessionIdFromJsonNode(JsonNode? node)
+ {
+ if (node == null)
+ return null;
+
+ switch (node)
+ {
+ case JsonObject jsonObject:
+ {
+ RemoveSessionIdKeys(jsonObject);
+
+ foreach (var property in jsonObject.ToList())
+ {
+ jsonObject[property.Key] =
+ RemoveSessionIdFromJsonNode(property.Value);
+ }
+
+ break;
+ }
+
+ case JsonArray jsonArray:
+ {
+ for (var i = 0; i < jsonArray.Count; i++)
+ {
+ jsonArray[i] =
+ RemoveSessionIdFromJsonNode(jsonArray[i]);
+ }
+
+ break;
+ }
+ }
+
+ return node;
+ }
+
+ private static JToken RemoveSessionIdFromJToken(JToken token)
+ {
+ switch (token)
+ {
+ case JObject jObject:
+ {
+ RemoveSessionIdKeys(jObject);
+
+ foreach (var property in jObject.Properties().ToList())
+ property.Value = RemoveSessionIdFromJToken(property.Value);
+
+ break;
+ }
+
+ case JArray jArray:
+ {
+ for (var i = 0; i < jArray.Count; i++)
+ jArray[i] = RemoveSessionIdFromJToken(jArray[i]);
+
+ break;
+ }
+ }
+
+ return token;
+ }
+
+ private static void RemoveSessionIdKeys(
+ IDictionary dictionary)
+ {
+ var keys = dictionary.Keys
+ .Where(IsSessionId)
+ .ToList();
+
+ foreach (var key in keys)
+ dictionary.Remove(key);
+ }
+
+ private static void RemoveSessionIdKeys(JsonObject jsonObject)
+ {
+ var keys = jsonObject
+ .Select(x => x.Key)
+ .Where(IsSessionId)
+ .ToList();
+
+ foreach (var key in keys)
+ jsonObject.Remove(key);
+ }
+
+ private static void RemoveSessionIdKeys(JObject jObject)
+ {
+ var properties = jObject.Properties()
+ .Where(x => IsSessionId(x.Name))
+ .ToList();
+
+ foreach (var property in properties)
+ property.Remove();
+ }
+
+ private static bool IsSessionId(string key)
+ {
+ return string.Equals(
+ key,
+ SessionIdKey,
+ StringComparison.OrdinalIgnoreCase);
+ }
+}
\ No newline at end of file
diff --git a/NuGet.Config b/NuGet.Config
index 5691179..2610c12 100644
--- a/NuGet.Config
+++ b/NuGet.Config
@@ -9,6 +9,6 @@
-
+
\ No newline at end of file
diff --git a/SampleWorkflowWeb/appsettings.json b/SampleWorkflowWeb/appsettings.json
index e908329..9be432d 100644
--- a/SampleWorkflowWeb/appsettings.json
+++ b/SampleWorkflowWeb/appsettings.json
@@ -7,7 +7,7 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
- "DefaultConnection": "Data Source=.;Initial Catalog=WfDb;User ID=sa;Password=123456;Integrated Security=false;TrustServerCertificate=True;"
+ "DefaultConnection": "Data Source=.;Initial Catalog=WfLive;User ID=sa;Password=123456;Integrated Security=false;TrustServerCertificate=True;"
},
"LogDatabaseSettings": {
"LogsCollectionName": "TestWorkflowCollection",