WCF入門教程(二)從零做起-創建WCF服務
通過最基本的操作看到最簡單的WCF如何實現的。這是VS的SDK默認創建的樣本
1、創建WCF服務庫
2、看其生成結構
1)IService1.cs(協議)
定義了協議,具體什么操作,操作的參數和返回值等信息
通過ServiceContract、OperationContract、DataContract、DataMember等屬性參數,將服務、操作、數據結構定義清楚。
在這里我們還清晰看到,WCF服務傳輸數據類型不只是通用數據類型,而且還可以傳輸自定義的復雜類型。哪些字段要進行傳輸,都可以通過DataMember標簽予以指定。
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace SecondWCFSample { // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“IService1”。 [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: 在此添加您的服務操作 } // 使用下面示例中說明的數據協定將復合類型添加到服務操作 [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; } } } }
2)Service1.cs(服務實現)
實現協議定義的方法、數據結構。
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace SecondWCFSample { // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的類名“Service1”。 public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } } }
3)App.Config(配置)
定義了了Host,Endpoint,我們需要認真閱讀這個配置文件的結構,以及備注說明,在以后系統開發中肯定會用到。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <!-- 部署服務庫項目時,必須將配置文件的內容添加到 主機的 app.config 文件中。System.Configuration 不支持庫的配置文件。--> <system.serviceModel> <services> <service name="SecondWCFSample.Service1"> <host> <baseAddresses> <add baseAddress = "http://localhost:8732/Design_Time_Addresses/SecondWCFSample/Service1/" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否則地址將與上面提供的基址相關 --> <endpoint address ="" binding="wsHttpBinding" contract="SecondWCFSample.IService1"> <!-- 部署時,應刪除或替換下列標識元素,以反映 用來運行所部署服務的標識。刪除之后,WCF 將 自動推斷相應標識。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元數據交換終結點供相應的服務用於向客戶端做自我介紹。 --> <!-- 此終結點不使用安全綁定,應在部署前確保其安全或將其刪除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- 為避免泄漏元數據信息, 請在部署前將以下值設置為 false 並刪除上面的元數據終結點 --> <serviceMetadata httpGetEnabled="True"/> <!-- 要接收故障異常詳細信息以進行調試, 請將以下值設置為 true。在部署前設置為 false 以避免泄漏異常信息--> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
3、測試運行
直接以這個項目為啟動項目,即可看到WCF測試客戶端。
我們可以看到WCF測試客戶端,包括,具體協議。然后我們點擊某一個操作,即可看到起請求參數列表,填入合適的值,點擊調用,即可看到最后的運行結果
4、增加方法
在以后的協定中增加方法HelloWorld
代碼如下:
[ServiceContract(Namespace="http://wcf.yank.com",Name="Service1",ProtectionLevel=ProtectionLevel.EncryptAndSign)] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); string HelloWorld(string name); // TODO: 在此添加您的服務操作 }
在Service類中實現
public string HelloWorld(string name) { return string.Format("Hello {0},Welcome to the WCF world.", name); }
直接運行,我們會發現,測試窗口沒有看到此方法,為什么呢?
原因:接口方法中,並沒有指定[OperationContract],這是操作的協定標識。
我們加上,運行就能看到了。修改后:
[OperationContract] string HelloWorld(string name);
運行結果:
其他的屬性標簽也直接關系着服務協定,關於屬性標簽的有關問題參見:
http://www.cnblogs.com/yank/p/3666672.html
5、增加新的服務協定
聲明服務協定
using System; using System.ServiceModel; namespace SecondWCFSample { [ServiceContract] public interface IContact { [OperationContract] string HelloWorld(string name); } }
實現服務:
using System; namespace SecondWCFSample { public class Contact: IContact { public string HelloWorld(string name) { return string.Format("Hello {0},Welcome to the WCF world.", name); } } }
配置服務:
在Services的節點下增加新的服務
<service name="SecondWCFSample.Contact"> <host> <baseAddresses> <add baseAddress = "http://localhost:8732/Design_Time_Addresses/SecondWCFSample/Contact/" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否則地址將與上面提供的基址相關 --> <endpoint address ="" binding="wsHttpBinding" contract="SecondWCFSample.IContact"> <!-- 部署時,應刪除或替換下列標識元素,以反映 用來運行所部署服務的標識。刪除之后,WCF 將 自動推斷相應標識。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元數據交換終結點供相應的服務用於向客戶端做自我介紹。 --> <!-- 此終結點不使用安全綁定,應在部署前確保其安全或將其刪除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service>
運行結果:
6、源碼地址
關於如何書寫配置文件,這里不展開介紹,具體可見:
WCF入門教程(五)配置文件
http://www.cnblogs.com/yank/p/3668371.html