/*
# Microshaoft
/r:System.Xaml.dll
/r:System.Activities.dll
/r:System.Activities.DurableInstancing.dll
/r:System.Runtime.DurableInstancing.dll
/r:"D:\Microshaoft.Nuget.Packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll"
*/
namespace Microshaoft
{
using Newtonsoft.Json.Linq;
using System;
using System.Activities;
using System.Activities.Tracking;
using System.Activities.XamlIntegration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xaml;
using System.Xml;
using System.Runtime.DurableInstancing;
public static class WorkFlowHelper
{
public static WorkflowApplication CreateWorkflowApplication
(
string xaml
, string localAssemblyFilePath = null
, Func<InstanceStore> onPersistProcessFunc = null
)
{
var activity = XamlToActivity
(
xaml
, localAssemblyFilePath
);
WorkflowApplication workflowApplication = new WorkflowApplication(activity);
if (onPersistProcessFunc != null)
{
workflowApplication.InstanceStore = onPersistProcessFunc();
}
return workflowApplication;
}
public static Activity XamlToActivity
(
string xaml
, string localAssemblyFilePath = null
)
{
Assembly localAssembly = null;
if (string.IsNullOrEmpty(localAssemblyFilePath))
{
localAssembly = Assembly
.GetExecutingAssembly();
}
else
{
localAssembly = Assembly
.LoadFrom(localAssemblyFilePath);
}
var stringReader = new StringReader(xaml);
var xmlReader = XmlReader.Create(stringReader);
var xamlXmlReader = new XamlXmlReader
(
xmlReader
, new XamlXmlReaderSettings()
{
LocalAssembly = localAssembly
}
);
var xamlReader = ActivityXamlServices
.CreateReader
(
xamlXmlReader
);
var activity = ActivityXamlServices
.Load
(
xamlReader
, new ActivityXamlServicesSettings()
{
CompileExpressions = true
}
);
return activity;
}
public static TrackingProfile GetTrackingProfileFromJson
(
string json
, bool isArray = false
)
{
TrackingProfile trackingProfile = null;
var trackingQueries = GetTrackingQueriesFromJson(json, isArray);
if (trackingQueries != null)
{
foreach (var trackingQuery in trackingQueries)
{
if (trackingProfile == null)
{
trackingProfile = new TrackingProfile();
}
trackingProfile
.Queries
.Add(trackingQuery);
}
}
return trackingProfile;
}
public static TrackingParticipant GetTrackingParticipantFromJson<TTrackingParticipant>
(
string json
, bool isArray = false
)
where TTrackingParticipant : TrackingParticipant, new()
{
TrackingParticipant trackingParticipant = null;
TrackingProfile trackingProfile
= GetTrackingProfileFromJson(json, isArray);
if (trackingProfile != null)
{
trackingParticipant = new TTrackingParticipant();
trackingParticipant.TrackingProfile = trackingProfile;
}
return trackingParticipant;
}
public static IEnumerable<TrackingQuery> GetTrackingQueriesFromJson
(
string json
, bool isArray = false
)
{
IEnumerable<TrackingQuery> r = null;
if (isArray)
{
//闭包
var key = string.Empty;
r = JsonHelper
.DeserializeToFromDictionary<string, JObject[], JObject[]>
(
json
, (x, y) =>
{
//闭包
key = x;
return y;
}
)
.SelectMany
(
(x) =>
{
return x;
}
)
.Select
(
(x) =>
{
//闭包
return
GetTrackingQuery(key, x);
}
);
}
else
{
r = JsonHelper
.DeserializeToFromDictionary<string, JObject, TrackingQuery>
(
json
, (x, y) =>
{
return GetTrackingQuery(x, y);
}
);
}
return r;
}
public static TrackingQuery GetTrackingQuery(string queryName, JObject jObject)
{
var json = jObject.ToString();
return
GetTrackingQuery
(
queryName
, json
);
}
public static TrackingQuery GetTrackingQuery(string queryName, string json)
{
TrackingQuery r = null;
if (string.Compare(queryName, "WorkflowInstanceQuery", true) == 0)
{
r = JsonHelper
.DeserializeByJTokenPath<WorkflowInstanceQuery>
(
json
);
}
else if (string.Compare(queryName, "ActivityStateQuery", true) == 0)
{
r = JsonHelper
.DeserializeByJTokenPath<ActivityStateQuery>
(
json
);
}
else if (string.Compare(queryName, "CustomTrackingQuery", true) == 0)
{
r = JsonHelper
.DeserializeByJTokenPath<CustomTrackingQuery>
(
json
);
}
else if (string.Compare(queryName, "FaultPropagationQuery", true) == 0)
{
r = JsonHelper
.DeserializeByJTokenPath<FaultPropagationQuery>
(
json
);
}
else if (string.Compare(queryName, "BookmarkResumptionQuery", true) == 0)
{
r = JsonHelper
.DeserializeByJTokenPath<BookmarkResumptionQuery>
(
json
);
}
else if (string.Compare(queryName, "ActivityScheduledQuery", true) == 0)
{
r = JsonHelper
.DeserializeByJTokenPath<ActivityScheduledQuery>
(
json
);
}
else if (string.Compare(queryName, "CancelRequestedQuery", true) == 0)
{
r = JsonHelper
.DeserializeByJTokenPath<CancelRequestedQuery>
(
json
);
}
return r;
}
}
}
namespace Microshaoft
{
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
public static class JsonHelper
{
public static string XmlToJson
(
string xml
, Newtonsoft
.Json
.Formatting formatting
= Newtonsoft
.Json
.Formatting
.Indented
, bool needKeyQuote = false
)
{
XNode xElement;
xElement = XElement.Parse(xml).Elements().First();
string json = string.Empty;
using (var stringWriter = new StringWriter())
{
using (var jsonTextWriter = new JsonTextWriter(stringWriter))
{
jsonTextWriter.Formatting = formatting;
jsonTextWriter.QuoteName = needKeyQuote;
var jsonSerializer = new JsonSerializer();
jsonSerializer.Serialize(jsonTextWriter, xElement);
json = stringWriter.ToString();
}
}
return json;
}
public static string JsonToXml
(
string json
, bool needRoot = false
, string defaultDeserializeRootElementName = "root"
)
{
if (needRoot)
{
json = string.Format
(
@"{{ {1}{0}{2} }}"
, " : "
, defaultDeserializeRootElementName
, json
);
}
//XmlDocument xmlDocument = JsonConvert.DeserializeXmlNode(json, defaultDeserializeRootElementName);
var xDocument = JsonConvert
.DeserializeXNode
(
json
, defaultDeserializeRootElementName
);
var xml = xDocument
.Elements()
.First()
.ToString();
return xml;
}
public static T DeserializeByJTokenPath<T>
(
string json
, string jTokenPath = null //string.Empty
)
{
var jObject = JObject.Parse(json);
var jsonSerializer = new JsonSerializer();
if (string.IsNullOrEmpty(jTokenPath))
{
jTokenPath = string.Empty;
}
var jToken = jObject.SelectToken(jTokenPath);
using (var jsonReader = jToken.CreateReader())
{
return
jsonSerializer
.Deserialize<T>(jsonReader);
}
}
public static string Serialize
(
object target
, bool formattingIndented = false
, bool keyQuoteName = false
)
{
string json = string.Empty;
using (StringWriter stringWriter = new StringWriter())
{
using (var jsonTextWriter = new JsonTextWriter(stringWriter))
{
jsonTextWriter.QuoteName = keyQuoteName;
jsonTextWriter.Formatting = (formattingIndented ? Formatting.Indented : Formatting.None);
var jsonSerializer = new JsonSerializer();
jsonSerializer.Serialize(jsonTextWriter, target);
json = stringWriter.ToString();
}
}
return json;
}
public static void ReadJsonPathsValuesAsStrings
(
string json
, string[] jsonPaths
, Func<string, string, bool> onReadedOncePathStringValueProcesssFunc = null
)
{
using (var stringReader = new StringReader(json))
{
using (var jsonReader = new JsonTextReader(stringReader))
{
bool breakAndReturn = false;
while
(
jsonReader.Read()
&&
!breakAndReturn
)
{
foreach (var x in jsonPaths)
{
if (x == jsonReader.Path)
{
if (onReadedOncePathStringValueProcesssFunc != null)
{
var s = jsonReader.ReadAsString();
breakAndReturn
= onReadedOncePathStringValueProcesssFunc
(
x
, s
);
if (breakAndReturn)
{
break;
}
}
}
}
}
}
}
}
public static IEnumerable<TElement>
DeserializeToFromDictionary<TKey, TValue, TElement>
(
string json
, Func<TKey, TValue, TElement> OnOneElementProcessFunc
)
{
//IEnumerable<TElement> r = default(IEnumerable<TElement>);
return
DeserializeByJTokenPath<Dictionary<TKey, TValue>>(json)
.Select
(
(x) =>
{
var rr = OnOneElementProcessFunc(x.Key, x.Value);
return rr;
}
);
//return r;
}
}
}
namespace Microshaoft
{
using System;
using System.Activities.Tracking;
public class CommonTrackingParticipant : TrackingParticipant
{
public Func<TrackingRecord, TimeSpan, bool> OnTrackingRecordReceived;
protected override void Track(TrackingRecord record, TimeSpan timeout)
{
var r = false;
if (OnTrackingRecordReceived != null)
{
r = OnTrackingRecordReceived(record, timeout);
}
}
}
}

最新文章

  1. kubernetes单机板
  2. 夜深了,写了个JQuery的省市区三级级联效果
  3. DirectXMath
  4. python中mysqldb的用法
  5. SNMP常用数据操作
  6. MySQL 5.6 Threadpool(优先队列)介绍及性能测试【转】
  7. error: insufficient permissions for device: verify udev rules
  8. BZOJ1690: [Usaco2007 Dec]奶牛的旅行
  9. SQL实现递归及存储过程中In()参数传递解决方案[转]
  10. MongoDB深圳用户组线下活动召集
  11. VMware workstation批量创建虚拟机和自动化安装操作系统(一)
  12. 来一轮带注释的demo,彻底搞懂javascript中的replace函数
  13. elasticsearch分词器Jcseg安装手册
  14. vue定义全局组件
  15. bug优先级别
  16. 远程执行shell脚本的小技巧
  17. 开源深度学习架构Caffe
  18. echarts x轴文字显示不全解决办法
  19. 搭建Airflow数据流调度器
  20. 【转】如何基于linux进程通信设计方案

热门文章

  1. Ubuntu 16.04 安装 Kodi v17 “Krypton” Alpha 2
  2. Linux系统资源查看
  3. juery学习6——焦点事件
  4. 关于springMVC+Mybatis jar包详解
  5. AD域控制器通过组策略禁止USB设备
  6. (一)SQL Server分区详解Partition(目录)
  7. Code Snippets 代码片段
  8. 第3月第27天 uitableviewcell复用
  9. retrofit一点点理解
  10. iOS音频解码表格