代码改变世界

Windows Workflow RC HOL学习笔记(六):创建一个基本的活动

2007-05-15 11:50  努力学习的小熊  阅读(1021)  评论(0编辑  收藏  举报

本文内容来自Microsoft Hands-on Labs for Windows Workflow Foundation RC中的HOL02中的第二个练习,其中包括六个任务:

使用Activity Designer创建一个复杂活动Composite Activity

Ø         创建一个工作流项目

Ø         创建一个Activity Project活动项目

Ø         生成工作流解决方案

Ø         添加一个执行组件

Ø         生成项目

Ø         更新Activity来发送Email

 

这个练习中将创建另一个工作流项目包含一个可以自定义属性的Activity。你可以使用一个自定义属性字段来指定Email消息。将为这个基本的自定义活动添加一个执行组件来添加到工作流。这个执行组件(executor component)使用ASP.NETAPI和包含在字段中的email信息数据。

1.打开Visual Studio 2005,新建一个Sequential Workflow Console Application类型的项目。路径:C:\Windows Workflow Foundation\Labs\Lab02。项目名称为CustomPropertySample

2.将Workflow1.cs文件重命名为SendMailWorkflow.cs

任务一完成。

 

3.新建一个工作流项目,选择Workflow Activity Library模板。在Name字段中输入SendMailActivityLibrary

4.选择添加到解决方案,点击OK

5.将Activity1.cs文件重命名为SendMailActivity.cs

6.点击SendMailActivity,查看属性窗口。
   

7.在属性窗口中选择Base Class属性,点击右边出现的“...”按钮。
   

8.在弹出的窗口中选择类型选项卡,选择System.Workflow.ComponentModel作为基类。

9.选择System.Workflow.ComponentModel后,右边列出了两个基类:ActivityComposite Activity

10.选择Activity。效果如下:
   

11.转到SendMailActivity.cs代码视图。

12.在SendMailActivity构造函数后面按两次回车。按鼠标右键选择“插入代码段”。
   

13.选择Workflow
   

14.选择DependencyProperty - Property
   

15.这个片段将添加一些可以自定义的代码模板。

16.以下是一些特性attributes的描述,可以添加到属性的标签中。

Name

Type

Description

Browseable

Boolean

Indicates whether this property appears in the Properties window.

Category

String

A user-defined category for the property.

Description

String

A description for the property.

DesignerSerializationVisibility

Visible, Hidden, or Content

Determines how and if properties will be serialized.

Visible (the default) – the property will be serialized normally.

Hidden – prevents property serialization

Content – used for collection properties. The collection object itself is not serialized, however the contents of the collection are.

If a developer chooses a collection type, this property will be set to Content.  If a developer chooses a non-serializable type, this property will be set to Hidden.

ValidationVisibility

Optional, Required, or Hidden

Specifies how the property’s value is validated.

Optional (the default) – the property can accept null values.

Required – The property must be set to a non-null value and is checked to ensure that this is the case.

Hidden – There is no automatic validation of the property’s value.

If ValidationVisibility is set to ‘Required,’ when the component is reused, it will require the property to be configured via smart tags, obviating the need for a check in the activity’s Validator class.

 

17.在生成的代码样板中输入这些字段的值。使用Tab键可以在这些绿色底的值之间移动,用回车键完成输入。

NameTo

TypeString

DescriptionTo Email Address

CategoryEmailActivity

设置完毕后,代码如下:

        public static DependencyProperty ToProperty = System.Workflow.ComponentModel.DependencyProperty.Register("To", typeof(string), typeof(SendMailActivity));

 

        [Description("To Email Address")]

        [Category("EmailActivity")]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

        public string To

        {

            get

            {

                return ((string)(base.GetValue(SendMailActivity.ToProperty)));

            }

            set

            {

                base.SetValue(SendMailActivity.ToProperty, value);

            }

        }

18.使用同样的方法输入以下属性:

Name

Type

Description

Category

From

string

From Email Address

EmailActivity

Subject

string

Subject of Email

EmailActivity

Body

string

Body of Email

EmailActivity

这样这个activity就拥有了4个属性。

任务二完成。

 

19.编译生成解决方案。这一步会在工具箱中添加一个SendMailActivity。切换到SendMailWorkflow设计视图如下:
   

20.在设计视图中添加一个SendMailActivity,并修改Name属性为sendMail

21.这时在属性窗口中可以看到刚才自定义的属性。
   

22.现在就可以设置这些自定义属性的值了:

Bodybody of email

Fromemail@any.domain

Subjectsubject of email

Toemail@some.domain

任务三完成。

 

任务四:添加一个Executor Component

23.切换到SendMailActivity代码视图。

24.在代码文件的底部,在Activity类内定义的范围内,override覆写Execute method。在这个方法中添加如下代码,可以访问activity的其中一个自定义属性并显示它的内容。

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

        {

            Console.WriteLine(To);

            return ActivityExecutionStatus.Closed;

        }

任务四完成。

 

25.重新生成解决方案。

26.切换到SendMailWorkflow设计视图,按Ctrl+F5选择不调试运行。刚才设置的To属性的值会显示出来。

27.按回车键结束。

任务五完成。

 

任务六:更新Activity发送Email

28.首先,需要在实验及其上建立email。这个实验是假设SMTP server已经实验机器上建立好了。

29.切换到SendMailActivity代码视图。在顶部添加命名空间。

using System.Net.Mail;

30.在方法中使用MailMessageSMTPClient类实现发送Email。代码如下:

            MailAddress toAddress = new MailAddress(To);

            MailAddress fromAddress = new MailAddress(From);

 

            MailAddressCollection addresses = new MailAddressCollection();

            addresses.Add(toAddress);

 

            MailMessage msg = new MailMessage(fromAddress, toAddress);

 

            msg.Subject = Subject;

            msg.Body = Body;

 

            SmtpClient mail = new SmtpClient("localhost");

 

            mail.Send(msg);

            return ActivityExecutionStatus.Closed;

31.切换到SendMailActivity设计视图。设置sendMail ActivityTo属性为一个真实的Email地址。

32.生成解决方案。按Ctrl+F5选择不调试运行。这里我使用的公司的Exchange Server进行实验的,用OutLook收邮件,实验成功。

任务六完成。

 

参考资料:

Microsoft Hands-on Labs for Windows Workflow Foundation RC