zoukankan      html  css  js  c++  java
  • WF & WCF(1)

    前几篇文章已经为大家介绍过Web服务与Workflow工作流之间的相互调用的方法,在这两篇文章里面开始为大家介绍一下WCF与 Workflow工作流之间的关系。微软在WCF和WF之间扩展了强大的功能,使用两者能够协调工作,在.NET Framework3.5中既可以把Workflow发布为WCF实现工作流服务,也可以在WCF中调用Workflow工作流,增强其使用的灵活性。

    下面先为大家介绍如何把Workflow工作流发布为WCF服务,这里还是以最简单的Hello World为例子。

    首先设定一个服务契约接口

    View Code
    namespace Microsoft.IService
    {
    [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的完整代码。

    View Code

    现在可以为这个服务建立一个*.svc文件了,注意Service用于绑定此Workflow类文件的全名称

    <%@ ServiceHost Language="C#" Debug="true" Service="Microsoft.Workflow.Workflow4 " Factory="System.ServiceModel.Activation.WorkflowServiceHostFactory" %>

    然后实现*.config配置

     

    View Code
    <configuration>

    <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文件。

     

    View Code
    <configuration>
    <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>

    测试

     

    View Code
    static void Main(string[] args)
    {
    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服务。

  • 相关阅读:
    Lecture04_转换控制_GAMES101 课堂笔记——2020.2.21
    自动求梯度(pytorch版本)——2020.2.20
    深度学习之线性回归从零实现
    Lecture03_Transformation(变换)_GAMES101 课堂笔记——2020.2.18
    使用jupyter切换子环境,以及导致的`找不到指定模块`和`找不到指定的程序`问题
    多层感知机从0开始实现(Pytorch版本)——2020.2.16
    《动手学深度学习》(pytorch版本)中`d2lzh_pytorch`包问题
    Lecture02_向量与线性代数_GAMES101 课堂笔记——2020.2.14
    数据结构与算法(24)——优先队列和二叉堆
    剑指 Offer 06. 从尾到头打印链表
  • 原文地址:https://www.cnblogs.com/Mayvar/p/wanghonghua_201108040826.html
Copyright © 2011-2022 走看看