.NET 中的 SOAP 客户端 - 参考还是示例?

发布于 2024-09-06 20:41:52 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夏天碎花小短裙 2024-09-13 20:41:53

先决条件:您已经拥有该服务并发布了 WSDL 文件,并且您希望从 C# 客户端应用程序调用您的 Web 服务。

有两种主要方法可以实现此目的:

A) ASP.NET 服务,这是实现 SOA 的旧方法
B) WCF,正如 John 所建议的,它是 MS 的最新框架,提供了许多协议,包括开放协议和 MS 专有协议。

逐步添加服务引用

最简单的方法是在 C# 应用程序中生成代理类(这个过程称为添加服务引用)。

  1. 在 Visual Studio 中打开您的项目(或创建一个新项目),
  2. 在解决方案资源管理器中右键单击该项目(在项目上,而不是解决方案上),然后单击“添加服务引用”
  3. 将出现一个对话框,如下面的屏幕截图所示。输入 wsdl 文件的 url 并单击“确定”。请注意,如果您在点击“确定”后收到错误消息,请尝试从 url 中删除 ?wsdl 部分。

添加服务参考对话框

我正在使用 http://www.dneonline.com/以calculator.asmx?WSDL 为例,

  1. 在“解决方案资源管理器”中展开“服务引用”,然后双击“CalculatorServiceReference”(或者您在上一步中为服务命名的任何名称)。

您应该看到生成的代理类名称和命名空间。

在我的例子中,命名空间是SoapClient.CalculatorServiceReference,代理类的名称是CalculatorSoapClient。正如我上面所说,类名可能因您的情况而异。

服务参考proxy calss

  1. 转到 C# 源代码并添加以下内容

    使用 WindowsFormsApplication1.ServiceReference1
    
  2. 现在您可以通过这种方式调用服务。

    Service1Client 服务 = new Service1Client();
    int 年 = service.getCurrentYear();
    

Prerequisites: You already have the service and published WSDL file, and you want to call your web service from C# client application.

There are 2 main way of doing this:

A) ASP.NET services, which is old way of doing SOA
B) WCF, as John suggested, which is the latest framework from MS and provides many protocols, including open and MS proprietary ones.

Adding a service reference step by step

The simplest way is to generate proxy classes in C# application (this process is called adding service reference).

  1. Open your project (or create a new one) in visual studio
  2. Right click on the project (on the project and not the solution) in Solution Explorer and click Add Service Reference
  3. A dialog should appear shown in screenshot below. Enter the url of your wsdl file and hit Ok. Note that if you'll receive error message after hitting ok, try removing ?wsdl part from url.

add service reference dialog

I'm using http://www.dneonline.com/calculator.asmx?WSDL as an example

  1. Expand Service References in Solution Explorer and double click CalculatorServiceReference (or whatever you named the named the service in the previous step).

You should see generated proxy class name and namespace.

In my case, the namespace is SoapClient.CalculatorServiceReference, the name of proxy class is CalculatorSoapClient. As I said above, class names may vary in your case.

service reference proxy calss

  1. Go to your C# source code and add the following

    using WindowsFormsApplication1.ServiceReference1
    
  2. Now you can call the service this way.

    Service1Client service = new Service1Client();
    int year = service.getCurrentYear();
    
¢好甜 2024-09-13 20:41:53

我已经完成了您所谈论的相当多的内容,并且平台之间的 SOAP 互操作性有一个基本规则:合同第一。不要从代码中派生 WSDL,然后尝试在不同的平台上生成客户端。任何超过“Hello World”类型的函数都很可能无法生成代码,无法在运行时对话,或者(我最喜欢的)无法正确发送或接收所有数据而不引发错误。

也就是说,WSDL 是复杂、令人讨厌的东西,我尽可能避免从头开始编写它。以下是可靠的服务互操作指南(使用 Web 引用、WCF、Axis2/Java、WS02、Ruby、Python 等):

  • 继续以代码优先的方式创建初始 WSDL。然后,删除代码并从 WSDL 重新生成服务器类。几乎每个平台都有这方面的工具。这将向您展示您的特定平台有哪些奇怪的习惯,并且您可以开始调整 WSDL,使其变得更简单、更直接。调整,重新生成,重复。通过这种方式你会学到很多东西,而且是可移植的知识。
  • 对于复杂类型,坚持使用简单的旧语言类(POCO、POJO 等)。不要使用特定于平台的结构,例如 List<>或数据表。即使 PHP 关联数组看起来可以工作,但也会以难以跨平台调试的方式失败。
  • 坚持基本数据类型:bool、int、float、string、date(Time) 和数组。很可能,您对数据类型的了解越具体,随着时间的推移,您对新需求的敏捷性就越低。如果可以避免的话,您不想更改 WSDL。
  • 上述数据类型的一个例外 - 给自己提供某种 NameValuePair 机制。您无法相信这些事情的清单可以在灵活性方面节省您多少次。
  • 为您的 WSDL 设置真实的命名空间。这并不难,但你可能不相信我在命名空间“http://www.tempuri.org”中看到了多少 Web 服务。另外,使用 URN(“urn:com-myweb-servicename-v1”,而不是基于 URL 的命名空间(“http://servicename.myweb.com/v1”)。它不是一个网站,而是一组抽象字符这定义了一个逻辑分组。我可能有十几个人打电话给我寻求支持,并说他们去了“网站”,但它不起作用

:)

I have done quite a bit of what you're talking about, and SOAP interoperability between platforms has one cardinal rule: CONTRACT FIRST. Do not derive your WSDL from code and then try to generate a client on a different platform. Anything more than "Hello World" type functions will very likely fail to generate code, fail to talk at runtime or (my favorite) fail to properly send or receive all of the data without raising an error.

That said, WSDL is complicated, nasty stuff and I avoid writing it from scratch whenever possible. Here are some guidelines for reliable interop of services (using Web References, WCF, Axis2/Java, WS02, Ruby, Python, whatever):

  • Go ahead and do code-first to create your initial WSDL. Then, delete your code and re-generate the server class(es) from the WSDL. Almost every platform has a tool for this. This will show you what odd habits your particular platform has, and you can begin tweaking the WSDL to be simpler and more straightforward. Tweak, re-gen, repeat. You'll learn a lot this way, and it's portable knowledge.
  • Stick to plain old language classes (POCO, POJO, etc.) for complex types. Do NOT use platform-specific constructs like List<> or DataTable. Even PHP associative arrays will appear to work but fail in ways that are difficult to debug across platforms.
  • Stick to basic data types: bool, int, float, string, date(Time), and arrays. Odds are, the more particular you get about a data type, the less agile you'll be to new requirements over time. You do NOT want to change your WSDL if you can avoid it.
  • One exception to the data types above - give yourself a NameValuePair mechanism of some kind. You wouldn't believe how many times a list of these things will save your bacon in terms of flexibility.
  • Set a real namespace for your WSDL. It's not hard, but you might not believe how many web services I've seen in namespace "http://www.tempuri.org". Also, use a URN ("urn:com-myweb-servicename-v1", not a URL-based namespace ("http://servicename.myweb.com/v1". It's not a website, it's an abstract set of characters that defines a logical grouping. I've probably had a dozen people call me for support and say they went to the "website" and it didn't work.

</rant> :)

相对绾红妆 2024-09-13 20:41:53

如果您可以让它在浏览器中运行,那么像这样简单的事情就可以工作

var webRequest = WebRequest.Create(@"http://webservi.se/year/getCurrentYear");

using (var response = webRequest.GetResponse())
{
    using (var rd = new StreamReader(response.GetResponseStream()))
    {
        var soapResult = rd.ReadToEnd();
    }
}

If you can get it to run in a browser then something as simple as this would work

var webRequest = WebRequest.Create(@"http://webservi.se/year/getCurrentYear");

using (var response = webRequest.GetResponse())
{
    using (var rd = new StreamReader(response.GetResponseStream()))
    {
        var soapResult = rd.ReadToEnd();
    }
}
寄意 2024-09-13 20:41:53

查看“使用 WCF使用 PHP 的服务”。它解释了您需要的基础知识。

作为理论总结:

WCF 或 Windows Communication Foundation 是一种技术,允许定义从调用方式(底层通信方法)中抽象出来的服务。

这个想法是,您定义一个关于服务做什么和提供什么的契约,还定义另一个关于实际使用服务的通信方法(无论是 TCP、HTTP 还是 SOAP)的契约。

您可以在 此处查看文章的第一部分,解释如何创建一个非常基本的 WCF 服务。

更多资源:

将 WCF 与 PHP5 结合使用

还可以看看 NuSOAP。如果您现在NuSphere,这是一个可以让您从 PHP 连接到 WCF 服务的工具包。

Take a look at "using WCF Services with PHP". It explains the basics of what you need.

As a theory summary:

WCF or Windows Communication Foundation is a technology that allow to define services abstracted from the way - the underlying communication method - they'll be invoked.

The idea is that you define a contract about what the service does and what the service offers and also define another contract about which communication method is used to actually consume the service, be it TCP, HTTP or SOAP.

You have the first part of the article here, explaining how to create a very basic WCF Service.

More resources:

Using WCF with PHP5.

Aslo take a look to NuSOAP. If you now NuSphere this is a toolkit to let you connect from PHP to an WCF service.

各自安好 2024-09-13 20:41:53

你找错地方了。您应该查找Windows 通信框架


WCF既可以用在客户端上,也可以用在服务器上。

You're looking in the wrong place. You should look up Windows Communication Framework.


WCF is used both on the client and on the server.

微凉徒眸意 2024-09-13 20:41:53

在这里您可以找到一个很好的教程,用于从 .NET 客户端应用程序调用基于 NuSOAP 的 Web 服务。但在我看来,您还应该考虑 PHP 的 WSO2 Web 服务框架 (WSO2 WSF/PHP) 用于维修。请参阅 适用于 PHP 2.0 的 WSO2 Web 服务框架有力提升行业唯一用于创建 SOAP 和 REST 服务的 PHP 库。还有一个关于它的网络研讨会

现在,在.NET世界中,考虑到互操作性问题,我也鼓励使用WCF。可以在此处找到互操作性示例,但此示例使用 PHP 客户端 + WCF 服务而不是对面的。请随意实施 PHP 服务 & WFC 客户端。

codeplex.com 上有一些与 WCF 相关的开源项目,我发现它们非常高效。这些项目对于设计和开发非常有用。实现 Win Forms 和 Windows Presentation Foundation 应用程序:智能客户端网页客户端移动客户端。它们可以与 WCF 结合使用来明智地调用任何类型的 Web 服务。

一般来说,模式和模式实践团队总结了良好的实践和经验在处理 .NET 平台(特别是 Web)的各种开源项目中进行设计。因此,我认为对于任何与 .NET 客户端相关的设计决策来说,这是一个很好的起点。

Here you can find a nice tutorial for calling a NuSOAP-based web-service from a .NET client application. But IMO, you should also consider the WSO2 Web Services Framework for PHP (WSO2 WSF/PHP) for servicing. See WSO2 Web Services Framework for PHP 2.0 Significantly Enhances Industry’s Only PHP Library for Creating Both SOAP and REST Services. There is also a webminar about it.

Now, in .NET world I also encourage the use of WCF, taking into account the interoperability issues. An interoperability example can be found here, but this example uses a PHP-client + WCF-service instead of the opposite. Feel free to implement the PHP-service & WFC-client.

There are some WCF's related open source projects on codeplex.com that I found very productive. These projects are very useful to design & implement Win Forms and Windows Presentation Foundation applications: Smart Client, Web Client and Mobile Client. They can be used in combination with WCF to wisely call any kind of Web services.

Generally speaking, the patterns & practices team summarize good practices & designs in various open source projects that dealing with the .NET platform, specially for the web. So I think it's a good starting point for any design decision related to .NET clients.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文