用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入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7 无法启动网络(servi
- 下一篇: 查询进程并杀死该进程