1.  
  2. /*
  3. # Microshaoft
  4. /r:System.Xaml.dll
  5. /r:System.Activities.dll
  6. /r:System.Activities.DurableInstancing.dll
  7. /r:System.Runtime.DurableInstancing.dll
  8. /r:"D:\Microshaoft.Nuget.Packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll"
  9. */
  10. namespace Microshaoft
  11. {
  12. using Newtonsoft.Json.Linq;
  13. using System;
  14. using System.Activities;
  15. using System.Activities.Tracking;
  16. using System.Activities.XamlIntegration;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Reflection;
  21. using System.Xaml;
  22. using System.Xml;
  23. using System.Runtime.DurableInstancing;
  24. public static class WorkFlowHelper
  25. {
  26. public static WorkflowApplication CreateWorkflowApplication
  27. (
  28. string xaml
  29. , string localAssemblyFilePath = null
  30. , Func<InstanceStore> onPersistProcessFunc = null
  31. )
  32. {
  33. var activity = XamlToActivity
  34. (
  35. xaml
  36. , localAssemblyFilePath
  37. );
  38. WorkflowApplication workflowApplication = new WorkflowApplication(activity);
  39. if (onPersistProcessFunc != null)
  40. {
  41. workflowApplication.InstanceStore = onPersistProcessFunc();
  42. }
  43. return workflowApplication;
  44. }
  45. public static Activity XamlToActivity
  46. (
  47. string xaml
  48. , string localAssemblyFilePath = null
  49. )
  50. {
  51. Assembly localAssembly = null;
  52. if (string.IsNullOrEmpty(localAssemblyFilePath))
  53. {
  54. localAssembly = Assembly
  55. .GetExecutingAssembly();
  56. }
  57. else
  58. {
  59. localAssembly = Assembly
  60. .LoadFrom(localAssemblyFilePath);
  61. }
  62. var stringReader = new StringReader(xaml);
  63. var xmlReader = XmlReader.Create(stringReader);
  64. var xamlXmlReader = new XamlXmlReader
  65. (
  66. xmlReader
  67. , new XamlXmlReaderSettings()
  68. {
  69. LocalAssembly = localAssembly
  70. }
  71. );
  72. var xamlReader = ActivityXamlServices
  73. .CreateReader
  74. (
  75. xamlXmlReader
  76. );
  77. var activity = ActivityXamlServices
  78. .Load
  79. (
  80. xamlReader
  81. , new ActivityXamlServicesSettings()
  82. {
  83. CompileExpressions = true
  84. }
  85. );
  86. return activity;
  87. }
  88. public static TrackingProfile GetTrackingProfileFromJson
  89. (
  90. string json
  91. , bool isArray = false
  92. )
  93. {
  94. TrackingProfile trackingProfile = null;
  95. var trackingQueries = GetTrackingQueriesFromJson(json, isArray);
  96. if (trackingQueries != null)
  97. {
  98. foreach (var trackingQuery in trackingQueries)
  99. {
  100. if (trackingProfile == null)
  101. {
  102. trackingProfile = new TrackingProfile();
  103. }
  104. trackingProfile
  105. .Queries
  106. .Add(trackingQuery);
  107. }
  108. }
  109. return trackingProfile;
  110. }
  111. public static TrackingParticipant GetTrackingParticipantFromJson<TTrackingParticipant>
  112. (
  113. string json
  114. , bool isArray = false
  115. )
  116. where TTrackingParticipant : TrackingParticipant, new()
  117. {
  118. TrackingParticipant trackingParticipant = null;
  119. TrackingProfile trackingProfile
  120. = GetTrackingProfileFromJson(json, isArray);
  121. if (trackingProfile != null)
  122. {
  123. trackingParticipant = new TTrackingParticipant();
  124. trackingParticipant.TrackingProfile = trackingProfile;
  125. }
  126. return trackingParticipant;
  127. }
  128. public static IEnumerable<TrackingQuery> GetTrackingQueriesFromJson
  129. (
  130. string json
  131. , bool isArray = false
  132. )
  133. {
  134. IEnumerable<TrackingQuery> r = null;
  135. if (isArray)
  136. {
  137. //闭包
  138. var key = string.Empty;
  139. r = JsonHelper
  140. .DeserializeToFromDictionary<string, JObject[], JObject[]>
  141. (
  142. json
  143. , (x, y) =>
  144. {
  145. //闭包
  146. key = x;
  147. return y;
  148. }
  149. )
  150. .SelectMany
  151. (
  152. (x) =>
  153. {
  154. return x;
  155. }
  156. )
  157. .Select
  158. (
  159. (x) =>
  160. {
  161. //闭包
  162. return
  163. GetTrackingQuery(key, x);
  164. }
  165. );
  166. }
  167. else
  168. {
  169. r = JsonHelper
  170. .DeserializeToFromDictionary<string, JObject, TrackingQuery>
  171. (
  172. json
  173. , (x, y) =>
  174. {
  175. return GetTrackingQuery(x, y);
  176. }
  177. );
  178. }
  179. return r;
  180. }
  181. public static TrackingQuery GetTrackingQuery(string queryName, JObject jObject)
  182. {
  183. var json = jObject.ToString();
  184. return
  185. GetTrackingQuery
  186. (
  187. queryName
  188. , json
  189. );
  190. }
  191. public static TrackingQuery GetTrackingQuery(string queryName, string json)
  192. {
  193. TrackingQuery r = null;
  194. if (string.Compare(queryName, "WorkflowInstanceQuery", true) == 0)
  195. {
  196. r = JsonHelper
  197. .DeserializeByJTokenPath<WorkflowInstanceQuery>
  198. (
  199. json
  200. );
  201. }
  202. else if (string.Compare(queryName, "ActivityStateQuery", true) == 0)
  203. {
  204. r = JsonHelper
  205. .DeserializeByJTokenPath<ActivityStateQuery>
  206. (
  207. json
  208. );
  209. }
  210. else if (string.Compare(queryName, "CustomTrackingQuery", true) == 0)
  211. {
  212. r = JsonHelper
  213. .DeserializeByJTokenPath<CustomTrackingQuery>
  214. (
  215. json
  216. );
  217. }
  218. else if (string.Compare(queryName, "FaultPropagationQuery", true) == 0)
  219. {
  220. r = JsonHelper
  221. .DeserializeByJTokenPath<FaultPropagationQuery>
  222. (
  223. json
  224. );
  225. }
  226. else if (string.Compare(queryName, "BookmarkResumptionQuery", true) == 0)
  227. {
  228. r = JsonHelper
  229. .DeserializeByJTokenPath<BookmarkResumptionQuery>
  230. (
  231. json
  232. );
  233. }
  234. else if (string.Compare(queryName, "ActivityScheduledQuery", true) == 0)
  235. {
  236. r = JsonHelper
  237. .DeserializeByJTokenPath<ActivityScheduledQuery>
  238. (
  239. json
  240. );
  241. }
  242. else if (string.Compare(queryName, "CancelRequestedQuery", true) == 0)
  243. {
  244. r = JsonHelper
  245. .DeserializeByJTokenPath<CancelRequestedQuery>
  246. (
  247. json
  248. );
  249. }
  250. return r;
  251. }
  252. }
  253. }
  254. namespace Microshaoft
  255. {
  256. using Newtonsoft.Json;
  257. using Newtonsoft.Json.Linq;
  258. using System;
  259. using System.IO;
  260. using System.Linq;
  261. using System.Xml.Linq;
  262. using System.Collections.Generic;
  263. public static class JsonHelper
  264. {
  265. public static string XmlToJson
  266. (
  267. string xml
  268. , Newtonsoft
  269. .Json
  270. .Formatting formatting
  271. = Newtonsoft
  272. .Json
  273. .Formatting
  274. .Indented
  275. , bool needKeyQuote = false
  276. )
  277. {
  278. XNode xElement;
  279. xElement = XElement.Parse(xml).Elements().First();
  280. string json = string.Empty;
  281. using (var stringWriter = new StringWriter())
  282. {
  283. using (var jsonTextWriter = new JsonTextWriter(stringWriter))
  284. {
  285. jsonTextWriter.Formatting = formatting;
  286. jsonTextWriter.QuoteName = needKeyQuote;
  287. var jsonSerializer = new JsonSerializer();
  288. jsonSerializer.Serialize(jsonTextWriter, xElement);
  289. json = stringWriter.ToString();
  290. }
  291. }
  292. return json;
  293. }
  294. public static string JsonToXml
  295. (
  296. string json
  297. , bool needRoot = false
  298. , string defaultDeserializeRootElementName = "root"
  299. )
  300. {
  301. if (needRoot)
  302. {
  303. json = string.Format
  304. (
  305. @"{{ {1}{0}{2} }}"
  306. , " : "
  307. , defaultDeserializeRootElementName
  308. , json
  309. );
  310. }
  311. //XmlDocument xmlDocument = JsonConvert.DeserializeXmlNode(json, defaultDeserializeRootElementName);
  312. var xDocument = JsonConvert
  313. .DeserializeXNode
  314. (
  315. json
  316. , defaultDeserializeRootElementName
  317. );
  318. var xml = xDocument
  319. .Elements()
  320. .First()
  321. .ToString();
  322. return xml;
  323. }
  324. public static T DeserializeByJTokenPath<T>
  325. (
  326. string json
  327. , string jTokenPath = null //string.Empty
  328. )
  329. {
  330. var jObject = JObject.Parse(json);
  331. var jsonSerializer = new JsonSerializer();
  332. if (string.IsNullOrEmpty(jTokenPath))
  333. {
  334. jTokenPath = string.Empty;
  335. }
  336. var jToken = jObject.SelectToken(jTokenPath);
  337. using (var jsonReader = jToken.CreateReader())
  338. {
  339. return
  340. jsonSerializer
  341. .Deserialize<T>(jsonReader);
  342. }
  343. }
  344. public static string Serialize
  345. (
  346. object target
  347. , bool formattingIndented = false
  348. , bool keyQuoteName = false
  349. )
  350. {
  351. string json = string.Empty;
  352. using (StringWriter stringWriter = new StringWriter())
  353. {
  354. using (var jsonTextWriter = new JsonTextWriter(stringWriter))
  355. {
  356. jsonTextWriter.QuoteName = keyQuoteName;
  357. jsonTextWriter.Formatting = (formattingIndented ? Formatting.Indented : Formatting.None);
  358. var jsonSerializer = new JsonSerializer();
  359. jsonSerializer.Serialize(jsonTextWriter, target);
  360. json = stringWriter.ToString();
  361. }
  362. }
  363. return json;
  364. }
  365. public static void ReadJsonPathsValuesAsStrings
  366. (
  367. string json
  368. , string[] jsonPaths
  369. , Func<string, string, bool> onReadedOncePathStringValueProcesssFunc = null
  370. )
  371. {
  372. using (var stringReader = new StringReader(json))
  373. {
  374. using (var jsonReader = new JsonTextReader(stringReader))
  375. {
  376. bool breakAndReturn = false;
  377. while
  378. (
  379. jsonReader.Read()
  380. &&
  381. !breakAndReturn
  382. )
  383. {
  384. foreach (var x in jsonPaths)
  385. {
  386. if (x == jsonReader.Path)
  387. {
  388. if (onReadedOncePathStringValueProcesssFunc != null)
  389. {
  390. var s = jsonReader.ReadAsString();
  391. breakAndReturn
  392. = onReadedOncePathStringValueProcesssFunc
  393. (
  394. x
  395. , s
  396. );
  397. if (breakAndReturn)
  398. {
  399. break;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }
  408. public static IEnumerable<TElement>
  409. DeserializeToFromDictionary<TKey, TValue, TElement>
  410. (
  411. string json
  412. , Func<TKey, TValue, TElement> OnOneElementProcessFunc
  413. )
  414. {
  415. //IEnumerable<TElement> r = default(IEnumerable<TElement>);
  416. return
  417. DeserializeByJTokenPath<Dictionary<TKey, TValue>>(json)
  418. .Select
  419. (
  420. (x) =>
  421. {
  422. var rr = OnOneElementProcessFunc(x.Key, x.Value);
  423. return rr;
  424. }
  425. );
  426. //return r;
  427. }
  428. }
  429. }
  430. namespace Microshaoft
  431. {
  432. using System;
  433. using System.Activities.Tracking;
  434. public class CommonTrackingParticipant : TrackingParticipant
  435. {
  436. public Func<TrackingRecord, TimeSpan, bool> OnTrackingRecordReceived;
  437. protected override void Track(TrackingRecord record, TimeSpan timeout)
  438. {
  439. var r = false;
  440. if (OnTrackingRecordReceived != null)
  441. {
  442. r = OnTrackingRecordReceived(record, timeout);
  443. }
  444. }
  445. }
  446. }

WorkFlowHelper的更多相关文章

  1. WF4与MVC结合示例

    很多初学者,首先最想解决的问题是:如何将WF与MVC程序相结合.由于Web程序属于长时间运行的流程,因此持续化功能的运用就非常重要了. 本文将结合书签.WorkflowApplication.生命周期 ...

  2. AJAX请求.net controller数据交互过程

    AJAX发出请求 $.ajax({ url: "/Common/CancelTaskDeal", //CommonController下的CancelTaskDeal方法 type ...

  3. 工作流2013 assign to问题

    根据您的确认, 该问题已经通过我们所提供的方案进行修改后测试通过, 问题解决. 以下为该问题的产生原因: SharePoint 2013使用的默认认证机制与2007不一样,  2007使用的是Wind ...

  4. K2工作流引擎Demo

    ---恢复内容开始--- 以前的工作都是电商网站形式的,从未接触过工作流相关工作,新公司是传统制造业行业,我进的这个组又是做工作流这块相关工作的,所以避免不了和工作流打交道. 这边工作流主要用K2来做 ...

随机推荐

  1. github怎么退出组织和删除自己创建的组织

    1. 点击头像,进入settings 2. 点击左侧菜单中的 Organizations 切换到Origanizations后,右侧面板中会出现所有的oragnizations,我这里只有一个,是我自 ...

  2. [整理]AngularJS移动端开发遇到的问题

    最近在开发一个移动WebAPP的小项目,因为之前一直使用AngularJS, 对于这个项目,废话不多说,拿过来就用上了. 开发过程是一路通畅和舒服的,但是,却忽略了一个非常重要的问题,移动2G环境下的 ...

  3. nodejs net模块

    net模块是同样是nodejs的核心模块.在http模块概览里提到,http.Server继承了net.Server,此外,http客户端与http服务端的通信均依赖于socket(net.Socke ...

  4. java合集框架第一天

    文章目录 1 collection接口 2  list接口 3 Iterator 4 Vertor 5  ArrayList 6 LinkedList 主体部分: (1)collection Java ...

  5. jq 个性的隔行变色

      效果图大致这样: 我的html格式部分这样:/*不过他们都说我的dom结构不太合理,同意.BUT,我就是不想写表格而写成的这样的,所以后面jq部分也要迁就了*/ <div class=&qu ...

  6. 如何在Android应用中引入外部网页

    在某些情况下,我们需要在Android应用中引入外部网页,这里记录一下如何操作(其实很简单^.^). 先介绍一下开发环境: 开发工具:Android Studio 1.5 SDK API版本:17 操 ...

  7. php curl 多线程方法

    <?php /** * curl 多线程 * @param array $array 并行网址 * @param int $timeout 超时时间 * @return array */ fun ...

  8. web自动化工具-liveStyle

    web自动化工具-liveStyle LiveStyle. The first bi-directional real-time edit tool for CSS, LESS and SCSS主要用 ...

  9. 面向对象Part2

    `变量: 成员变量:又叫全局变量,定义在类中,方法外面. 1).类成员变量.   使用Static 2).实例成员变量.  没有使用Static. 局部变量:出了成员变量,其他的都是局部变量. 1). ...

  10. Tomcat使用详解

    Tomcat简介 官网:http://tomcat.apache.org/ Tomcat GitHub 地址:https://github.com/apache/tomcat Tomcat是Apach ...