/*
# 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);
}
}
}
}

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. 入门:Java Map<String,String>遍历及修改

    重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...

  2. EFCore教程

    https://docs.microsoft.com/en-us/ef/core/modeling/alternate-keys aspnet core 教程 https://docs.microso ...

  3. 使用sublime一键格式化XML文件

    1 sublime简介 sublime是一款代码编辑和阅读软件,体积小,运行快,界面非常简洁漂亮.官方地址:https://www.sublimetext.com/ 2 在sublime上安装插件 使 ...

  4. jQuery知识点二 实现隔行变色

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  5. Javascript操作DOM常用API总结

    基本概念 在讲解操作DOM的api之前,首先我们来复习一下一些基本概念,这些概念是掌握api的关键,必须理解它们. Node类型 DOM1级定义了一个Node接口,该接口由DOM中所有节点类型实现.这 ...

  6. [Machine Learning & Algorithm] 神经网络基础

    目前,深度学习(Deep Learning,简称DL)在算法领域可谓是大红大紫,现在不只是互联网.人工智能,生活中的各大领域都能反映出深度学习引领的巨大变革.要学习深度学习,那么首先要熟悉神经网络(N ...

  7. [Evolutionary Algorithm] 进化算法简介

    进化算法,也被成为是演化算法(evolutionary algorithms,简称EAs),它不是一个具体的算法,而是一个“算法簇”.进化算法的产生的灵感借鉴了大自然中生物的进化操作,它一般包括基因编 ...

  8. scrapy 学习笔记

    1.scrapy 配合 selenium.phantomJS 抓取动态页面, 单纯的selemium 加 Firefox浏览器就可以抓取动态页面了, 但开启窗口太耗资源,而且一般服务器的linux 没 ...

  9. android onCreate中获取view宽高为0的解决方法

    view.post(runnable) 通过post可以将一个runnable投递到消息队列的尾部,然后等待UI线程Looper调用此runnable的时候,view也已经初始化好了. view.po ...

  10. Session、Cookie--2017年1月3日

    Session Session 对象用于存储用户的信息.存储于 session 对象中的变量持有单一用户的信息,并且对于一个应用程序中的所有页面都是可用的.    Session 对象 当您操作某个应 ...