.NET 中的 SOAP 客户端 - 参考或示例? [英] SOAP client in .NET - references or examples?

查看:20
本文介绍了.NET 中的 SOAP 客户端 - 参考或示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我正在创建一个网络服务站点,它将通过 SOAP 和可能的其他协议提供多种类型的简单服务.目标是使转换、RSS 解析、垃圾邮件检查和许多其他类型的工作变得容易.该网站将主要面向初学者开发者.

我的问题:

我从未为此开发过任何 C# 或 .NET.多年前我确实破解了一些 VB6,但仅此而已.现在我需要一些在 C# 中通过 SOAP 进行 RPC 调用的示例.我尝试在网上和 Stack Overflow 上搜索以找到这个,但没有找到很多资源,而且我不知道如何对资源进行排名(哪些是旧的?哪些是不正确的?等等).

我创建了一个简单的示例服务,在 PHP 中是这样调用的:

getCurrentYear();//该方法返回一个整数,称为年"?>

我现在想在 C# 中尽可能轻松地调用此方法.非常欢迎所有参考资料和示例.我该从哪里开始?我可以使用哪些类/模块/任何东西?

如果有更好的通信框架(后端旨在可扩展),该解决方案根本不必涉及 SOAP,但请注意,服务器端是在 Unix 上用 PHP 实现的,因此微软的专有解决方案不在服务器端的问题.

请注意,我需要这个,所以我可以为 J. Random Web Developer 编写可能遵循的文档(即使他们在共享网络托管上).因此,我认为最好的方法应该是仅在代码中执行此操作,但当然也欢迎其他方式执行此操作.

解决方案

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

有两种主要的方法:

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

逐步添加服务引用

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

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

    我正在使用

  4. 转到您的 C# 源代码并添加以下内容

    使用 WindowsFormsApplication1.ServiceReference1

  5. 现在您可以通过这种方式调用服务.

    Service1Client service = new Service1Client();int year = service.getCurrentYear();

希望这会有所帮助.如果您遇到任何问题,请告诉我们.

Background:

I am creating a webservices site which will provide many types of simple services over SOAP and possibly other protocols too. The goal is to make it easy to do for example conversions, RSS parsing, spam checks and many other types of work. The site will be targeted mostly at beginner developers.

My Problem:

I have never developed any C#, or .NET for that matter. I did hack some VB6 many years ago but that's it. Now I need some examples of doing RPC calls over SOAP in C#. I have tried to search the web, and Stack Overflow, to find this but didn't find many resources, and I have no idea how to rank the resources (which are old? which are incorrect? etc).

I have created a simple example service, which is called like this in PHP:

<?php
$client = new SoapClient('http://webservi.se/year'); //URL to the WSDL
echo $client->getCurrentYear(); //This method returns an integer, called "year"
?>

I now want to call this method as easily as possible in C#. All references and examples are very welcome. Where do I begin? Which classes/modules/whatever can I utilize?

The solution does not have to involve SOAP at all if there are better communication frameworks (the back end is meant to be extensible), but note that the server side is implemented in PHP on Unix so proprietary solutions from Microsoft are out of the question on the server side.

Note that I need this so I can write documentation possible for J. Random Web Developer to follow (even if they are on shared web hosting). I therefore think the best approach should be to do this in code only, but even other ways of doing this are of course welcome.

解决方案

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.

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

  4. 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.

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

    using WindowsFormsApplication1.ServiceReference1
    

  6. Now you can call the service this way.

    Service1Client service = new Service1Client();
    int year = service.getCurrentYear();
    

Hope this helps. If you encounter any problems, let us know.

这篇关于.NET 中的 SOAP 客户端 - 参考或示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆