C# Web 参考与服务参考

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2158106/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:01:36  来源:igfitidea点击:

Web Reference vs. Service Reference

c#.net-3.5paypal

提问by PositiveGuy

I just hit a huge brick wall with Paypal. I had created a regular C# project to create some wrapper classes using their WSDL.

我刚刚用 Paypal 撞到了一堵巨大的砖墙。我创建了一个常规的 C# 项目来使用它们的 WSDL 创建一些包装类。

If you create a non-web project, the only option you get to add a wsdl is a Web Service Reference. And this builds kinda the same set of proxy classes as a Web Reference would but not really..it adds more that even the PayPal guys are not aware of.

如果创建非 Web 项目,添加 wsdl 的唯一选项是 Web 服务引用。这构建了与 Web 引用相同的一组代理类,但实际上并非如此……它增加了甚至 PayPal 人员都不知道的更多内容。

So I was this entire time looking for the right Interface in this list of proxy classes to use as the service (SoapBinding) and the PayPalAPIAASoapBindingwas not there I kept telling our PayPal point in contact.

所以我一直在这个代理类列表中寻找正确的接口以用作服务(SoapBinding),PayPalAPIAASoapBinding但我一直告诉我们的 PayPal 联系人。

I could only see the following 2 Interfaces that appeared to me what I needed to use since I did not see a PayPalAPIAASoapBindingwhich you CAN see in a Web Reference based service reference:

我只能看到以下 2 个接口,它们在我看来是我需要使用的,因为我没有看到PayPalAPIAASoapBinding您可以在基于 Web 参考的服务参考中看到的接口:

PayPalAPIAAInterfaceClient
PayPalAPIInterfaceClient 

So I figured out oh, I probably had created a Service Reference vs. a Web Reference which Web Reference is an option in a Web project. But I don't want my service reference tightly coupled to my web project. So that's why I created the C# Project.

所以我想通了哦,我可能已经创建了一个服务引用与一个 Web 引用,其中 Web 引用是 Web 项目中的一个选项。但我不希望我的服务引用与我的 Web 项目紧密耦合。这就是我创建 C# 项目的原因。

So what the heck is a Service Reference vs. Web Reference? And how am I supposed to separate this out into anther project if Service Reference is going to throw me a loop and give me a set of different interfaces than a Web Reference would?

那么服务引用与 Web 引用到底是什么?如果服务引用会给我一个循环并为我提供一组与 Web 引用不同的接口,我应该如何将其分离到另一个项目中?

Also, to make things even MORE confusing, VS 2008 has a Web Service Application project.

此外,更令人困惑的是,VS 2008 有一个 Web 服务应用程序项目。

So what do I use? We're using the .NET 3.5 framework and we're notready to move to WCF. So can I still use the new Service Reference even if not using WCF or what? IF you're using .NET 3.5 and not WCF yet and you still want to do basic web services, do you still go the Service Reference route and just not use the WCF framework? Meaning can it be used like a .NET 2.0 Web Reference still, just that you're going to get an entirely different generation of the WSDL?

那我用什么? 我们正在使用 .NET 3.5 框架,但我们还没有准备好迁移到 WCF。那么即使不使用 WCF 或什么,我仍然可以使用新的服务引用吗?如果您正在使用 .NET 3.5 而不是 WCF 并且您仍然想要做基本的 Web 服务,您是否仍然选择服务引用路线而不使用 WCF 框架?这意味着它仍然可以像 .NET 2.0 Web Reference 一样使用,只是您将获得完全不同的 WSDL 代?

采纳答案by marc_s

Add Web Referenceis the old-style, deprecated ASP.NET webservices (ASMX) technology (using only the XmlSerializer for your stuff) - if you do this, you get an ASMX client for an ASMX web service. You can do this in just about any project (Web App, Web Site, Console App, Winforms - you name it).

添加 Web 引用是旧式的、已弃用的 ASP.NET Web 服务 (ASMX) 技术(仅使用 XmlSerializer 来处理您的内容) - 如果您这样做,您将获得 ASMX Web 服务的 ASMX 客户端。您几乎可以在任何项目中执行此操作(Web 应用程序、网站、控制台应用程序、Winforms - 您可以命名)。

Add Service Referenceis the new way of doing it, adding a WCF service reference, which gives you a much more advanced, much more flexible service model than just plain old ASMX stuff.

添加服务引用是一种新的方法,添加 WCF 服务引用,它为您提供了一个更先进、更灵活的服务模型,而不仅仅是简单的旧 ASMX 东西。

Since you're not ready to move to WCF, you can also still add the old-style web reference, if you really must: when you do a "Add Service Reference", on the dialog that comes up, click on the [Advanced] button in the button left corner:

由于您还没有准备好迁移到 WCF,您仍然可以添加旧式 Web 引用,如果您确实必须:当您执行“添加服务引用”时,在出现的对话框中,单击 [高级] 按钮左上角的按钮:

alt text

替代文字

and on the next dialog that comes up, pick the [Add Web Reference] button at the bottom.

在出现的下一个对话框中,选择底部的 [Add Web Reference] 按钮。

回答by Otávio Décio

Adding a service reference allows you to create a WCF client, which can be used to talk to a regular web service provided you use the appropriate binding. Adding a web reference will allow you to create onlya web service (i.e., SOAP) reference.

添加服务引用允许您创建 WCF 客户端,如果您使用适当的绑定,它可用于与常规 Web 服务对话。添加 Web 引用将允许您创建Web 服务(即 SOAP)引用。

If you are absolutely certain you are not ready for WCF (really don't know why) then you should create a regular web service reference.

如果您绝对确定您还没有为 WCF 做好准备(真的不知道为什么),那么您应该创建一个常规的 Web 服务引用。

回答by Moose

If I understand your question right:

如果我理解你的问题:

To add a .net 2.0 Web Service Reference instead of a WCF Service Reference, right-click on your project and click 'Add Service Reference.'

要添加 .net 2.0 Web 服务引用而不是 WCF 服务引用,请右键单击您的项目并单击“添加服务引用”。

Then click "Advanced.." at the bottom left of the dialog.

然后单击对话框左下角的“高级...”。

Then click "Add Web Reference.." on the bottom left of the next dialog.

然后单击下一个对话框左下角的“添加 Web 引用..”。

Now you can add a regular SOAP web reference like you are looking for.

现在您可以添加您正在寻找的常规 SOAP Web 参考。

回答by user3628475

In the end, both do the same thing. There are some differences in code: Web Services doesn't add a Root namespace of project, but Service Reference adds service classes to the namespace of the project. The ServiceSoapClientclass gets a different naming, which is not important. In working with TFS I'd rather use Service Reference because it works better with source control. Both work with SOAP protocols.

最后,两者都做同样的事情。代码上有一些区别: Web Services 没有添加项目的 Root 命名空间,但 Service Reference 将服务类添加到项目的命名空间。本ServiceSoapClient类获取不同的命名,这并不重要。在使用 TFS 时,我宁愿使用服务引用,因为它更适合源代码控制。两者都适用于 SOAP 协议。

I find it better to use the Service Reference because it is new and will thus be better maintained.

我发现使用服务参考更好,因为它是新的,因此会得到更好的维护。