WF Foundation + Persistence Instance Summary

Source: Internet
Author: User

From object-oriented to service-oriented, the concept of access to more and more, the field of vision is gradually expanding, the concept of object-oriented seems to be a lot of different changes in the day by day. Many have not thought of, now everyone is beginning to discuss their specific implementation, it seems really learning ah!

first, the concept of SOA

The concept of exposure to SOA has been in fact for some time, service oriented, for the programming of components, modules or functional units, to provide them with interfaces and services. is a complement to object-oriented thinking. The WCF we use is also based on this idea.

and Workflow (Workflow), meaning" business process computer automation in application environments. The main problem for the workflow is: in order to achieve A business goal that uses a computer to automatically pass documents, information, or tasks between multiple participants according to a predetermined rule.

In fact, the attraction here is the word ' automation ', from which we can see that the application workflow allows us to develop the software to automatically implement its own function according to the requirements, to the maximum extent possible from the manual operation or code behind the writing.

ii. WF Basics1, three types: sequence, flowchart and state machine.

The state machine workflow is one of the most common and powerful functions in development.

2. Two ways to start a workflow:

1. Initiate the corresponding workflow in the main thread of the boot by using the Invoke method

Activity activity = newdocactivity ();

Workflowinvoker.invoke (activity);

2, Microsoft provides a kind of application operation

3. Bookmarks

In a workflow, bookmarks enable a workflow to stop and receive parameters or specific actions.

4. WF Persistence

How to achieve better automation, in the user approval or other appropriate actions in the middle, can not limit time, or more convenient, which is used in WF persistence.

WF persistence Sql:  .net Framework 4.5 Setup will be associated with SQL The workflow instance store feature is associated with the SQL script file copied to the Span lang= "en-us" style= "background: #CCFFFF" >%windir%\microsoft.net\framework\v4.xxx\sql\en folder.  sql workflow instance Store SQL for persisting workflow instances Server 2005 or sql Server page database, Run these script files.   First run Sqlworkflowinstancestoreschema.sql Sqlworkflowinstancestorelogic.sql file.

The database of WF persisted SQL can be established by the above operation. The use of persistent technology, user approval work, you can at any time according to their own arrangements to carry out. Persisting the database will fully reproduce the current state of the system in front of the user.


Third, WF instance application

Examples of the most common application workflows: Leave approval. When an applicant submits an application, the workflow automatically submits the application to the designated approver for specific approval actions, and then decides whether to continue the flow. The following code will cover the basics of the WF mentioned above

Overall process design such as:

Starting from start into the workflow start to State 1, the entire small demo declared a variable of num, T1: Condition 1 if num is greater than 5, then directly into the end state, T2: Condition 2 If num is less than 5, then return status 1, re-approval.

It uses a sequential stream, inputname as a parameter. Wait4inputactivity for the creation of a code activity, specifically implemented as follows;

 public sealed class Wait4inputactivity:nativeactivity//nativeactivity allow creation of bookmarks {//define a string type of activity input parameter pub        Lic inargument<string> BookmarkName {get; set;}        Public outargument<int> Num {get; set;}            protected override bool Caninduceidle {get {return true;        }}//If the activity returns a value, the value is derived from codeactivity<tresult>//and returned from the Execute method. protected override void Execute (Nativeactivitycontext context) {//Gets the run-time value of the Text input parameter//strin G Text = context. GetValue (this.            Text); String name = Context.           GetValue (BookmarkName); When you create a bookmark, the workflow is idle, and the work context is persisted.        CreateBookMark (name,new bookmarkcallback (Continueexcuteworkflow));  }//object:value value, which is the argument private void Continueexcuteworkflow (Nativeactivitycontext context, when we let the workflow continue to execute down) Bookmark Bookmark, Object value) {//When we call the workflow a way to let the workflow relayExecute this method first.        SetValue (Num, (int) value); }    }

Then start the workflow and then execute, before this, to pull the code out of the public part of the workflow involved as a static class as follows:

public static  class Workflowapplicationhelper    {//through the configuration file link WF Persistent database public       static string workflowsqlconn;       Static Workflowapplicationhelper ()       {           workflowsqlconn = configurationmanager.appsettings[" Sqlworkflowinstancestore "];       }       Create a workflow instance, and execute the public       static workflowapplication Createhttpapplicationandrun (Activity activity,idictionary <string,object> inputs) {
Use Application to control workflow workflowapplication application = new Workflowapplication (activity,inputs);           Create a SQL serialization object required for workflow serialization Sqlworkflowinstancestore store = new Sqlworkflowinstancestore (workflowsqlconn); An instance application that causes the instance association period of the current application object to be created.           Instancestore = store; Application.               OnUnhandledException + = m = {Console.WriteLine ("Unhandled exception occurred in Workflow");           return unhandledexceptionaction.terminate;           }; Application. Unloaded + = m = {Console.WriteLine ("Workflow Uninstall!           ");           }; Application. Persistableidle + = a = {Console.WriteLine ("The Workflow is serialized!               ");           return persistableidleaction.unload;           }; Application. aborted + = EA = {Console.WriteLine ("The workflow has collapsed!           ");           }; Application.           Idle + = m = {Console.WriteLine ("The Workflow is Idle");           };           Application.           Run ();       return application; } public static Workflowapplication loadworkflowapplication (Activity activity,guid id) {Workflowap  Plication application = new workflowapplication (activity); There is no need to pass arguments here//create the SQL serialization object required for workflow serialization Sqlworkflowinstancestore store = new Sqlworkflowinstancestore (WORKF           Lowsqlconn); An instance application that causes the instance association period of the current application object to be created.           Instancestore = store; Application.               OnUnhandledException + = m = {Console.WriteLine ("Unhandled exception occurred in Workflow");           return unhandledexceptionaction.terminate;           }; Application. Unloaded + = m = {Console.WriteLine ("Workflow Uninstall!           ");           }; Application. Persistableidle + = a = {Console.WriteLine ("The Workflow is serialized!               ");           return persistableidleaction.unload;           }; Application.        aborted + = EA =   {Console.WriteLine ("Workflow Forced Quit!"}           ");           }; Application.           Idle + = m = {Console.WriteLine ("The Workflow is Idle");                      }; Loads the application state application from the database.           Load (ID);       return application; }}
So when you start a workflow, you only need to:

var inputs= new dictionary<string, object> () {                  {"InputName", DateTime. Now. ToString ()},                  {"Tempbookmarkname", This.txtbookmark. Text}};                               Workflowapplication  application =workflowapplicationhelper. Createhttpapplicationandrun (New docactivity (), inputs);           txtID. Text=application. Id. ToString ();//Record Application ID
At this point, when helper is invoked, the current state of the workflow instance is stored in the persisted database. If we close the program, when it is opened, when you click Restore Bookmark, the saved ID is assigned to the Application object, and the original saved instance is executed.

To restore a bookmark:

<span style= "White-space:pre" ></span>workflowapplication application= Workflowapplicationhelper.loadworkflowapplication (New Docactivity (), Guid.parse (txtID. Text));            Application. Resumebookmark (this.txtBookMark.Text, Int. Parse (Txtnum.text));
After the above steps, the workflow persistence is complete. Based on this principle, we can take advantage of workflow persistence to accomplish a lot of practical work such as approving the time span. Is indeed a very powerful kind of thought.

So the above is just a small example of WF, how to apply WF to the development, but also need constant research, such as how WF and generics to the greatest degree of abstraction, WF internal many ways to use the delegate to achieve, Then how to use the business with the delegation to a greater extent to play its powerful service-oriented force, or it is worth our further discussion.



WF Foundation + Persistence Instance Summary

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.