VS2008中Web Reference和Service Reference的區(qū)別
很早就發(fā)現(xiàn)在vs2008中應(yīng)用web service有兩種方式,即Add Web Reference和Add Service Reference,但是一直不是很清楚這兩者有什么區(qū)別。趁著今天有空實(shí)驗(yàn)一下這兩者的區(qū)別并記錄下來供大家參考。
首先在網(wǎng)上查找,發(fā)現(xiàn)有如下兩個主要區(qū)別:
1.Add Web Reference是由wsdl.exe生成客戶端代理的。
Add Service Reference是由svcutil.exe生成客戶端代理的。
2.Add Web Reference生成的代理可以被.net1.1或.net2.0的客戶端調(diào)用
Add Service Reference生成的代理只能被.net3.0+的客戶端調(diào)用,而且Add Service Reference后不僅生成代理類,在web.config中還會生成相應(yīng)的Tag。
下面是我的實(shí)驗(yàn):
首先建立一個solution,在其中增加三個工程(一個WebApplication,一個Webservice,一個Wcfservice)。
1.測試Web Reference
(1.1)在WebApplication中引用WebService(即WebServiceForTest)
引用后可正常使用,web.config中多出如下配置(url上的端口號和個人機(jī)器相關(guān))
<applicationSettings>
<WebReferAndSvcRefer.Properties.Settings>
<setting name="WebReferAndSvcRefer_AsmxWebRefer_Service1"
serializeAs="String">
<value>http://localhost:1253/Service1.asmx"</value>
</setting>
</WebReferAndSvcRefer.Properties.Settings>
</applicationSettings
(1.2) 在WebApplication中引用WcfService(即WcfServiceForTest)
引用后無法正常使用,web.config中多出的配置和上面類似
<applicationSettings>
<WebReferAndSvcRefer.Properties.Settings>
<setting name="WebReferAndSvcRefer_WcfWebRefer_Service1"
serializeAs="String">
<value>http://localhost:1254/Service1.svc</value>
</setting>
</WebReferAndSvcRefer.Properties.Settings>
</applicationSettings>
2.測試Service Reference
(2.1)在WebApplication中引用WebService(即WebServiceForTest)
引用后可正常使用,web.config中多出如下配置(url上的端口號和個人機(jī)器相關(guān))
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Service1Soap" closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1253/Service1.asmx" binding="basicHttpBinding"
bindingConfiguration="Service1Soap"
contract="AsmxSvcRefer.Service1Soap" name="Service1Soap" />
</client>
</system.serviceModel>
(2.2)在WebApplication中引用WcfService(即WcfServiceForTest)
引用后可正常使用,web.config中多出如下配置(url上的端口號和個人機(jī)器相關(guān))
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1"
closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00"
sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows"
proxyCredentialType="None" realm="" />
<message clientCredentialType="Windows"
negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1254/Service1.svc"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1"
contract="WcfSvcRefer.IService1"
name="WSHttpBinding_IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
3.測試客戶端的.net framework要求
將WebApplication(即WebReferAndSvcRefer)的Target Framework降為2.0
然后發(fā)現(xiàn)在WebReferAndSvcRefer中無法引用Service Reference,證明了Add Service Reference生成的代理只能被.net3.0+的客戶端調(diào)用。
4.總結(jié)
以上的實(shí)驗(yàn)過程基本證明了本文開頭提到的兩個區(qū)別,其中(1.2)Add Web Reference的方式不能使用Wcf也證明了Add Web Reference生成的代理是面向.net1.1或.net2.0的客戶端的(wcf需要.net3.0的支持)。

浙公網(wǎng)安備 33010602011771號