Download example:
Http://files.cnblogs.com/foundation/wcfwfSample.rar
Example:
WCF Workflow Service
Create a WF-enabled WCF Service
Example 1: a service used to calculate Addition
Add a WCF Workflow Service
After adding,
1. VS2010 automatically generates a template with the extension xamlx,
2. Add two activities to the template: [Receive] and [Send]. [Receive] is used to Receive client requests and [Send] is used to Send messages to the client.
3. Set [OperationName] of [Receive] to [GetData]. This is the method name of the WCF Service.
4. Add an int variable [data] and bind [data] to [Receive. Content] as a parameter of the WCF Service method name.
5. Bind [data] to [Send. Content] As the return value of the WCF Service method.
Design Process
Scenario:
Design a service as follows
Double addxy (double myX, double myY)
{
Double myXY = yX + myY;
Return myXY;
}
1. Delete the data variable automatically added by vs2001
2. Add double Variables [x], [y], [xy]
3. Set [OperationName] of [Receive] to [addxy], and set [CanCreateInstance] attribute to [true]
4. Bind [x] and [y] to [Receive. Content] as parameters of the WCF Service method name.
5. Bind [xy] to [Send. Content] As the return value of the WCF Service method.
6. Add [Assign] for computing
Client call
1. Add a WPF Program
2. reference the [addService] service added above
3. Complete the following design for the WPF Program
<Grid>
<TextBox Height = "23" HorizontalAlignment = "Left" Margin = "44,54, 0, 0" Name = "x" VerticalAlignment = "Top" Width = "73 "/
<TextBox Height = "23" HorizontalAlignment = "Right" Margin = "," Name = "y" VerticalAlignment = "Top" Width = "77" Text = ""/>
<TextBox Height = "23" HorizontalAlignment = "Left" Margin = "340,0, 0,234" Name = "xy" VerticalAlignment = "Bottom" Width = "83" Text = ""/>
<TextBlock Height = "23" HorizontalAlignment = "Left" Margin = "135,57," Name = "textBlock1" Text = "+" verticalignment = "Top"/>
<Button Content = "=" Height = "23" HorizontalAlignment = "Left" Margin = "266,54, 0, 0 "Name =" add "verticalignment =" Top "Width =" 51 "Click =" add_Click "/>
</Grid>
|
Private Void add_Click (object sender, RoutedEventArgs e)
{
MyService. ServiceClient server = new myService. ServiceClient ();
Double myX = double. Parse (x. Text );
Double myY = double. Parse (y. Text );
Double myXY = server. addxy (myX, myY );
Xy. Text = myXY. ToString ();
}
|
4. Call results
Example 2: custom object transfer
Scenario:
The addition service, but the design method is as follows:
Class myData
{
Double x, y, xy;
}
MyData addxy (myData mydata)
{
Mydata. xy = mydata. x + mydata. y;
Return mydata;
}
Add a WCF Workflow Service
Add a [WCF Workflow Service] named addObjService
Add Custom Data class
Namespace wcfwfSample
{
[DataContract ()]
Public Class MyData
{
[DataMember ()]
Public Double x
{Set; get ;}
[DataMember ()]
Public Double y
{Set; get ;}
[DataMember ()]
Public Double xy
{Set; get ;}
}
}
|
Design Process
Client call
1. Add a WPF Program
[WpfApplication2]
2. reference the [addObjService] service added above
3. Complete the following design for the WPF Program
<Grid Name = "dataRoot" Loaded = "dataRoot_Loaded">
<TextBox Height = "23" HorizontalAlignment = "Left" Margin = "44,54, 0, 0" VerticalAlignment = "Top" Width = "73" Text = "{Binding Path = x, Mode = TwoWay}"/>
<TextBox Height = "23" HorizontalAlignment = "Right" Margin =" VerticalAlignment = "Top" Width = "77" Text = "{Binding Path = y, Mode = TwoWay}"/>
<TextBox Height = "23" HorizontalAlignment = "Left" Margin = "0,234, 0" VerticalAlignment = "Bottom" Width = "83" Text = "{Binding Path = xy, Mode = TwoWay}"/>
<TextBlock Height = "23" HorizontalAlignment = "Left" Margin = "135,57," Name = "textBlock1" Text = "+" verticalignment = "Top"/>
<Button Content = "=" Height = "23" HorizontalAlignment = "Left" Margin = "266,54, 0, 0 "Name =" add "verticalignment =" Top "Width =" 51 "Click =" add_Click "/>
</Grid>
|
Private Void add_Click (object sender, RoutedEventArgs e)
{
MyData data = dataRoot. DataContext MyData;
ServiceClient server = new ServiceClient ();
Server. addxy (ref data );
This. dataRoot. DataContext = data;
}
Private Void dataRoot_Loaded (object sender, RoutedEventArgs e)
{
DataRoot. DataContext = new MyData ();
}
|
4. Call results