日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用VS 2008开发WCF(一)——最快速的WCF入门

發布時間:2025/4/5 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用VS 2008开发WCF(一)——最快速的WCF入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一步,打開VS 2008,然后新建一個項目,項目使用WCF類型,具體選擇“WCF類庫”。

什么都不用改,直接設置新建好了的WCF類庫項目為啟動項目,Ctrl+F5開始運行。

什么?類庫不能直接運行?你且試試。

系統托盤會出現一個WCF服務主機的小圖標,點擊,查看這個WCF項目被分配為什么訪問路徑。

這樣我們就新建好了一個WCF服務,其中的代碼應該是默認的。

?

IService1.cs

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceLibrary { // 注意: 如果更改此處的接口名稱“IService1”,也必須更新 App.config 中對“IService1”的引用。 [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // 任務: 在此處添加服務操作 } // 使用下面示例中說明的數據協定將復合類型添加到服務操作 [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; } } } }

?

Service1.cs

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceLibrary { // 注意: 如果更改此處的類名“IService1”,也必須更新 App.config 中對“IService1”的引用。 public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } } }

?

App.config

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <!-- 部署服務庫項目時,必須將配置文件的內容添加到 主機的 app.config 文件中。System.Configuration 不支持庫的配置文件。--> <system.serviceModel> <services> <service name="WcfServiceLibrary.Service1" behaviorConfiguration="WcfServiceLibrary.Service1Behavior"> <host> <baseAddresses> <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary/Service1/" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否則地址將與上面提供的基址相關 --> <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary.IService1"> <!-- 部署時,應刪除或替換下列標識元素,以反映 在其下運行部署服務的標識。刪除之后,WCF 將 自動推導相應標識。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元數據交換終結點由服務用于向客戶端做自我描述。--> <!-- 此終結點不使用安全綁定,應在部署前確保其安全或將其刪除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WcfServiceLibrary.Service1Behavior"> <!-- 為避免泄漏元數據信息, 請在部署前將以下值設置為 false 并刪除上面的元數據終結點 --> <serviceMetadata httpGetEnabled="True"/> <!-- 要接收故障異常詳細信息以進行調試, 請將下值設置為 true。在部署前 設置為 false 以避免泄漏異常信息--> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>

?

下面我們新建一個WindowsConsole項目(方便而已,你完全可以新建一個其它的,只要能輸出結果就行)

在項目上右擊,添加服務引用。

注:必須最低是.NET 3.0項目才會出現此選項,如果發現沒有,請查看是否該項目創建版本低于3.0了

服務引用的地址就是剛才系統默認分配的地址

添加完成后,在代碼中調用下面的代碼,即可看到調用成功了

IService1 serv = new Service1Client(); Console.WriteLine(serv.GetData(2));

?

根本不需要特意寫一個HOST,因為VS已經提供WCF的服務主機了。

先跨進WCF的門檻,然后再慢慢研究如果托管,不是更好嗎。

轉載于:https://www.cnblogs.com/vanpan/archive/2009/02/11/3583045.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的用VS 2008开发WCF(一)——最快速的WCF入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。