本文實例講述了wcf實現的計算器功能。分享給大家供大家參考,具體如下:
對于wcf,我們有了前面的理論基礎,今天通過一個計算器的實例主要給大家講解怎么一步一步地創建一個完整的wcf應用。
一、創建整個解決方案
calculator.service:一個類庫項目,定義服務契約(service contract),應用system.servicemodel程序集;提供對wcf服務的實現。
calculator.host:一個windows窗體應用程序,實現對定義在calculator.service項目中的服務的寄宿,該項目需要引用calculator.service項目和system.servicemodel程序集。
calculator.client:一個windows窗體應用程序模擬服務的客戶端,該項目應用system.servicemodel程序集。
二、創建服務契約
一般,我們通過接口的形式定義服務契約。通過下面的代碼,將一個接口icalculator定義成服務契約。我們通過在接口上應用system.servicemodel.servicecontractattribute特性將一個接口定義成服務契約。
將接口定義成服務契約后,接口的方法成員并不能自動成為服務的操作。我們需要在相應的操作方法上面顯式地應用operationcontractattribute特性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using system; using system.collections.generic; using system.linq; using system.text; using system.servicemodel; namespace calculator.service { [servicecontract] public interface icalculator { [operationcontract] double add( double x, double y); [operationcontract] double subtract( double x, double y); [operationcontract] double multiply( double x, double y); [operationcontract] double divide( double x, double y); } } |
三、創建服務
當服務契約創建成功后,我們需要通過實現服務契約來創建具體的wcf服務,wcf服務calculatorservice實現了服務契約的接口icalculator,實現了所有的服務操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
using system; using system.collections.generic; using system.linq; using system.text; namespace calculator.service { public class calculatorservice:icalculator { public double add( double x, double y) { return x + y; } public double subtract( double x, double y) { return x - y; } public double multiply( double x, double y) { return x * y; } public double divide( double x, double y) { return x / y; } } } |
四、通過自我寄宿的方式寄宿服務
服務寄宿的目的就是開啟一個進程,為wcf服務提供一個運行的環境。通過為服務添加一個或多個中級誒單,使之暴露給潛在的服務消費者。服務消費者最終通過相匹配的終結點對該服務進行調用。我們完全可以通過代碼的方式完成所有的服務寄宿工作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
using calculator.service; using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.servicemodel; using system.servicemodel.description; using system.text; using system.windows.forms; namespace calculator.host { public partial class form1 : form { public form1() { initializecomponent(); } servicehost host = null ; private void btnopen_click( object sender, eventargs e) { host = new servicehost( typeof (calculatorservice)); host.addserviceendpoint( typeof (icalculator), new wshttpbinding(), "http://localhost:8008/calculator" ); if (host.description.behaviors.find<servicemetadatabehavior>()== null ) { servicemetadatabehavior behavior = new servicemetadatabehavior(); behavior.httpgetenabled = true ; behavior.httpgeturl = new uri( "http://localhost:8008/calculator/metadata" ); host.description.behaviors.add(behavior); } host.opened += delegate { label1.text = "服務已經啟動!" ; }; host.open(); } private void btnclose_click( object sender, eventargs e) { if (host.state != communicationstate.closed) { host.closed += delegate { label1.text = "服務已經停止!" ; }; host.close(); } } } } |
五、創建客戶端調用服務
服務被成功寄宿后,服務端便開始了服務調用請求的監聽工作。此外,服務寄宿將服務描述通過元數據的形式發布出來,相應的客戶端就可以獲取這些元數據,創建愛你客戶端程序進行服務的消費。在vs下,當我們添加服務引用的時候,vs在內部幫我們實現元數據的獲取,并借組這些元數據通過代碼生成工具自動生成用于服務調用的服務代理相關代碼和相應的配置。
我們可以創建calculatorclient對象,執行相應方法調用服務操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace calculator.client { public partial class form1 : form { public form1() { initializecomponent(); } private void form1_load( object sender, eventargs e) { combobox1.text = "+" ; } private void button1_click( object sender, eventargs e) { calculatorservice.calculatorclient client = new calculatorservice.calculatorclient(); double x = convert.todouble (textbox1.text); double y = convert.todouble(textbox2.text); double result=0; string operater = combobox1.text; switch (operater ) { case "+" : result = client.add(x, y); break ; case "-" : result = client.subtract(x, y); break ; case "*" : result = client.multiply(x, y); break ; case "/" : if (y==0) { messagebox.show( "除數不能為0!" ); return ; } result = client.divide(x, y); break ; } label1.text = textbox1.text + combobox1.text + textbox2.text + " = " + convert.tostring(result); } } } |
在這個計算器實例中,我們實現了一個簡單的計算服務(calculatorservice),提供基本的加、減、乘、除的運算。客戶端和服務通過運行在不同進程模擬,體現了客戶端和服務端進程互相調用的關系。
希望本文所述對大家c#程序設計有所幫助。