本章內容主要是根據我做的實驗來闡述這2種添加服務針對WCF的不同之處,我們按照示例一步一步來看。
如下是工程的結構:
該WCF服務是通過控制台程序(Host)以自宿的形式發布的,綁定使用wsHttpBinding。我們在Client端分別添加
服務引用(add service references)和添加Web引用(add Web Reference )來引用WCF服務。
以下是客戶端的代碼,分別使用添加服務引用和添加Web引用的服務代理來調用WCF的方法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Yingchao.Client.localhost; using Yingchao.Client.ServiceReference1; namespace Yingchao.Client { class Program { static void Main(string[] args) { // add service reference's proxy Service1Client client = new Service1Client(); Console.WriteLine(client.GetData(111)); // add web reference's proxy Service1 s = new Service1(); Console.WriteLine(s.GetData(1234, true)); Console.Read(); } } }
客戶端配置文件:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="Yingchao.Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <system.serviceModel> <client> <!-- 添加服務引用時自動生成 --> <endpoint address="http://localhost:8732/service" binding="wsHttpBinding" contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"> <identity> <dns value="localhost" /> </identity> </endpoint> </client> </system.serviceModel> <applicationSettings> <!-- 添加Web服務引用時自動生成 --> <Yingchao.Client.Properties.Settings> <setting name="Yingchao_Client_localhost_Service1" serializeAs="String"> <value>http://localhost:8732/service</value> </setting> </Yingchao.Client.Properties.Settings> </applicationSettings> </configuration>
服務端配置文件:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <system.serviceModel> <services> <service name="Yingchao.Service.Service1"> <host> <baseAddresses> <add baseAddress = "http://localhost:8732/service" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否則地址將與上面提供的基址相關 --> <endpoint address ="" binding="wsHttpBinding" contract="Yingchao.Contract.IService1"> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元數據交換終結點供相應的服務用於向客戶端做自我介紹。 --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
我們啟動服務后,運行客戶端,我們看看結果是什么:
我們看到這里添加Web服務代理調用WCF的方法的結果沒有顯示出來,而是出現了"操作超時"錯誤。
那我們更改服務端配置文件的綁定:wsHttpBinding 改成 basicHttpBinding,編譯后更新引用的服務。
然后再次運行客戶端,我們看看結果:
我們看到這次2個引用服務都成功調用。可見添加Web服務應該只能使用basicHttpBinding,也許微軟是為了向前兼容留下的。
然后,分別添加的服務引用生成的Reference.cs里面生成的代碼也不一樣。添加服務引用更偏向WCF規則。
我查資料也發現跟我想的差不多.(http://social.microsoft.com/Forums/zh-CN/xmlwebserviceszhchs/thread/808d870b-49f1-47ac-b105-4beb580bcec6)