我怎么能异步调用此WebService? [英] How can I call this webservice asynchronously?

查看:258
本文介绍了我怎么能异步调用此WebService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio中我创建了一个 Web服务(并检查生成异步操作),在这个网址:




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




和可获取数据的同步但什么是语法获取数据出来的异步地

 使用System.Windows; 
使用TestConsume2343.ServiceReference1;
使用系统;使用System.Net
;

命名空间TestConsume2343
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent() ;

GlobalWeatherSoapClient客户端=新GlobalWeatherSoapClient();

//同步
串getWeatherResult = client.GetWeather(柏林,德国);
Console.WriteLine(获取天气结果:+ getWeatherResult); //工程

//异步
client.BeginGetWeather(柏林,德国,新的AsyncCallback(GotWeather),NULL);
}

无效GotWeather(IAsyncResult的结果)
{
//Console.WriteLine(\"Get天气结果:+结果???)。
}

}
}



答案:



感谢TLiebe,与您的 EndGetWeather 建议我能得到像这样的工作:

 使用System.Windows; 
使用TestConsume2343.ServiceReference1;
使用系统;

命名空间TestConsume2343
{
公共部分类窗口1:窗口
{
GlobalWeatherSoapClient客户端=新GlobalWeatherSoapClient();

公共窗口1()
{
的InitializeComponent();
client.BeginGetWeather(柏林,德国,新的AsyncCallback(GotWeather),NULL);
}

无效GotWeather(IAsyncResult的结果)
{
Console.WriteLine(获取天气结果:+ client.EndGetWeather(结果)的ToString()) ;
}

}
}


解决方案

在你的GotWeather()方法,你需要调用EndGetWeather()方法。看看一些示例代码在 MSDN 。您需要使用IAsyncResult对象,让您的委托方法,这样就可以调用EndGetWeather()方法。


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

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

and can get the data out synchronously but 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 EndGetWeather suggestion I was able to get it to work like this:

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()); 
        }

    }
}

解决方案

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.

这篇关于我怎么能异步调用此WebService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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