WF and WebService

Source: Internet
Author: User
Tags reflector web hosting

Interaction between WF and WebService

WF provides four activities: webserviceinputactivity, webserviceoutputactivity, invokewebserviceactivity, and webservicefaultactivity.
The following is a brief introduction to these four activities:

1. webserviceinputactivity: enables a workflow to receive data from a Web service. When starting a web service, publish the service as a Web service method, and then call the Web Service
Method.
2. webserviceoutputactivity: used to respond to Web Service requests made to the workflow. This activity must be associated with the webserviceinputactivity.
3. webservicefaultactivity: Creates a model for a Web service error.
4. invokewebserviceactivity: Call the Web Service through the proxy class and pass and receive the specified parameters.

Application Example

1. The following example shows that the method received by the webserviceinputactivity activity must be defined in the interface. Therefore, we must first create an interface iaddservice. The Code is as follows:

namespace CaryWFLib{    interface IAddService    {        double Add(double number1, double number2);    }}

2. workflow designer:

3. In the workflow designer, we first drag a webserviceinputactivity activity, set its interfacetype attribute, then methodname, and set isactivating to true,
The default value is false. After the methodname attribute is selected, the attribute window will automatically display more parameters. Complete the addition logic in the codeactivity activity, and finally webserviceoutputactivity
Activity. The workflow code is as follows:

public sealed partial class AddWorkflow: SequentialWorkflowActivity{     public double number1;     public double number2;     public double result;

     public AddWorkflow()     {        InitializeComponent();     }     private void codeActivity1_ExecuteCode(object sender, EventArgs e)     {         result = number1 + number2;     }        }
4. At this time, you can use "publish as Web Service" in the right-click menu of the project to publish the workflow as WebService. After the release is complete, a WebService will be added to the current solution.
Project, we can run the test, such:
 
Only the workflow library can be published as WebService. The newly generated WebService project will contain a carywflib. addworkflow_webservice.asmx file and a web. config
Configuration file. The content of the web. config file is as follows:
<? XML version = "1.0"?> <Configuration> <configsections> <section name = "workflowruntime" type = "system. workflow. runtime. configuration. workflowruntimesection, system. workflow. runtime, version = 3.0.255.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "/> </configsections> <workflowruntime name =" carywflib "> <commonparameters> <Add name =" connectionstring "value =" Initial catalog = workflowpersistence; data Source = localhost \ sqlexpress; Integrated Security = sspi; "/> </commonparameters> <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.20.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"
Unloadonidle = "true"/> </services> </workflowruntime> <etettings/> <connectionstrings/> <system. web> <compilation DEBUG = "false"/> <Authentication mode = "Windows"/>
5. It is normal for us to call WebService for the first time. If you call WebService again, the following error will occur:
System. invalidoperationexception: The workflow with the ID "21cf8af3-885d-47dd-acbc-530eeba794ed" cannot be found in the state persistence storage.
This is because workflowwebhostingmodule is used in Web. config. During the second call, WebService will load the previous workflow instance, and the actual
For example, it is destroyed. If you turn off the browser and open it again, this is because the new workflow instance is generated again.
 
WorkflowWebHostingModule

1. The workflowwebhostingmodule class is the default routing mechanism used to route web service requests to the corresponding workflow by Using ASP. NET cookies. The client that sends these requests must support
Cookie. The main purpose is to call the method of a workflow instance multiple times. We can design a new workflow to solve the above problem and extend the original interface. The Code is as follows:

namespace CaryWFLib{    interface IAddStatefulService    {        void StartWorkflow();        double Add(double number1, double number2);        void StopWorkflow();    }}

2. The workflow is designed as follows:

We use listenactivity to listen, webserviceinputactivity1 binds startworkflow method, webserviceinputactivity2 binds add method, webserviceinputactivity3
Bind the stopworkflow method. If the stopworkflow method is called, set the while condition to false in the event of webserviceinputactivity3 call completion. In web. config
The persistence service is loaded, so persistent storage is performed during workflow idle. When the second call is performed, the same workflow instance id is loaded from the persistent database according to the cookie.
Workflow instance.

3. If we need to manually load the persistent workflow instance in other programs, such as winform, how do we know the ID of the workflow instance? Let's reflector the workflowwebhostingmodule Code as follows:

Public sealed class workflowwebhostingmodule: ihttpmodule {// fields private httpapplication currentapplication; // Methods public workflowwebhostingmodule () {workflowtrace. host. traceevent (8, 0, "workflow web hosting module created");} private void onacquirerequeststate (Object sender, eventargs e) {workflowtrace. host. traceevent (8, 0, "webhost module routing begin"); httpcookie cookie = httpcont Ext. Current. Request. Cookies. Get ("wf_workflowinstanceid"); If (cookie! = NULL) {httpcontext. current. items. add ("_ workflowinstanceid _", new GUID (cookie. value) ;}} private void onreleaserequeststate (Object sender, eventargs e) {If (httpcontext. current. request. cookies. get ("wf_workflowinstanceid") = NULL) {httpcookie cookie = new httpcookie ("wf_workflowinstanceid"); object obj2 = httpcontext. current. items ["_ workflowinstanceid _"]; If (obj2! = NULL) {cookie. value = obj2.tostring (); httpcontext. current. response. cookies. add (cookie) ;}} void ihttpmodule. dispose () {} void ihttpmodule. init (httpapplication application) {workflowtrace. host. traceevent (8, 0, "workflow web hosting module initialized"); this. currentapplication = application; application. releaserequeststate + = new eventhandler (this. onreleaserequeststate); application. acquire Requeststate + = new eventhandler (this. onacquirerequeststate) ;}} 4. from the source code output by reflector, we can find that the ID of the workflow instance is stored in the cookie named wf_workflowinstanceid, and item is _ workflowinstanceid __.
We can get the ID of the workflow instance through the following code:
localhost.AddWorkflow_WebService ws = new localhost.AddWorkflow_WebService();ws.CookieContainer = new System.Net.CookieContainer();ws.Url = "http://localhost:6411/CaryWFLib_WebService1/CaryWFLib.AddWorkflow_WebService.asmx";ws.Add(1, 2);Uri uri = new Uri(ws.Url);CookieCollection cc = ws.CookieContainer.GetCookies(uri);foreach (Cookie c in cc){   if (c.Name == "WF_WorkflowInstanceId")   {       this.Label3.Text = c.Value;   }}
5. Because the workflowwebhostingmodule must require the client to enable cookies, we can write a new workflowwebhostingmodule when the client cannot Enable cookies.
6. The Web Service released above uses the functions provided by WF itself, which can be implemented by ourselves. The Code is as follows:
namespace CaryWFLib{    [WebService(Namespace = "http://CaryWFLibWebService.com/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    public class CaryWFLibWebService:System.Web.Services.WebService    {        private static WorkflowRuntime workflowRuntime = new WorkflowRuntime();        [WebMethod]        public double Add(double number1,double number2)        {            double result=0;            AutoResetEvent waitHandle = new AutoResetEvent(false);            workflowRuntime.WorkflowCompleted +=               delegate(object sender, WorkflowCompletedEventArgs e)               {                   result =Convert.ToDouble( e.OutputParameters["result"]);                   waitHandle.Set();               };            workflowRuntime.WorkflowTerminated +=               delegate(object sender, WorkflowTerminatedEventArgs e)               {                   waitHandle.Set();               };                       Dictionary<string, object> wfParas = new Dictionary<string, object>();            wfParas.Add("number1", number1);            wfParas.Add("number2", number2);            WorkflowInstance instance =               workflowRuntime.CreateWorkflow(typeof(ManualAddWorkflow),wfParas);            instance.Start();            waitHandle.WaitOne();            return result;        }        }}

Of course, at this time, the manualaddworkflow workflow design only requires a codeactivity to complete the addition logic, and webserviceinputactivity and other activities are not required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.