Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Working with Windows Workflow Foundation in ASP.NET

Working with Windows Workflow Foundation in ASP.NET

作者头像
张善友
发布于 2018-01-29 03:43:25
发布于 2018-01-29 03:43:25
1K0
举报
文章被收录于专栏:张善友的专栏张善友的专栏

September of 2005, Microsoft unleashed Windows Workflow Foundation (Windows WF) at its semi-annual Professional Developer's Conference. As one of the pillars of the WinFX APIs, Windows WF provides developers with a common framework on which to develop process driven and workflow-centric applications.

Currently, when organizations wish to automate business processes the standard answer is to assemble a team of developers to write the appropriate code. While this approach has generally served organizations well, it has some inherent problems. To understand why, you need to understand some fundamental characteristics of a workflow.

A workflow is essentially a way of documenting the activities involved in completing a unit of work. Typically, work "flows" through one or more activities during processing. These activities can be performed by either machines or by people, and can be as simple as defining the sequence of pages in an Internet application, or as complex as managing documents or products that must be seen, altered, and approved by any number of people.

Because so many workflows must allow for human involvement, they can take long periods of time to complete, ranging from hours to months or longer. For example, people involved in the process may be unavailable, out-of-town, or busy with other tasks; therefore, workflows must be able to persist themselves during periods of inactivity. Furthermore, processes implemented solely using code can be difficult for non-technical people to understand and for developers to change. This and other factors are the target of generic workflow frameworks such as Windows WF, which aim to make creating, altering, and managing workflows easier, both by giving them a visual interface and by defining a common API.

You can host Windows WF workflows in any type of .NET application, including Windows Forms, console applications, Windows Services, and ASP.NET Web applications. Each type requires special considerations. While examples that showcase hosting workflows in Windows Forms and console applications are already plentiful, this article focuses on issues facing ASP.NET developers who wish to integrate workflow into their applications.

Author's Note: The code provided with this article was written with Beta 1 of Windows WF and Beta 2 of Visual Studio 2005. You can find information on installing Windows WF at www.windowsworkflow.net. While this article covers some basics of Windows WF, there are other resources available that cover the essentials. I've assumed that readers have at least some exposure to Windows WF. The purpose of this article is to dig into Windows WF and ASP.NET, rather than to cover Windows WF at a high level.

Windows WF and the MVC Pattern

One common way in which you might use Windows WF in an ASP.NET application is to implement a Model-View-Controller (MVC) approach. Essentially, the goal of MVC is to separate the presentation layer, application logic, and application flow logic from each other.

To illustrate how this would be useful in an ASP.NET application, consider a help desk ticket workflow scenario. A business user starts the workflow by filling out an ASP.NET Web form and clicking a submit button. Next, a server notifies a help desk employee using a Windows Forms application that a new ticket is available. The help desk employee would then work on the problem, and eventually close the ticket. If this workflow scenario were developed using Windows WF, all the process logic and flow could be contained in the workflow itself, and the ASP.NET application would need to know nothing about the logic.

This scenario provides some solid evidence that separating presentation from logic is a good thing. Because the process of handling help desk requests is common, if the logic were implemented using C# or VB.NET code in several different .NET applications, you'd run the risk of duplicate code or worse, different implementations of the same business process in completely different code. But if you implement the process in Windows WF, the developers of applications that need the process would need to modify the steps in only one place—the workflow itself—without worrying about changing logic in their applications. Code duplication and the ambiguity of where to implement process can be mitigated with Windows WF.

When implementing the MVC architecture in ASP.NET using Windows WF, developers should attempt to build workflows independent of the applications in which they will be hosted. This will assist in keeping logic separate from presentationand maintain a high degree of independence between the order of workflow steps and page flow in the Web application.

A novice Windows WF developer might be tempted to develop a workflow with a set number of activities in a certain order and afterward, develop a set of ASP.NET Web forms that flow from one to another in the same order. Unfortunately, while that seems logical, it's counterproductive, because you would be implementing the workflow logic twice. Web page X should not need to know whether it needs to go to Page Y or Page Z to implement the workflow steps correctly. Instead, the workflow (the model) should tell ASP.NET (the controller) what the next step is, and then ASP.NET should determine which page (the view) to display. In this way, each page requires little knowledge of the overall process; it only needs to know how to complete one distinct activity and let the workflow worry about how the pages flow from one to the next. This separation provides developers with a great deal of flexibility related to the order of page flow. For example, if you decided to change the page display sequence, you could easily do that in the workflow, without changing one line of code in the ASP.NET application.

A Simple Workflow MVC Example To demonstrate this idea, I'll show you a simple ASP.NET application and workflow. The oversimplified workflow describes a process that collects some personal information from an external application, and then displays it. Here are the steps:

  1. Call a method that signifies a request for a person's name; the workflow uses the InvokeMethod activity (see Figure 1).
  2. Wait until an event is fired signifying the receipt of a name; the workflow uses the EventSink activity for this step.
  3. Obtain an e-mail address from the host using a similar call.
  4. Wait for an event signifying the receipt of the address.
  5. After receiving both the name and e-mail address the workflow launches an InvokeMethod activity to send the personal data to the calling application. This last step does not make much sense in a real-world scenario. More likely, you'd call a Web service to send the data to some other system, or place it into a database.

To implement this workflow in ASP.NET you need a page to collect a person's name, a page to collect the email address, and a page that displays the personal data. Remember, the data entry forms should know nothing about what has happened before or what happens next. The same goes for the display page. However, the ASP.NET application has to somehow know which page to display to the user; this where the controller comes in. This example uses an HttpHandler to implement the solution. The custom handler, called WorkflowControllerHandler, deals with the following tasks:

  • Obtaining a reference to the workflow runtime.
  • Obtaining a reference to or starting a new workflow instance (depending on whether a workflow instance is already started).
  • Setting up communications between the controller and the workflow.
  • Handling events from the workflow.
  • Telling ASP.NET which page needs to be displayed depending on which stage in the workflow is currently being executed.

As you can see, the custom handler essentially takes care of all the dirty work related to Windows WF and page control, letting the individual ASP.NET pages remain "dumb" about what's going on behind the scenes. The only thing the Web forms need to worry about is performing the specific task at hand and passing the necessary data to the controller. By default, Windows WF works in an asynchronous model. This means that when an application host starts a workflow instance, control immediately returns to the host while workflow execution continues on another thread. This can be very useful in a Windows Forms application where continual responsiveness of the user interface is highly desirable. Using the asynchronous model the workflow executes in the background and the user can continue to manipulate the application. In a Web application, however, this type of behavior may not be desirable, because control would usually only be returned to the user after the server completes a unit of work. This is where the extensibility of Windows WF shines. In Windows WF, developers can utilize or write "runtime services" to monitor and even modify the workflow runtime. Examples include:

  • Persistence services that store workflow state between execution and idle time.
  • Tracking services that output information about workflow execution to some medium
  • Transaction services that help maintain data integrity in the workflow execution process
代码语言:js
AI代码解释
复制
In addition, threading services let developers control how workflow instances are executed. As discussed, the workflow runtime will by default run instances asynchronously, on a separate thread from the host. But as this is probably not desirable in ASP.NET, you need to swap out the default workflow threading service. Luckily, Microsoft already provides a solution for such a purpose, the ASPNetThreadingService. To make this change, you can either manually add the ASPNetThreadingService to the workflow runtime services in code, or do it in the web.config file. The sample application uses configuration. In the WorkflowRuntime/Services section of the web.config (see Listing 1), add a line similar to the following:
   <add type=
      "System.Workflow.Runtime.Hosting.ASPNetThreadingService, 
      System.Workflow.Runtime, Version=3.0.00000.0, 
      Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

Implementing the Controller Logic

Next you need to implement the controller logic as an HttpHandler. To construct the controller, all you need to do is create a class called WorkflowControllerHandler that implements the IHttpHandler interface. So far, nothing you've seen is specially related to Windows WF—this is functionality native to ASP.NET (see this article for more information).

In the WorkflowControllerHandler class, the IHttpHandler interface method called ProcessRequest handles a Web request from the ASP.NET application. Here, you need to obtain a reference to a static workflow runtime instance, setup the event handlers for the workflow, and kick off the workflow execution. Before starting a workflow instance, you need to check to see if the request's query string values contain a GUID representing a workflow instance ID. If the ID is present, you know that an instance is already running, so you can obtain a reference to that instance, and continue execution. If the ID is not present, you need to create a new instance and begin the execution process by calling the StartWorkflow method of the workflow runtime.

After starting an instance, the event handlers will manage communications to and from the workflow. Because the purpose of this article is not to cover local communication services, I will not go into great detail on that subject here. Rather, I will cover the high level details, and again, how this might work in an ASP.NET application. To facilitate the communication process, you'll need several .NET interfaces that represent the messages sent to and from the workflow and host. You will find these in the WorkflowClassLibrary project in the sample code provided with this article. You'll also find classes that implement these interfaces and the respective functionality necessary for the workflow to function.

Take another look at the web.config file. Notice that next to the ASPNetThreadingService element discussed earlier, there will be three elements representing the communication service classes:

代码语言:js
AI代码解释
复制
   <add type="Workflow.RuntimeServices.GetNameService, 
     Workflow.Library, Version=1.0.0.0, Culture=neutral, 
     PublicKeyToken=c4620ae819b5257e"/>
   <add type="Workflow.RuntimeServices.GetEmailService, 
     Workflow.Library, Version=1.0.0.0, Culture=neutral, 
     PublicKeyToken=c4620ae819b5257e"/>
   <add type="Workflow.RuntimeServices.SendDataService, 
     Workflow.Library, Version=1.0.0.0, Culture=neutral, 
     PublicKeyToken=c4620ae819b5257e"/>

Also present is an element that instructs the workflow runtime to use the SqlStatePersistenceService. This additional service persists a workflow's state to a SQL Server database between page requests. You must create this database manually ahead of time, but Microsoft provides the SQL scripts to do this. You'll find them in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\Windows Workflow Foundation\SQL folder. Like the threading model service, you could have added these services programmatically, but doing it in the configuration cuts down on code and provides flexibility even after the code is in production. Also in the web.config is a line that adds an HttpModule that supports the Windows WF runtime in ASP.NET, and a line setting up the HttpHandler controller discussed earlier. As you can see, there is a lot going on in the configuration file.

In conclusion, Windows WF provides developers with an extremely versatile and extensible framework on which to develop workflow based applications. Implementing business processes has and will continue to be an important application of technology. Unless you are a technology services firm or ISV, software is there to support the business and its processes. With a tool such as Windows WF developers can approach process development with a new sense of ease and flexibility.

Page 3 of 3

Sample ASP.NET Workflow Application

http://www.devx.com/dotnet/Article/29992/0/page/1

http://www.devx.com/dotnet/Article/29992/0/page/2

http://www.devx.com/dotnet/Article/29992/0/page/3

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2006-08-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
(收藏)搭建.NET Framework 3.0开发环境 及SharePoint 2007/WSS 3环境
第一步:首先您必须安装.NET Framework 3.0,则可以下载其Redistributable Package Microsoft .NET Framework 3.0 Redistributable Package  第二步:Microsoft® Windows® Software Development Kit for Windows Vista™ and .NET Framework 3.0 Runtime Components正式版 这是.NET 3.0程序开发的运行库,里面有你开发WPF、
用户1172164
2018/01/16
1.7K0
Introduction to Model Driven Development with AndroMDA
Introduction AndroMDA (pronounced "Andromeda") is a free and open source extensible generator framework that adheres to the Model Driven Architecture (MDA) paradigm. It transforms UML models into deployable components for your favorite platform. While Andr
张善友
2018/01/30
8620
【C#】转一篇MSDN杂志文:ASP.NET Pipeline: Use Threads and Build Asynchronous Handlers in Your Server-Side Web
序:这是一篇发表在2003年6月刊的MSDN Magazine的文章,现在已经不能在线阅读,只提供chm下载。讲的是异步请求处理那些事,正是我上一篇博文涉及的东西(BTW,事实上这篇杂志阐述了那么搞然并卵),期间有搜索到这篇文章,很受裨益。担心MS哪么时候不再提供下载以及本地保管不便,所以现在誊上来,作为备份,方便日后回顾。
AhDung
2018/09/13
6530
【C#】转一篇MSDN杂志文:ASP.NET Pipeline: Use Threads and Build Asynchronous Handlers in Your Server-Side Web
Mix 10 上的asp.net mvc 2的相关Session
Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hanselman in Lagoon L on Monday at 11:30 AM The web has changed and there's a new way of thinking about your applications. You can't just write some HTML and CSS anymore and expect
张善友
2018/01/31
7710
ASP.NET MVC 5 Authentication Breakdown
In my previous post, "ASP.NET MVC 5 Authentication Breakdown", I broke down all the parts of the new ASP.NET MVC authentication scheme. That's great, but I didn't have a working example that you, a curious developer, could download and play around with. So
阿新
2018/04/12
9790
【翻译】Asp.net Core介绍
ASP.NET Core is a significant redesign of ASP.NET. This topic introduces the new concepts in ASP.NET
脑洞的蜂蜜
2018/02/02
1.2K0
Docker 学习资源整理
本文介绍了Docker技术的一些重要概念、使用场景、优点、缺点以及如何在本地和云上使用Docker。作者还讲解了Docker的技术实现、基础架构、容器管理、镜像管理、日志和监控、网络、安全、集群以及Kubernetes等内容。此外,还介绍了一些有用的工具和插件,以及如何学习和使用Docker。
shaonbean
2018/01/02
3.3K0
How to build Multi-Language Web Sites with ASP.NET 2.0 and VS.Net 2005
Introduction: In order to reach international markets through the Internet, supporting different cultures through our applications is essential for being successful. The .NET Framework 2.0 as well as 1.x comes with an integrated infrastructure for creatin
张善友
2018/01/31
9990
How to build Multi-Language Web Sites with ASP.NET 2.0 and VS.Net 2005
从头编写 asp.net core 2.0 web api 基础框架 (1)
工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相关知识就不介绍了, 这里主要是从头编写一个asp.net core 2.0 web api的基础框架. 我一直在关注asp.net core 和 angular 2/4, 并在用这对开发了一些比较小的项目. 现在我感觉是时候使用这两个技术去为企业开发大一点的项目了, 由于企业有时候需要SSO(单点登录), 所以
solenovex
2018/03/01
2.2K0
从头编写 asp.net core 2.0 web api 基础框架 (1)
Simulate a Windows Service using ASP.NET to run scheduled jobs
Introduction How to run scheduled jobs from ASP.NET without requiring a Windows Service to be instal
阿新
2018/04/12
1.4K0
Top Web Development Technologies and Frameworks
Groundbreaking-Web-App-Development-Tech.png Are you looking forward to staying abreast with the late
用户4822892
2019/07/30
4250
从 WebAPI Beta 更新到WebAPI RC
The official word on changes from Beta to RC for Web API-related topics (filtered from the original page). ASP.NET Web API now uses Json.NET for JSON formatting: The default JSON formatter in ASP.NET Web API now uses Json.NET for JSON serialization. Json.N
张善友
2018/01/29
1.2K0
从 WebAPI Beta 更新到WebAPI RC
【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)
HTTP is not just for serving up web pages. It’s also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a
脑洞的蜂蜜
2018/02/01
1.4K0
【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)
IIS7 beta FREE WEBHOSTING FREE HOSTING FOR ASP.NET 2.0 IIS7[通俗易懂]
From: http://blogs.iis.net/bills/archive/2007/04/28/free-iis7-web-hosting.aspx
全栈程序员站长
2022/11/08
5K0
Windows Server AppFabric Beta 2 已经发布
Windows Server AppFabric Beta 2是一个包含完全功能的AppFabric版本(This build represents our “feature complete” milestone. That is, it contains all the features that we plan to ship in Windows Server AppFabric v1 by Q3 of 2010),也就是RTM的时间是在2010年的Q3,那么在.net 4/vs2010 RTM的时
张善友
2018/01/31
6500
dotnet conf 2023 Agenda
Damian Edwards, Safia Abdalla, David Fowler, Gaurav Seth, Daniel Roth, Glenn Condron, Maddy Montaquila, Maria Naggaga
JusterZhu
2023/11/07
4460
dotnet conf 2023 Agenda
ASP.NET Core | 笔记
将 [EnableCors] 属性与命名策略一起使用在限制支持 CORS 的终结点方面提供了最佳控制。 警告 UseCors 必须按正确的顺序调用 。 有关详细信息,请参阅 中间件顺序。 例如, UseCors 在使用 之前必须 UseResponseCaching 调用 UseResponseCaching 。
yiyun
2022/04/01
4.8K0
ASP.NET Core | 笔记
Features of .Net Development Platform
.Net Framework has been probably the best advancement by Microsoft for web applications, work area applications and site improvement. .Net designers and individuals who have been in steady interface with this stage definitely realize that it has its own library of dialects, structure and further turns of events. Novices and entrepreneurs should realize that from a straightforward design, .Net system has advanced to a total environment offering help and improvement procedures for a wide range of uses
saurabhkumawat
2021/02/24
3760
Features of .Net Development Platform
WCF技术剖析之二:再谈IIS与ASP.NET管道
在2007年9月份,我曾经写了三篇详细介绍IIS架构和ASP.NET运行时管道的文章,深入介绍了IIS 5.x与IIS 6.0HTTP请求的监听与分发机制,以及ASP.NET运行时管道对HTTP请求的处理流程: [原创]ASP.NET Process Model之一:IIS 和 ASP.NET ISAPI [原创]ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline - Part I [原创]ASP.NET Process Model之二:ASP.N
蒋金楠
2018/01/16
1.6K0
WCF技术剖析之二:再谈IIS与ASP.NET管道
dotnetconf 2024 Agenda Day 1
James Montemagno, Damian Edwards, Maddy Montaquila, Maria Naggaga, Steve Sanderson, Safia Abdalla, Daniel Roth, David Ortinau, Filisha Shah
JusterZhu
2025/01/23
950
dotnetconf 2024 Agenda Day 1
相关推荐
(收藏)搭建.NET Framework 3.0开发环境 及SharePoint 2007/WSS 3环境
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档