WPFWCFWF打造HelloWorld程序
英文版:http://www.codeproject.com/KB/WF/wcf-wpf-wf-hello-world.aspx
分析篇:
Hello World程序作為所有編程語(yǔ)言的起始階段,Hello World占據(jù)著無(wú)法改變的地位。本例子很簡(jiǎn)單,用了WPF、WCF、WF三種還算比較新的技術(shù)。這個(gè)例子有三個(gè)項(xiàng)目:
1、WPFProject:WPF應(yīng)用程序
2、WCFProject:WCF服務(wù)
3、WFProject:WF流程定義
執(zhí)行順序是WPFProject程序去啟動(dòng)WFProject項(xiàng)目的工作流,WFProject項(xiàng)目去調(diào)用WCFProject的WCF服務(wù)。接著WCFProject服務(wù)將'Hello World'這行編程界最經(jīng)典的話(huà)返回給WFProject,WFProject由將將'Hello World'返回給WPFProject的UI。執(zhí)行順序如下圖:
實(shí)現(xiàn)篇:
新建一個(gè)WpfApplication、一個(gè)ConsoleApplication、一個(gè)workflow的ActivityLibrary,共三個(gè)項(xiàng)目,命名分別為:WPFProject、WCFProject、WFProject,解決方案如下圖:
我們就在這些由VS模板生成的代碼實(shí)現(xiàn)這個(gè)Hello world程序,我們盡量地利用這些生成好的代碼。
最不熟悉WCF:
引用System.ServiceModel.dll
添加接口IService1:
1 [ServiceContract]
2 public interface IService1
3 {
4 [OperationContract]
5 string GetData();
6 }
添加類(lèi)Service1:
1 public class Service1 : IService1
2 {
3 public string GetData()
4 {
5 return string.Format("Hello World");
6 }
7 }
App.config配置:
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <services>
5 <service name="WCFProject.Service1" behaviorConfiguration="metadata">
6 <host>
7 <baseAddresses>
8 <add baseAddress="http://localhost:8001/Service1"/>
9 </baseAddresses>
10 </host>
11 <!--The BasicHttpBinding is used because the Workflow uses a generated ASP.Net proxy to communicate with this service-->
12 <endpoint binding="basicHttpBinding" contract="WCFProject.IService1"/>
13 </service>
14 </services>
15 <behaviors>
16 <serviceBehaviors>
17 <behavior name="metadata">
18 <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
19 <serviceMetadata httpGetEnabled="true"/>
20 <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
21 <serviceDebug includeExceptionDetailInFaults="false"/>
22 </behavior>
23 </serviceBehaviors>
24 </behaviors>
25 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
26 </system.serviceModel>
27 </configuration>
在Program.cs添加啟動(dòng)WCF服務(wù)代碼:
1 using (ServiceHost host = new ServiceHost(typeof(Service1)))
2 {
3 host.Open();
4 Console.WriteLine("The service is ready.");
5 Console.WriteLine("Press <ENTER> to terminate service.");
6
7 Console.ReadLine();
8 }
以上實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的WCF服務(wù),這樣WCFProject就完成了。
最熟悉的WF:
在Activity1上托一個(gè)Sequence,再在Sequence托一個(gè)Send活動(dòng)調(diào)用上面定義好的WCF服務(wù)。
添加一個(gè)存儲(chǔ)返回值的returnValue輸出參數(shù)。
Send活動(dòng)需要知道WCF服務(wù)的ABC。故做如下設(shè)置。
1、OperateName:GetData(與上面WCF方法名字一致)
2、EndPoint設(shè)置為EndPoint
3、Bingding設(shè)置為basichttpBingding。
4、EndPointAddress設(shè)置為New Uri("http://localhost:8001/Service1"),與在WCFProject的app.config的地址相同
5、ServiceContractName設(shè)置為WCF服務(wù)的接口名字IService1。
右擊Send活動(dòng),選擇Create ReceivReply,這樣就創(chuàng)建了一個(gè)ReceivReply,它需要設(shè)置一個(gè)屬性:點(diǎn)擊Content設(shè)置如下:
最后流程如下圖所示:
這樣我們完成了Workflow的項(xiàng)目。
最后的WPF
由于WPFProject與WFProject是直接引用的關(guān)系,故在WPFProject直接引用WFProject的dll,如下圖。
由于要啟動(dòng)WF,故還需添加工作流的System.Activities.dll引用。
在MainWindow窗體中拖一個(gè)按鈕,將文本改成:“Invoke Workflow”,在click事件中添加下面代碼:
1 IDictionary<string, object> results = WorkflowInvoker.Invoke(new Activity1());
2
3 MessageBox.Show(results["returnValue"].ToString());
這樣WPFProject 也完成了物流配貨網(wǎng)http://wlphw.com/ QQ在線(xiàn):471226865。
調(diào)試與結(jié)果:
啟動(dòng)WCPProject,如下圖:
啟動(dòng)WPFProject。點(diǎn)擊Invoke Workflow,結(jié)果如下圖:
總結(jié):
結(jié)合了WPF、WCF、WF實(shí)現(xiàn)了Hello world程序。
這篇文章用最簡(jiǎn)單Hello World程序教你使用了三種最新技術(shù)。是不是值得你推薦一下,謝謝。:)
代碼:/Files/zhuqil/wpfwcfwf.rar
作者:朱祁林
出處:http://zhuqil.cnblogs.com
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
總結(jié)
以上是生活随笔為你收集整理的WPFWCFWF打造HelloWorld程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 专注嵌入式学习
- 下一篇: asp.net ajax控件工具集 Au