程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WF4.0基礎篇 (二十七) WCF Workflow Service 在WCF中使用WF

WF4.0基礎篇 (二十七) WCF Workflow Service 在WCF中使用WF

編輯:關於.NET

例子說明:

WCF Workflow Service 創建啟用WF的WCF服務

例1:一個用於計算加法的服務 添加WCF Workflow Service

當添加完成後,

1.VS2010會自動生成一個模板,擴展名為xamlx,

2.在模板中添加兩個Activity,[Receive]與[Send],其中[Receive]是用於接收客戶端請求的,[Send]是用於向客戶端發送消息的.

3.將[Receive]的[OperationName]設為[GetData],這就是WCF服務的方法名

4.添加一個int型變量[data],將[data]綁定到[Receive.Content]上,做為WCF服務方法名的參數

5.將[data]綁定到[Send.Content]上,做為WCF服務方法的返回值

設計流程

場景:

設計一個如下方式的服務

double addxy(double myX,double myY)
{
double myXY = yX + myY;
return myXY;
}

1.刪除vs2001自動添加的data變量

2.添加double型變量[x],[y],[xy]

3.將[Receive]的[OperationName]設為[addxy],將[CanCreateInstance] 屬性設為[true]

4.將[x],[y]綁定到[Receive.Content]上,做為WCF服務方法名的參數

5.將[xy]綁定到[Send.Content]上,做為WCF服務方法的返回值

6.添加[Assign]用於完成計算

客戶端調用

1.添加一WPF程序

2.引用上面添加的[addService]服務

3.完成WPF程序的如下設計

<Grid>
  <TextBox Height="23" HorizontalAlignment="Left" Margin="44,54,0,0" Name="x" VerticalAlignment="Top" Width="73"  />
  <TextBox Height="23" HorizontalAlignment="Right" Margin="0,54,258,0" 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,0,0" Name="textBlock1" Text="+"  VerticalAlignment="Top" />
<Button Content="=" Height="23" HorizontalAlignment="Left" Margin="266,54,0,0" Name="add"  VerticalAlignment="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.調用結果

例2:自定義對象的傳遞

場景:

還是加法服務,但設計方式如下

class myData
{
double x,y,xy;
}

myData addxy(myData mydata)
{
mydata.xy=mydata.x + mydata.y;
return mydata;
}

添加WCF Workflow Service

添加一個名為addObjService的[WCF Workflow Service]

添加自定義數據類

namespace wcfwfSample
{
[DataContract()]
public class myData
{

[DataMember()]
public double x

{ set; get; }

[DataMember()]
public double y

{ set; get; }

[DataMember()]
public double xy

{ set; get; }

}

}

設計流程

客戶端調用

1.添加一WPF程序

[WpfApplication2]

2.引用上面添加的[addObjService]服務

3.完成WPF程序的如下設計

<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="0,54,258,0" VerticalAlignment="Top" Width="77"  Text="{Binding Path=y,Mode=TwoWay}"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="340,0,0,234" VerticalAlignment="Bottom" Width="83"  Text="{Binding Path=xy,Mode=TwoWay}" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="135,57,0,0" Name="textBlock1" Text="+"  VerticalAlignment="Top" />
<Button Content="=" Height="23" HorizontalAlignment="Left" Margin="266,54,0,0" Name="add"  VerticalAlignment="Top" Width="51" Click="add_Click" />

</Grid>

private void add_Click(object sender, RoutedEventArgs e)
{
myData data= dataRoot.DataContext as 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.調用結果

示例代碼:http://www.bianceng.net/dotnet/201208/244.htm

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved