visual-studio Visual Studio/SOAP -“添加服务引用”与“添加 Web 服务引用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2192772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio/SOAP - 'Add Service Reference' vs 'Add Web Service Reference'
提问by chillitom
I've found that I can import a SOAP/WSDL service that I plan on using into my solution either as a "Web Service Reference" (System.Web.Services) or as a "Service Reference" (System.ServiceModel / WCF).
我发现我可以将计划使用的 SOAP/WSDL 服务作为“Web 服务参考”(System.Web.Services)或“服务参考”(System.ServiceModel/WCF)导入到我的解决方案中.
I was wondering if what the differences were. I understand that 'Add Service Reference'/WCF is newer, are there any disadvantages to using it over System.Web.Services or is it now the preferred way of consuming SOAP services in .Net?
我想知道是否有什么区别。我知道“添加服务引用”/WCF 是较新的,在 System.Web.Services 上使用它是否有任何缺点,或者它现在是在 .Net 中使用 SOAP 服务的首选方式吗?
回答by marc_s
The preferred and most useful way is indeed to use Add Service Reference. This will add your service as WCF client side proxy.
首选和最有用的方法确实是使用Add Service Reference. 这会将您的服务添加为 WCF 客户端代理。
Add Web Referenceis the "old-style" ASMX/ASP.NET webservice way of doing things.
Add Web Reference是“旧式”ASMX/ASP.NET web 服务的处理方式。
WCF is the much better choice than ASMX, because:
WCF 是比 ASMX 更好的选择,因为:
- it's newer and will be supported in the future (ASMX is on the way out); if you learn it now, you won't have to learn it later when ASMX is definitely gone
- it offers much more flexibility in every aspect
- you can only ever host an ASMX service is IIS, using HTTP as your protocol; WCF can be hosted in IIS; self-hosted in a Windows NT Service; WCF can use HTTP, NetTCP, MSMQ and many more protocols
- WCF offers a lot more security and other settings, making it much more powerful to use
- 它较新,将来会得到支持(ASMX 即将退出);如果你现在学习它,你就不必在 ASMX 绝对消失后学习它
- 它在各个方面都提供了更大的灵活性
- 你只能托管一个 ASMX 服务是 IIS,使用 HTTP 作为你的协议;WCF可以托管在IIS中;自托管在 Windows NT 服务中;WCF 可以使用 HTTP、NetTCP、MSMQ 和更多协议
- WCF 提供了更多的安全性和其他设置,使其使用起来更加强大
Yes, WCF has a bad rap about being really hard to learn - I don't really find that to be true. Check out those beginner's resources - very useful indeed!
是的,WCF 有一个很难学的坏名声 - 我真的不认为这是真的。查看那些初学者的资源 - 确实非常有用!
- MSDN WCF Developer Centerwhich has everything from beginner's tutorials to articles and sample code
- Endpoint.TV screen cast series by Aaron Skonnard on WCF (at Channel9)- it's an excellent series going from "Creating your first WCF service" and "Creating your first WCF client" all the way to rather advanced topics. Aaron Skonnard very nicely explains everything in 10-15 minutes screencasts - highly recommended!
- DotNet Rocks TV Show #122: Miguel Castro on Extreme WCF
- DotNet Rocks TV Show #135: Keith Elder Demystifies WCF
- MSDN WCF 开发人员中心,其中包含从初学者教程到文章和示例代码的所有内容
- Aaron Skonnard 在 WCF 上的 Endpoint.TV 屏幕投射系列(第 9 频道)- 这是一个出色的系列,从“创建您的第一个 WCF 服务”和“创建您的第一个 WCF 客户端”一直到相当高级的主题。Aaron Skonnard 在 10-15 分钟的截屏视频中很好地解释了所有内容 - 强烈推荐!
- DotNet Rocks 电视节目 #122:Miguel Castro 在 Extreme WCF 上
- DotNet Rocks 电视节目 #135:Keith Elder 揭开 WCF 的神秘面纱
回答by Tad Donaghe
I have an application that's calling an existing SOAP service that's written in J2EE and hosted in WebSphere.
我有一个应用程序调用现有的 SOAP 服务,该服务是用 J2EE 编写的并托管在 WebSphere 中。
I've created two console applications - one referencing the service as an old school Web Service and another that references it as a Service Reference.
我创建了两个控制台应用程序 - 一个将服务引用为老式 Web 服务,另一个将其引用为服务引用。
In both cases, Visual Studio creates a proxy class and appropriate configuration entries for the service.
在这两种情况下,Visual Studio 都会为服务创建代理类和适当的配置条目。
In the Service Reference console app, I get a lot more configuration options that I don't see in the Web Service app. In particular, I can set the maximum message size, etc.
在服务参考控制台应用程序中,我获得了更多在 Web 服务应用程序中看不到的配置选项。特别是,我可以设置最大消息大小等。
In fact, in order to get the Service Reference console app to work properly, I had to increase the default message size in order to get back all the data sent in one of the method calls.
事实上,为了让 Service Reference 控制台应用程序正常工作,我不得不增加默认消息大小,以便取回在其中一个方法调用中发送的所有数据。
Here's what the configuration looks like in the Service Reference app:
以下是 Service Reference 应用程序中的配置:
<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>
In my old school Web Service console app, I didn't have to alter the configuration at all to get back the giant set of data sent back. Here's what its configuration looks like:
在我的老式 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>
It's much simpler, but lacks a lot of the options we get with Service References.
它简单得多,但缺少我们通过服务引用获得的许多选项。
The actual code calling the service is nearly identical in both cases.
在这两种情况下,调用服务的实际代码几乎相同。
To answer your question, though, I think it's important to stick with the current way of doing things. Microsoft kinda makes this clear by forcing you to go through a couple levels of dialogs before you can even add a old school Web Reference (at least in VS2008).
不过,要回答您的问题,我认为坚持当前的做事方式很重要。微软通过强制您在添加旧式 Web 参考(至少在 VS2008 中)之前通过几个级别的对话框来明确说明这一点。
I think the WCF way is more flexible, and the configuration is a lot more descriptive about what's going on.
我认为 WCF 的方式更灵活,配置更能描述正在发生的事情。
Also, as you add new WCF components to your apps, it'll be nice to keep your configuration settings consistent, instead of mixing and matching between old school and WCF.
此外,当您向应用程序添加新的 WCF 组件时,最好保持您的配置设置一致,而不是在老式和 WCF 之间混合和匹配。
回答by Scott Dorman
I think one of the differences is in the autogenerated proxy code for the service. If you go with the service reference, your application will require the WCF layer to communicate. This generally isn't a problem, but if you are writing code that will be run on other platforms (like Mono) you will want to use the web service reference instead (since Mono doesn't support WCF yet.)
我认为差异之一在于服务的自动生成代理代码。如果您使用服务引用,您的应用程序将需要 WCF 层进行通信。这通常不是问题,但如果您正在编写将在其他平台(如 Mono)上运行的代码,您将希望改用 Web 服务引用(因为 Mono 尚不支持 WCF。)

