Workflow creation mode without code

Source: Internet
Author: User

Workflow Creation Mode

WF has three workflow creation modes:

Code only:Use Code only. For example, if we create workflow1., two files, workflow1.cs and workflow1.desiger. CS, will be generated. The latter is automatically generated. The former is us.
Implement the logic and design the workflow. If there are rules, a serialized. Rules file will be generated. When a project is generated, the. Rules file serves as the embedded resource of the Assembly. This
Method can only be changed through dynamic update during running.

Code separation:In. xoml format, this method of workflow serialization is stored in a file with the. xoml extension, and its code is saved in. xoml. CS. In this mode
The workflow compiler generates a temporary C # class from the. xoml file and compiles it with. xoml. CS.

No code:There is only one. xoml file. This mode is not supported in vs templates. In this mode, only existing workflow types and Members can be used. See:

We can clearly understand the features of this mode. This mode has great flexibility and can load the workflow Tag file to the runtime engine of the workflow through the Host Program. No need to re-compile
Translate the entire workflow. The following describes how to use this method.

1. First, we define a workflow base class to provide the attributes and methods required by the workflow. The Code is as follows:

using System;using System.Workflow.Activities;namespace CaryWFLib{    public class CaryBaseWorkflow : SequentialWorkflowActivity    {        public int Number { get; set; }        public void Condition(object sender, ConditionalEventArgs e)        {            e.Result = (Number > 0);        }    }}
2. Then we implement a custom activity caryprintactivity to complete the logic section. The main function is to output the message. The Code is as follows:
namespace CaryWFLib{    public partial class CaryPrintActivity: Activity    {        public static DependencyProperty MessageProperty            = System.Workflow.ComponentModel.DependencyProperty.Register(                "Message", typeof(String), typeof(CaryPrintActivity));        [Description("A string message to write")]        [Category("Pro Workflow")]        [Browsable(true)]        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]        public String Message        {            get            {                return ((String)(base.GetValue(CaryPrintActivity.MessageProperty)));            }            set            {                base.SetValue(CaryPrintActivity.MessageProperty, value);            }        }        public CaryPrintActivity()        {            InitializeComponent();        }        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)        {            if (Message != null)            {                Console.WriteLine(Message);            }            return base.Execute(executionContext);        }    }}

3. Then we can implement the workflow without Code. The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Ns0: carybaseworkflowx: Name = "carynocodeworkflow" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/workflow" xmlns: ns0 = "CLR-namespace: carywflib; assembly = carywflib "> <ifelseactivity X: Name =" ifelseactivity1 "> <ifelsebranchactivity X: Name =" ifelsebranchactivity1 "> <ifelsebranchactivity. condition> <codecondition condition = "{activitybind name = carynocodeworkflow, Path = condition}"/> </ifelsebranchactivity. condition> <ns0: caryprintactivity message = "the number you entered is greater than 0" X: Name = "caryprintactivity1"/> </ifelsebranchactivity> <ifelsebranchactivity X: name = "ifelsebranchactivity2"> <ns0: caryprintactivity message = "the number you entered is smaller than or equal to 0" X: name = "caryprintactivity2"/> </ifelsebranchactivity> </ifelseactivity> </ns0: carybaseworkflow>

Note:
. The root node is ns0: carybaseworkflow, which is bound to the activity condition.
. In the workflow creation mode without code, the X: class attribute cannot be included.
3. If the rule is declared in the form of. Rules, the above condition section should be:
<Ruleconditionreferenceconditionname = "condition"/>

. You need to set the workflow file property generation operation to none.

4. Host part:

namespace NoCodeWorkflow{    class Program    {        static void Main(string[] args)        {            using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())            {                AutoResetEvent waitHandle = new AutoResetEvent(false);                workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) 
{ waitHandle.Set(); }; workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; Dictionary<String, object> paras = new Dictionary<string, object>(); paras.Add("Number", 2); try { //XmlReader reader = XmlReader.Create("..\\..\\CaryNoCodeWorkflow.xoml"); XmlReader reader = XmlReader.Create("..\\..\\CaryNoCodeRulesWorkflow.xoml"); XmlReader readerRule = XmlReader.Create("..\\..\\CaryNoCodeRulesWorkflow.rules"); //WorkflowInstance instance = workflowRuntime.CreateWorkflow(reader, null, paras); WorkflowInstance instance = workflowRuntime.CreateWorkflow(reader, readerRule, paras); instance.Start(); } catch (WorkflowValidationFailedException ex) { foreach (ValidationError error in ex.Errors) { Console.WriteLine(error.ErrorText); } } catch (Exception ex) { Console.WriteLine(ex.Message); } waitHandle.WaitOne(); } } }}
In the host Program, we use another overload of workflowruntime. createworkflow. The final result is as follows:
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.