Create and start a workflow using xoml or XAML

Source: Internet
Author: User

In Workflow Foundation, multiple workflow design methods are provided. For example

  1. Code-only method (C #)
  2. Code separation method (xoml + C #)

You can select from the Project template provided by Visual Studio.

They look like the following in the designer:

Xoml is a special XML format that describes the process. For example, the following process

It is described in xoml as follows:

<SequentialWorkflowActivity x:Class="WorkflowConsoleApplication1.Workflow2" x:Name="Workflow2" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">    <CodeActivity x:Name="codeActivity1" />    <IfElseActivity x:Name="ifElseActivity1">        <IfElseBranchActivity x:Name="ifElseBranchActivity1">            <CodeActivity x:Name="codeActivity2" />        </IfElseBranchActivity>        <IfElseBranchActivity x:Name="ifElseBranchActivity2">            <CodeActivity x:Name="codeActivity3" />        </IfElseBranchActivity>    </IfElseActivity></SequentialWorkflowActivity>

 

Although the above XML can indeed describe the process, some special logic involved in the process may still need to write code. These two parts need to be compiled into a type.

Therefore, there is no essential difference between the above two methods.

 

Here we will introduce a pure xoml method:Can we use xoml to define the process? In this case, we can implement a more dynamic process, because there is no need to compile additional code, but we can modify the process by directly modifying the xoml file..

 

The answer is:Yes. However, there is a premise that since we do not want to use code, the activities used in the process should be able to complete the work through configuration..

 

Generally, we need to write a custom activity to achieve this requirement.

For example, we can make a simple activity, which prints a message on the screen based on a user input parameter.

using System;using System.ComponentModel;using System.ComponentModel.Design;using System.Collections;using System.Linq;using System.Workflow.ComponentModel;using System.Workflow.ComponentModel.Design;using System.Workflow.ComponentModel.Compiler;using System.Workflow.ComponentModel.Serialization;using System.Workflow.Runtime;using System.Workflow.Activities;using System.Workflow.Activities.Rules;namespace WorkflowConsoleApplication1{    public partial class MyActivity :Activity    {        public MyActivity()        {            InitializeComponent();        }        public string Message { get; set; }        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)        {            Console.WriteLine(Message);            return base.Execute(executionContext);        }    }}

To demonstrate how to use this custom activity directly using the method defined by xoml, we can generate a section of xoml using the following code.

                var workflow = new SequenceActivity();                workflow.Activities.Add(new MyActivity() { Message = "Hello,World" });                var serializer = new WorkflowMarkupSerializer();                serializer.Serialize(                    XmlWriter.Create("test.xoml",new XmlWriterSettings(){CloseOutput=true}), workflow);

The preceding Code creates a sequenceactivity and adds the custom activity to it.

After the code is executed, the generated test. xoml content is as follows:

<?xml version="1.0" encoding="utf-8"?><SequenceActivity x:Name="SequenceActivity" xmlns:ns0="clr-namespace:WorkflowConsoleApplication1;Assembly=WorkflowConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">    <ns0:MyActivity Message="Hello,World" x:Name="myActivity1" /></SequenceActivity>
 

Here, take a closer look at the differences with the previous xoml section.

  1. The top-level activity does not contain the X: class definition. (Because there is no code file, the class will not start from)
  2. All internal custom activities must pre-define the relevant namespace and reference them to the Assembly
  3. Custom activity settings are directly completed through attributes.

Then, how can we use the above definition to create and run the process?

                var instance2 = workflowRuntime.CreateWorkflow(                    XmlReader.Create("test.xoml"));                instance2.Start();

If you need to modify the process definition, you can directly modify the xoml file without compiling the program. For example, modify the message to "Hello, workflow"

The running result is as follows:

 

In fact, as long as we are willing, we can even save these defined strings in apsaradb, and then create and start the process through the following code.

VaR definition = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Sequenceactivity X: Name = \ "sequenceactivity \" xmlns: ns0 = \ "CLR-namespace: workflowconsoleapplication1; Assembly = workflowconsoleapplication1, version = 1.0.0.0, culture = neutral, publickeytoken = NULL \ "xmlns: x = \" http://schemas.microsoft.com/winfx/2006/xaml\ "xmlns = \" comment "> <ns0: myactivity message = \" Hello, workflow \ "X: name = \ "myactivity1 \"/> </sequenceactivity> "; // You can also read the definition var reader = xmlreader from the database. create (New stringreader (Definition); var workflow = workflowruntime. createworkflow (Reader); workflow. start ();

 

[Summary]

This article explains how to define and create a Workflow Based on xoml through examples.

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.