WCF入门(五)---创建WCF服务
使用Microsoft Visual Studio2012創(chuàng)建WCF服務(wù),理解如下所有必要的編碼,更好地創(chuàng)建WCF服務(wù)的概念,這里做一個簡單的任務(wù)。
-
啟動Visual Studio 2012。
-
單擊新建項(xiàng)目,然后在Visual C#標(biāo)簽,選擇WCF選項(xiàng)。
WCF服務(wù)創(chuàng)建,執(zhí)行如加法,減法,乘法和除法基本的算術(shù)運(yùn)算。主要的代碼是在兩個不同的文件 - 一個接口和一個類。
一個WCF中包含一個或多個接口和實(shí)現(xiàn)類。
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceLibrary1 { //NOTE: You can use the "Rename" command on the "Refactor" menu to change the //interface name "IService1" in both code and config file together. [ServiceContract] Public interface IService1 { [OperationContract] int sum(int num1, int num2); [OperationContract] int Subtract(int num1, int num2); [OperationContract] int Multiply(int num1, int num2); [OperationContract] int Divide(int num1, int num2); } //Use a data contract as illustrated in the sample below to add composite types //to service operations. [DataContract] Public class CompositeType { Bool boolValue = true; String stringValue = "Hello "; [DataMember] Public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] Public string StringValue { get { return stringValue; } set { stringValue = value; } } } }而其后面是類的代碼,
using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Runtime.Serialization; usingSystem.ServiceModel; usingSystem.Text; namespace WcfServiceLibrary1 { //NOTE: You can use the "Rename" command on the "Refactor" menu to change the //class name "Service1" in both code and config file together. publicclassService1 :IService1 { /// This Function Return summation of two integer numbers publicint sum(int num1, int num2) { return num1 + num2; } ///This function returns subtraction of two numbers. ///If num1 is smaller than number two then this function returns 0. publicint Subtract(int num1, int num2) { if (num1 > num2) { return num1 - num2; } else { return 0; } } ///This function returns multiplication of two integer numbers. publicint Multiply(int num1, int num2) { return num1 * num2; } ///This function returns integer value of two integer number. ///If num2 is 0 then this function returns 1. publicintDivide(int num1, int num2) { if (num2 != 0) { return (num1 / num2); } else { return 1; } } } }要運(yùn)行此服務(wù),請?jiān)赩isual Studio中點(diǎn)擊開始按鈕。
當(dāng)我們運(yùn)行這個服務(wù),下面的屏幕會出現(xiàn)。
上點(diǎn)擊sum方法,在下面的頁面將被打開。在這里,可以輸入任何兩個整數(shù),然后單擊Invoke按鈕。該服務(wù)將返回這兩個數(shù)字的總和。
?
像求和,我們可以執(zhí)行哪個都列在菜單中的所有算術(shù)運(yùn)算。這里是捕捉他們。
當(dāng)點(diǎn)擊下頁將出現(xiàn)在Sutbtarct方法。輸入整數(shù),點(diǎn)擊調(diào)用按鈕,得到的輸出如下所示。
下頁將出現(xiàn)在Multiply方法單擊時。輸入整數(shù),點(diǎn)擊調(diào)用按鈕,得到的輸出如下所示。
下面的頁面上會出現(xiàn)當(dāng)點(diǎn)擊Divide方法時。輸入整數(shù),點(diǎn)擊調(diào)用按鈕,得到的輸出如下所示。
一旦服務(wù)被調(diào)用,可以在它們之間,直接從這里切換。
原文地址:http://www.yiibai.com/wcf/wcf_creating_services.html轉(zhuǎn)載于:https://www.cnblogs.com/CSharpLover/p/5687263.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的WCF入门(五)---创建WCF服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据仓库参考
- 下一篇: App性能测试以及测试方法技巧