zoukankan      html  css  js  c++  java
  • WorkFlowHelper

    
    /*
    # Microshaoft
    /r:System.Xaml.dll
    /r:System.Activities.dll
    /r:System.Activities.DurableInstancing.dll
    /r:System.Runtime.DurableInstancing.dll
    /r:"D:Microshaoft.Nuget.PackagesNewtonsoft.Json.7.0.1lib
    et45Newtonsoft.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);
                }
            }
        }
    }
    
    
  • 相关阅读:
    VS2017离线安装与Oracle数据库开发环境搭建
    拒绝“高冷”词汇!初学C#中的委托
    拒绝“高冷”词汇!初学C#中实用的泛型!
    误删Django的model中的表解决办法
    Django-ORM操作
    请求头获取用户设备、点赞
    随机验证码、图片验证码和邮箱发送用户验证码
    Django的Form验证(2)
    Django的Form验证
    Pycharm导入Django项目
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/5064828.html
Copyright © 2011-2022 走看看