【发布时间】:2009-11-03 14:28:25
【问题描述】:
在 Visual Studio 中,我在此 URL 上创建了一个 Web 服务(并选中了“生成异步操作”):
并且可以同步获取数据,但是异步获取数据的语法是什么?
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.???);
}
}
}
答案:
感谢 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());
}
}
}
【问题讨论】:
-
错误是什么?什么都没打印?如果代码被注释掉就不会了。
-
好吧,如果我只输出“结果”,它会打印:获取天气结果:System.ServiceModel.Channels.ServiceChannel+SendAsyncResult,我不知道数据在“结果”对象中的位置,在本例中,我想像使用“e.Result”一样访问数据:tanguay.info/web/index.php?pg=codeExamples&id=205
标签: c# web-services asynchronous