简体   繁体   English

SOAP Web 服务和 C#

[英]SOAP Web service and C#

Im required to listen requests from a web service in C# and respond to them ion C#.. whats the approach to this?我需要在 C# 中侦听来自 web 服务的请求,并在 C# 中对它们做出响应。这是什么方法?

I am presuming you already have a web service you wish to consume.我假设您已经拥有想要使用的 web 服务。 There are several examples on consuming web services on the web (eg: Consuming web services from a WinForms app ).在 web 上使用 web 服务有几个示例(例如:从 WinForms 应用程序使用 web 服务)。

Adding a web reference添加 web 参考

First, you need to add a web reference to your C# project.首先,您需要将web 引用添加到您的 C# 项目中。 In VS2005, you can do this by right-clicking the project and selecting "Add web reference", and the providing the url of the web service.在 VS2005 中,您可以通过右键单击项目并选择“添加 web 引用”并提供 Z2567A5EC9705EB7AC2C984033E06189D 服务的 url 来执行此操作。 In VS2008 or newer, there are a couple of extra clicks as described here .在 VS2008 或更新版本中,有几个额外的点击,如此处所述

After you've done that, VS will generate all the necessary proxy classes for you, with methods for both synchronous and asynchronous invocation, which you can use as if the object was instantiated locally.完成后,VS 将为您生成所有必要的代理类,包括同步和异步调用的方法,您可以使用这些方法,就像 object 在本地实例化一样。

Viewing generated classes查看生成的类

For example, if your web service has a single method ( DoSomething ), and is located at www.example.com/MyService.asmx (and also named "MyService"), Visual Studio will create a class called `MyService" which will look something like this:例如,如果您的 web 服务有一个方法 ( DoSomething ),并且位于www.example.com/MyService.asmx (也名为“MyService”),Visual Studio 将创建一个名为“MyService”的 class,它看起来像这样的东西:

namespace MyNamespace // <-- this is the name you choose when you 
{                     //     added the web reference

    public class MyService : SoapHttpClientProtocol
    {
         // synchronous execution
         public void DoSomething()
         {

         }

         // async execution
         public void DoSomethingAsync()
         {

         }

         // callback event for async execution
         public event DoSomethingCompletedEventHandler DoSomethingCompleted;
     }
}

To examine the contents of the generated namespace, double click your web reference in Solution Explorer (it should be inside a folder called "Web References").要检查生成的命名空间的内容,请在解决方案资源管理器中双击 web 引用(它应该位于名为“Web References”的文件夹中)。 This will open the Object browser.这将打开 Object 浏览器。

Using generated classes synchronously同步使用生成的类

The simplest way to use your class is to create an instance, and invoke the method:使用 class 的最简单方法是创建一个实例,然后调用该方法:

// create a new instance of the service
MyService service = new MyService();

// invoke the method
service.DoSomething(); // <-- this will block the thread until completed

Using generated classes asynchronously异步使用生成的类

To use the async version, you can attach an event handler:要使用异步版本,您可以附加一个事件处理程序:

// create a new instance of the service
MyService service = new MyService();

// attach the event handler
service.DoSomethingCompleted += MyEventHandler;

// invoke the method asynchronously
service.DoSomethingAsync(); // <-- this will be invoked on a background thread

I would suggest looking at This to get started.我建议看一下这个开始。

If your question is: do Web Services have to be written in C#, the answer is "no".如果您的问题是:Web 服务必须写在 C# 中,答案是“否”。 You can write Web Services in many languages, including Java, VB.NET and even COBOL.NET.您可以使用多种语言编写 Web 服务,包括 Java、VB.NET 甚至 COBOL.NET。

If your question is: do Web Services have to be written in the same language as the programs that consume them, the answer again is "no", as long as your Web Services are not in the same Visual Studio project as the client(s).如果您的问题是:Web 服务是否必须用与使用它们的程序相同的语言编写,答案再次是“否”,只要您的 Web 服务与客户端不在同一个 Visual Studio 项目中(s )。

Choose "Add Service Reference" from the Solution Explorer (right click to your project) and than add the URL of your WebService you want to consume to the Adresss Bar and say "Go".从解决方案资源管理器中选择“添加服务参考”(右键单击您的项目),然后将您要使用的 WebService 的 URL 添加到地址栏并说“开始”。

The client should be created automatically from Visual Studio.客户端应从 Visual Studio 自动创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM