前几篇文章已经为大家介绍过Web服务与Workflow工作流之间的相互调用的方法,在这两篇文章里面开始为大家介绍一下WCF与 Workflow工作流之间的关系。微软在WCF和WF之间扩展了强大的功能,使用两者能够协调工作,在.NET Framework3.5中既可以把Workflow发布为WCF实现工作流服务,也可以在WCF中调用Workflow工作流,增强其使用的灵活性。
下面先为大家介绍如何把Workflow工作流发布为WCF服务,这里还是以最简单的Hello World为例子。
首先设定一个服务契约接口
{
[ServiceContract(SessionMode = SessionMode.NotAllowed)] // 这里先使用最简单的无状态服务为例子
public interface IWorkflowService
{
[OperationContract]
string Hello( string name);
}
}
现在新建一个Workflow,加入ReceiveActivity活动receiveActivity1,ReceiveActivity实现了 IEventActivity,可以作为EventDrivenActivity的第一个子活动,然后在receiveActivity1中加入 codeActivity1来执行操作。
现在先把receiveActivity1属性ServiceOperationInfo绑 定为刚设定的服务接口Microsoft.ISercive.IWorkflowService,在后台添加两个变量_returnValue和name 分别用来绑定返回值和输入参数。
然后把receiveActivity1的属性CanCreateInstance设置为True,这代表着每次被客户端调用的时候都创建一个新的对象实例。
现在应该为codeActivity1添加ExecuteCode事件的方法codeActivity1_ExecuteCode,下面为这个Workflow的完整代码。
{
public sealed partial class Workflow4 : SequentialWorkflowActivity
{
public string _name;
public string returnValue;
public Workflow4()
{
InitializeComponent();
}
private void codeActivity1_ExecuteCode( object sender, EventArgs e)
{
returnValue = " Hello " + _name;
}
}
}
现在可以为这个服务建立一个*.svc文件了,注意Service用于绑定此Workflow类文件的全名称
<%@ ServiceHost Language="C#" Debug="true" Service="Microsoft.Workflow.Workflow4 " Factory="System.ServiceModel.Activation.WorkflowServiceHostFactory" %>
然后实现*.config配置
< system.serviceModel >
< behaviors >
< serviceBehaviors >
< behavior name = " ServiceBehavior " >
< serviceMetadata httpGetEnabled = " true " />
< serviceDebug includeExceptionDetailInFaults = " false " />
behavior >
serviceBehaviors >
behaviors >
< services >
< service behaviorConfiguration = " ServiceBehavior " name = " Microsoft.Workflow.Workflow4 " >
< endpoint address = "" binding = " wsHttpBinding " contract = " Microsoft.IService.IWorkflowService " > // 注意service Name是此Workflow工作流的全名称,而contract是该服务契约的全名称
< identity >
< dns value = " localhost " />
identity >
endpoint >
< endpoint address = " mex " binding = " mexHttpBinding " contract = " IMetadataExchange " />
service >
services >
system.serviceModel >
configuration >
现在服务器端的开发已经完成,可以开发一个客户端进行测试。我们在客户端引用此Web服务,然后配置好.config文件。
< system.serviceModel >
< bindings >
< wsHttpBinding >
// 添加服务绑定配置,设置其接收时间,事务,最大接收量等等属性
< binding name = " WSHttpBinding_IWorkflowService " closeTimeout = " 00:01:00 "
openTimeout = " 00:01:00 " receiveTimeout = " 00:10:00 " sendTimeout = " 00:01:00 "
bypassProxyOnLocal = " false " transactionFlow = " false " hostNameComparisonMode = " StrongWildcard "
maxBufferPoolSize = " 524288 " maxReceivedMessageSize = " 65536 " messageEncoding = " Text "
textEncoding = " utf-8 " useDefaultWebProxy = " true " allowCookies = " false " >
< readerQuotas maxDepth = " 32 " maxStringContentLength = " 8192 " maxArrayLength = " 16384 "
maxBytesPerRead = " 4096 " maxNameTableCharCount = " 16384 " />
< reliableSession ordered = " true " inactivityTimeout = " 00:10:00 "
enabled = " false " />
< security mode = " Message " >
< transport clientCredentialType = " Windows " proxyCredentialType = " None "
realm = "" />
< message clientCredentialType = " Windows " negotiateServiceCredential = " true "
algorithmSuite = " Default " />
security >
binding >
wsHttpBinding >
bindings >
< client >
// 绑定其地址,服务契约等等属性
< endpoint address = " http://leslie-pc:5600/Service.svc " binding = " wsHttpBinding "
bindingConfiguration = " WSHttpBinding_IWorkflowService " contract = " ServiceReference1.IWorkflowService "
name = " WSHttpBinding_IWorkflowService " >
< identity >
< dns value = " localhost " />
identity >
endpoint >
client >
system.serviceModel >
configuration >
测试
{
try
{
WorkflowService.WorkflowServiceClient workflowService1 = new WorkflowService.WorkflowServiceClient();
workflowService1.Hello( " Leslie " );
}
catch (Exception ex)
{...}
Console.ReadKey();
}
结果显示:
Hello Leslie
经过简单的测试,证明此工作流服务已经能正常运行。使用此方式,可以轻松地Workflow发布为WCF服务而不需要编写任何干预探测代码,但值得注意的是WCF支持请求/响应,单向,双向多个工作模式,但Workflow工作流则只支持请求/响应这种模式。
微软一开始就注意把WCF与WF结合开发实现简单的相互调用,在下一篇文章将为你介绍一下如何使用SendActivity在Workflow工作流里面直接调用WCF服务。
WF工作流与Web服务的相互调用 —— 通过Web服务调用Workflow工作流(基础实例)
WF工作流与Web服务的相互调用 —— 通过Web服务调用Workflow工作流(开发持久化工作流)
WF工作流与Web服务的相互调用 —— 以InvokeWebServiceActivity在Workflow工作流调用Web服务
WF工作流与Web服务的相互调用 —— WF与WCF互相调用(利用ReceiveActivity把WF发布为WCF)