WF工作流与Web服务的相互调用 —— 通过Web服务调用Workflow工作流(基础实例)

在开发一个企业ERP系统时,其业务流程是开发的关键,系统往往会将开发好的业务方案发布为Web服务以供外界调用。客户可以通过服务器,互联网等等方式 去调用服务,而解决业务上需要及信息的交换问题。有见及此,微软在.NET 3.0基础上发布了WF,WCF,以及WCS,WPF(为开发表现层而设)。WF,WCF正是解决企业核心问题的关键,通过WF可以轻松地轻松地按照业务 逻辑去实现开发,然后凭借WCF的强大功能把同一服务绑定多个不同的EndPoint,这样客户端与服务与服务器端通讯就不会再受开发语言的影响。

 

在这章里面只是为你讲述如何将WF与Web Service结合使用,而关于WF的开发的详细介绍请参考(WF技术内幕 )。WF与Web Service相互调用分为两情况:

  1. 当客户只希望传入一些基础数据,而直接获取计算结果时,我们可以将WF发布为一个Web Service,这样就可以将WF实现的功能公开到互联网上,通过Web服务可以解决服务器与客户端之间开发语言的束缚。
  2. 当工作流的每个操作步骤都需要调用Web服务来获取结果时,我们则可以使用InvokeWebServiceWorkflow在工作流中调用Web Service来实现(具体操作可参考通过InvokeWebServiceActivity在Workflow工作流中调用Web服务 ) 。

在开发中小型ERP开发初期,往往都会把重点放在业务流程上,只要深入了解企业的业务流程后,将DAL实现为最基础的单表操作,然后就可以轻松地以 WF实现业务层的开发,最后就可以把WF作为服务公布在互联网上(当然在构建中大型系统时,会实现多层开发模式,使用工作流直接去实现业务逻辑,最后以 Web Service方式公开服务)。
而在这里,重点不是介绍ERP的开发,而只是想介绍一下如何将WF公开为Web服务,下面我们还是以“Hello World”为例子。
首先开发一个接口    

View Code
namespace Microsoft.IService
{
publicinterface IService_T1
{
string DoWork();
}

}

然后新建一个顺序工作流,分别插入WebServiceInputActivity和WebServiceOutputActivity作为启动,结束 项。WebServiceInputActivity是服务的启动标记,而WebServiceOutputActivity是服务的结束标记。

       注意,把webServiceInputActivity1的InActivating设置为True,这是证明webServiceInputActivity1为此工作流的启动项的标志。 然后InterfaceType设置为对应接口Microsoft.IService.IService_T1,将MethodName方法名设置为DoWork

在工作流的方法中设置codeActivity的执行方法里面设置方法内容

View Code
namespace Microsoft.Workflows
{

publicsealedpartialclass Workflow4 : SequentialWorkflowActivity
{
publicstring Data ;

public Workflow4()
{
InitializeComponent();
}

privatevoid codeActivity1_ExecuteCode(object sender, EventArgs e)
{
Data
="Hello World";
}
}

}

最后,在webServiceOuputActivity的InputActivityName设置为 webServiceInputActivity1,表示通过webServiceOutputActivity1来结束 webServiceInputActivity1,然后把ReturnValue属性设置为Data

这时候,基础的设置已经完成,现在在项目属性上选择“作为Web服务发布”。

此时,系统会自动把Workflow发布为*.asmx,生成的.asmx文件如下:

<%@ WebService class="Microsoft.Workflows.Workflow4_WebService " %>

//此处Class名称与Workflow的空间名和类名相对应

再为服务添加必要的配置文件:

View Code
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="WorkflowRuntime" type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</configSections>
<WorkflowRuntime Name="WorkflowServiceContainer">
<Services>
<add type="System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add type="System.Workflow.Runtime.Hosting.DefaultWorkflowCommitWorkBatchService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" UnloadOnIdle="true" LoadIntervalSeconds="5" ConnectionString="Data Source=LESLIE-PC;Initial Catalog=WorkflowPersistence;Integrated Security=True"/>

//这里是为Workflow添加SQL数据库持久化服务,这是可选设置。
</Services>
</WorkflowRuntime>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
<httpModules>
<add type="System.Workflow.Runtime.Hosting.WorkflowWebHostingModule, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="WorkflowHost"/>
</httpModules>
</system.web>
</configuration>

这时候在网络上第一次调用服务时,系统会正常操作。

<? xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">Hello World</string>

但第二次调用。系统将提示提示一个错误,这是因为WorkflowWebHostingModle HTTP模块是使用Cookies来存储工作流的GUID的,当工作流在服务器返回时,SQL持久化数据器里将不再储存这个GUID。这时候需要关闭浏览 器,重新启动才能正常运行。
System.InvalidOperationException: 在状态持久性存储中找不到 ID 为“3a8b9688-fb3f-4a10-bb84-6bf99c30119a”的工作流。
 
总结,通过WF可以轻松企业的业务逻辑,再结合Web Service在互联网上发布,就可以供给不同客户端使用,从而摆脱开发语言的困扰。以上的例子只是将WF发布为Web服务的最基础用法,因为 Workflow对象只会暂时存在,当服务结束时,Workflow对象就会被清理。但是很多时候系统可能要求能维持状态并支持多个Web Service调用的工作流,下一章将为你详细介绍有关内容。
 
对 .NET  开发有兴趣的朋友欢迎加入QQ群:162338858 共同探讨 !
 
 

posted on 2011-05-06 09:23  风尘浪子  阅读(2741)  评论(0编辑  收藏  举报

导航