Replacing the event method with an external method in wf4

Source: Internet
Author: User

Recently, the company asked to upgrade the system developed by WF3 to wf4 to form an approval project. Later, she will be used to handle the approval module of other projects of the company. Some time ago, I had the honor to attend simple training on the new features of vs10. If you want to upgrade wf4 directly through tools, it is estimated that your dream will be shattered. WF3 and wf4 are two different products. We were unconvinced. After installing vs10 to open the project, we found that the previous class libraries were gone,CodeA large area is warned in red. Instead, I began to learn how to use wf4. I want to learn from the cool man WF and click here. After a copy of the learning, I finally want to get started. You should have a deep understanding of the difficulties you have used 3, and the 4 compiling method is the biggest change. I am always confused and want to write something in invoked. It's really embarrassing to learn something before I learned WF in a year. At this time, I will waste my power and absorb wf4 again. Let's talk about the title-related stuff :)

 

Wf4 does not support XAML backend CS files. That is to say, you cannot write business logic code in the process file. The design concept of wf4 is that the business and code are completely separated (if you want to write it, you can still drop it around the road, O (pipeline _ pipeline) O ~). What should we do before, during, and after approval? Send a message to Microsoft's buddy, and the reply confirms that the XAML file cannot be used together with the CS file. You can either write an Xmal file to separate three events into three custom activities and combine them for use, or write a workflow that is pure code. The problem solved by a user-defined activity is complicated into three activities, which affect performance and is not easy to understand (our customers should look at the flowchart ). It is better to use workflow with pure code than WF3. Neither of the two methods is acceptable. Fortunately, few people still know that C # has a reflection. To put it bluntly, directly submit the code.

 


///   <Summary>
/// Call method before external approval
///   </Summary>
///   <Param name = "context"> </param>
///   <Param name = "AGRs"> </param>
Public   Void Callexternalapprovingmethod (system. Activities. nativeactivitycontext context, approvingeventargs AGRs)
{
String Sapprovingmethod = Context. getvalue ( This . Approvingmethod );
Type approvingclass = Context. getvalue ( This . Approvingclass );
// The class name and method name are not empty.
If (Approvingclass ! =   Null &&! String . Isnullorempty (sapprovingmethod ))
{
System. reflection. methodinfo Method = Approvingclass. getmethod (sapprovingmethod );
If (Method ! =   Null )
{
// Method Parameters
Object [] Oparams =   New   Object [] { This , AGRs };
// Execution Method
Method. Invoke ( Null , Oparams );
}
}
}

///   <Summary>
/// Call methods in external approval
///   </Summary>
///   <Param name = "context"> </param>
///   <Param name = "AGRs"> </param>
Public   Void Callexternalapprovemethod (system. Activities. nativeactivitycontext context, approveeventargs AGRs)
{
String Sapprovemethod = Context. getvalue ( This . Approvemethod );
Type approveclass = Context. getvalue ( This . Approveclass );
// The class name and method name are not empty.
If (Approveclass ! =   Null   &&   ! String . Isnullorempty (sapprovemethod ))
{
System. reflection. methodinfo Method = Approveclass. getmethod (sapprovemethod );
If (Method ! =   Null )
{
// Method Parameters
Object [] Oparams =   New   Object [] { This , AGRs };
// Execution Method
Method. Invoke ( Null , Oparams );
}
}
}

///   <Summary>
/// Call method after external approval
///   </Summary>
///   <Param name = "context"> </param>
///   <Param name = "AGRs"> </param>
Public   Void Callexternalapprovedmethod (system. Activities. nativeactivitycontext context, approvedeventargs AGRs)
{
String Sapprovedmethod = Context. getvalue ( This . Approvedmethod );
Type approvedclass = Context. getvalue ( This . Approvedclass );
// The class name and method name are not empty.
If (Approvedclass ! =   Null   &&   ! String . Isnullorempty (sapprovedmethod ))
{
System. reflection. methodinfo Method = Approvedclass. getmethod (sapprovedmethod );
If (Method ! =   Null )
{
// Method Parameters
Object [] Oparams =   New   Object [] { This , AGRs };
// Execution Method
Method. Invoke ( Null , Oparams );
}
}
}

On the process designer, you only need to set the called class name and method name.

 

 

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.