开发者

SOAP Web service and C#

Im required to listen requests from a web service in C# and respon开发者_StackOverflow社区d to them ion C# .. whats the approach to this ?


I am presuming you already have a web service you wish to consume. There are several examples on consuming web services on the web (e.g.: Consuming web services from a WinForms app).

Adding a web reference

First, you need to add a web reference to your C# project. 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. In VS2008 or newer, there are a couple of extra clicks as described here.

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.

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:

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"). This will open the Object browser.

Using generated classes synchronously

The simplest way to use your class is to create an instance, and invoke the method:

// 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". You can write Web Services in many languages, including Java, VB.NET and even 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).


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

The client should be created automatically from Visual Studio.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜