A simple test of WF messaging Activity

Source: Internet
Author: User

Firstly create a WF service which act like normal WCF Service to be our service for testing.

1. Create a WF console project.

2. Add a class which is responsible for building a workflow. Here is the code.

Class Receiveandreplyworkflow
{
Public Workflowservice getinstance ()
{
Workflowservice service;
Variable < Int > X = New Variable < Int > {Name = " X " };
Variable < Int > Y = New Variable < Int > {Name = " Y " };
Variable < Int > Addresult =
New Variable < Int > {Name = " Addresult " };
Receive receive = New Receive
{
Servicecontractname = " Icalculateservice " , // WCF contract name
Operationname = " Getdata " , // Contract Methods
Cancreateinstance = True , // Required. Create a WF instance after receiving the message.
Content = New Receiveparameterscontent
{
Parameters = {
{ " Xin " , New Outargument < Int > (X )}, // Receives the values of the "Xin" and "Yin" keywords in the message and assigns them to the X and Y variables.
{ " Yin " , New Outargument < Int > (Y )}
}
}
};
Sequence workflow = New Sequence ()
{
Variables = {X, Y, addresult },
Activities = {
New Writeline {text = " WF service is starting... " },
Receive,
New Writeline {text = " Receive request with two numbers " },
New Writeline {
TEXT = New Inargument < String > (AEC => " X = " + X. Get (AEC). tostring () + " Y = " + Y. Get (AEC ))
},
New Assign < Int > {
Value = New Inargument < Int > (AEC => X. Get (AEC) + Y. Get (AEC )),
To = New Outargument < Int > (Addresult)
},
New Writeline {
TEXT = New Inargument < String > (AEC => " Addresult = " + Addresult. Get (AEC). tostring ())
},
New Writeline {text = " Then send the result back to client " },
New Sendreply {
Request = receive,
Content = New Sendparameterscontent {
Parameters =
{
{ " Addresult " , New Inargument < Int > (Addresult )} // Assign the value of the addresult variable to the "addresult" element in the returned message"
// I personally think it is inargument or outargument. It depends on whether it is input or output. If it is output to this parameter. Then it is out, and vice versa is in.
},
},
},
New Writeline {text = " Sent result back done " }
},
};
Service = New Workflowservice
{
Name = " Addservice " ,
Body = Workflow
};
Return Service;
}

}

3. In the main method code.

Class Program
{
Static Void Main ( String [] ARGs)
{
Receiveandreplyworkflow rrw = New Receiveandreplyworkflow ();
Workflowservice wfservice = rrw. getinstance ();
Uri address =
New Uri ( " HTTP: /localhost: 8000/wfservices " );
Workflowservicehost host =
New Workflowservicehost (wfservice, address );

Try
{
Console. writeline ( " Opening service... " );
Host. open ();
Console. writeline
( " WF service is listening on " + Address. tostring ());
Console. Readline ();
}
Catch (Exception E)
{
Console. writeline
( " Some thing bad happened " + E. stacktrace );
}
Finally
{
Host. Close ();
}
}

}

4. And the configuration in APP. config.

<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< System. servicemodel >
< Behaviors >
< Servicebehaviors >
< Behavior Name = "" >
< Servicemetadata Httpgetenabled = "True" />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >
</ System. servicemodel >

</Configuration> 

5. Until now. The server endpoint is ready. CTRL + F5 make this service work.

 

6. You can use the WCF test client tool to verify the service working.

 

As we can see. This WCF has two input parameter. One return result. We can check it out in the "XML" tab.

 

Above figure show the send and response message which contact with the service we build.

Next. We will build a client which in charge of calling the service just like the work that WCF test client done.

1. We create a WF console project being the client.

2. Add a class which is responsible for building a workflow. Here is the code.

Class Sendandreceiveworkflow
{
Public Activity getinstance ()
{
Variable < Int > X = New Variable < Int > ( " X " , 20 );
Variable < Int > Y = New Variable < Int > ( " Y " , 10 );

Variable < Int > Addresult = New Variable < Int > (" Addresult " , 0 );
VaR Endpoint = New System. servicemodel. endpoint
{
Addressuri = New Uri ( " HTTP: /localhost: 8000/wfservices " ), // It must be specified. The WCF endpoint to be requested
Binding = New Basichttpbinding (),
};
Send addrequest = New Send
{
Servicecontractname = " Icalculateservice " ,
Endpoint = endpoint,
Operationname = " Getdata " ,
Content =New Sendparameterscontent
{
Parameters = {
{ " Xin " , New Inargument < Int > (X )},
{ " Yin " , New Inargument < Int > (Y )}
}
},
};
VaR Workflow = New Correlationscope
{
Body = New Sequence
{
Variables = {X, Y, addresult },
Activities = {
New Writeline {text = " Send X: 20 and Y: 10 to WF Service " },
Addrequest,
New Receivereply {
Request = addrequest, // It must be specified. In sendandreceivereplay activity, it will automatically specify the request to be created separately. When sending activity and reveive activity, you must manually specify the request.
Content = New Receiveparameterscontent {
Parameters = {
{ " Addresult " , New Outargument < Int > (Addresult )} // The first parameter represents a keyword in the message. The second parameter indicates that the value in the previous keyword is assigned to the next variable and an output parameter is generated.
}
},
},
New Writeline {
TEXT = New Inargument < String > (AEC => ( " The result is: " + Addresult. Get (AEC). tostring ()))
},
New Delay
{
Duration = New Timespan ( 0 , 0 , 5 )
}
}
}
};
Return Workflow;
}

}

3. The client main method code just is simply like this.

Class Program
{
Static Void Main ( String [] ARGs)
{
Sendandreceiveworkflow SRW = New Sendandreceiveworkflow ();
Workflowinvoker. Invoke (SRW. getinstance ());
}

}

 

Well. This is a beginning Guild for building and consuming the WF service. In the real business. I believe it is much more complicated than this.

Any comment are welcome. I will beAppreciate FOr your attention. Thanks

You can down load the project from here. testsendreceive.rar

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.