C# 如何异步调用此 Web 服务?

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

How can I call this webservice asynchronously?

c#web-servicesasynchronous

提问by Edward Tanguay

In Visual Studio I created a web service(and checked "generate asynchronous operations") on this URL:

在 Visual Studio 中,我在此 URL 上创建了一个Web 服务(并选中了“生成异步操作”):

http://www.webservicex.com/globalweather.asmx

http://www.webservicex.com/globalweather.asmx

and can get the data out synchronouslybut what is the syntax for getting the data out asychronously?

并且可以同步获取数据但是异步获取数据的语法是什么?

using System.Windows;
using TestConsume2343.ServiceReference1;
using System;
using System.Net;

namespace TestConsume2343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            GlobalWeatherSoapClient client = new GlobalWeatherSoapClient();

            //synchronous
            string getWeatherResult = client.GetWeather("Berlin", "Germany");
            Console.WriteLine("Get Weather Result: " + getWeatherResult); //works

            //asynchronous
            client.BeginGetWeather("Berlin", "Germany", new AsyncCallback(GotWeather), null);
        }

        void GotWeather(IAsyncResult result)
        {
            //Console.WriteLine("Get Weather Result: " + result.???); 
        }

    }
}

Answer:

回答:

Thanks TLiebe, with your EndGetWeathersuggestion I was able to get it to work like this:

谢谢 TLiebe,根据你的EndGetWeather建议,我能够让它像这样工作:

using System.Windows;
using TestConsume2343.ServiceReference1;
using System;

namespace TestConsume2343
{
    public partial class Window1 : Window
    {
        GlobalWeatherSoapClient client = new GlobalWeatherSoapClient();

        public Window1()
        {
            InitializeComponent();
            client.BeginGetWeather("Berlin", "Germany", new AsyncCallback(GotWeather), null);
        }

        void GotWeather(IAsyncResult result)
        {
            Console.WriteLine("Get Weather Result: " + client.EndGetWeather(result).ToString()); 
        }

    }
}

采纳答案by TLiebe

In your GotWeather() method you need to call the EndGetWeather() method. Have a look at some of the sample code at MSDN. You need to use the IAsyncResult object to get your delegate method so that you can call the EndGetWeather() method.

在您的 GotWeather() 方法中,您需要调用 EndGetWeather() 方法。查看MSDN上的一些示例代码。您需要使用 IAsyncResult 对象来获取您的委托方法,以便您可以调用 EndGetWeather() 方法。

回答by Pierre-Alain Vigeant

I suggest using the event provided by the auto-generated proxy instead of messing with the AsyncCallback

我建议使用自动生成的代理提供的事件,而不是搞乱 AsyncCallback

public void DoWork()
{
    GlobalWeatherSoapClient client = new GlobalWeatherSoapClient();
    client.GetWeatherCompleted += new EventHandler<WeatherCompletedEventArgs>(client_GetWeatherCompleted);
    client.GetWeatherAsync("Berlin", "Germany");
}

void client_GetWeatherCompleted(object sender, WeatherCompletedEventArgs e)
{
    Console.WriteLine("Get Weather Result: " + e.Result);
}