Visual Studio/SOAP - "添加服务引用"与"添加Web服务引用"

chi*_*tom 17 .net wcf soap web-services visual-studio

我发现我可以将我计划使用的SOAP/WSDL服务作为"Web服务引用"(System.Web.Services)或"服务引用"(System.ServiceModel/WCF)导入到我的解决方案中.

我想知道差异是什么.我理解'添加服务引用'/ WCF比较新,在System.Web.Services上使用它有什么不利之处,或者它现在是在.Net中使用SOAP服务的首选方式?

mar*_*c_s 21

确实使用了首选和最有用的方法Add Service Reference.这会将您的服务添加为WCF客户端代理.

Add Web Reference 是"旧式"ASMX/ASP.NET webservice的服务方式.

WCF是比ASMX更好的选择,因为:

  • 它更新,将来会得到支持(ASMX即将推出); 如果你现在学习它,你将不必在以后ASMX肯定消失时学习它
  • 它在各个方面提供了更大的灵活性
  • 您只能使用HTTP作为协议来托管ASMX服务IIS; WCF可以在IIS中托管; 在Windows NT服务中自托管; WCF可以使用HTTP,NetTCP,MSMQ和更多协议
  • WCF提供了更多的安全性和其他设置,使其使用起来更加强大

是的,WCF对于真正难以学习的说法很糟糕 - 我并不认为这是真的.看看那些初学者的资源 - 非常有用!


Tad*_*ghe 6

我有一个应用程序,它调用一个用J2EE编写并托管在WebSphere中的现有SOAP服务.

我创建了两个控制台应用程序 - 一个引用该服务作为旧学校Web服务,另一个引用它作为服务引用.

在这两种情况下,Visual Studio都会为服务创建代理类和相应的配置条目.

在服务参考控制台应用程序中,我获得了许多我在Web服务应用程序中看不到的配置选项.特别是,我可以设置最大邮件大小等.

事实上,为了使服务引用控制台应用程序正常工作,我不得不增加默认邮件大小,以便获取在其中一个方法调用中发送的所有数据.

以下是服务参考应用程序中的配置:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="ClaimSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536000" maxBufferPoolSize="524288" maxReceivedMessageSize="65536000"
                    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://urlgoeshere/ClaimService"
                binding="basicHttpBinding" bindingConfiguration="ClaimSoapBinding"
                contract="ClaimService.Claim" name="ClaimService" />
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

在我的旧学校Web服务控制台应用程序中,我根本不需要改变配置来取回发回的大量数据.这是它的配置:

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ServiceTesterOldSchool.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <ServiceTesterOldSchool.Properties.Settings>
            <setting name="ServiceTesterOldSchool_ClaimService_ClaimService"
                serializeAs="String">
                <value>http://urlgoeshere/ClaimService</value>
            </setting>
        </ServiceTesterOldSchool.Properties.Settings>
    </applicationSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

它更简单,但缺少我们通过服务参考获得的许多选项.

在这两种情况下,调用服务的实际代码几乎相同.

但是,要回答你的问题,我认为坚持目前的做事方式很重要.微软有点通过强迫你在你甚至可以添加旧学校Web参考(至少在VS2008中)之前经历几个级别的对话来明确这一点.

我认为WCF方式更灵活,配置更具描述性.

此外,当您向应用程序添加新的WCF组件时,最好保持配置设置一致,而不是在旧学校和WCF之间进行混合和匹配.