通过 1. events / 2. IAsyncResult 使用 Web 服务有何优缺点?

发布于 2024-08-10 09:45:07 字数 1706 浏览 3 评论 0原文

我制作了一个使用 web 服务 的 WPF 示例 (www.webservicex.com/ globalweather.asmx)以两种不同的方式:

使用像这样的事件

public Window1()
{
    InitializeComponent();
    DataContext = this;

    Location = "loading...";
    Temperature = "loading...";
    RelativeHumidity = "loading...";

    client.GetWeatherCompleted += 
            new EventHandler<GetWeatherCompletedEventArgs>(client_GetWeatherCompleted);
    client.GetWeatherAsync("Berlin", "Germany");
}

void client_GetWeatherCompleted(object sender, GetWeatherCompletedEventArgs e)
{
    XDocument xdoc = XDocument.Parse(e.Result);

    Location = xdoc.Descendants("Location").Single().Value;
    Temperature = xdoc.Descendants("Temperature").Single().Value;
    RelativeHumidity = xdoc.Descendants("RelativeHumidity").Single().Value;
}

以及使用像这样的Begin/End方法和IAsyncResult

public Window1()
{
    InitializeComponent();
    DataContext = this;

    Location = "loading...";
    Temperature = "loading...";
    RelativeHumidity = "loading...";

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

void GotWeather(IAsyncResult result)
{
    string xml = client.EndGetWeather(result).ToString();
    XDocument xdoc = XDocument.Parse(xml);

    Location = xdoc.Descendants("Location").Single().Value;
    Temperature = xdoc.Descendants("Temperature").Single().Value;
    RelativeHumidity = xdoc.Descendants("RelativeHumidity").Single().Value;

}

这两种方法似乎执行完全相同的任务。

它们的优点和缺点是什么?您什么时候会使用其中一种而不使用另一种?

I made a WPF example that consumes a web service (www.webservicex.com/globalweather.asmx) in two different ways:

with events like this:

public Window1()
{
    InitializeComponent();
    DataContext = this;

    Location = "loading...";
    Temperature = "loading...";
    RelativeHumidity = "loading...";

    client.GetWeatherCompleted += 
            new EventHandler<GetWeatherCompletedEventArgs>(client_GetWeatherCompleted);
    client.GetWeatherAsync("Berlin", "Germany");
}

void client_GetWeatherCompleted(object sender, GetWeatherCompletedEventArgs e)
{
    XDocument xdoc = XDocument.Parse(e.Result);

    Location = xdoc.Descendants("Location").Single().Value;
    Temperature = xdoc.Descendants("Temperature").Single().Value;
    RelativeHumidity = xdoc.Descendants("RelativeHumidity").Single().Value;
}

and with the Begin/End methods and IAsyncResult like this:

public Window1()
{
    InitializeComponent();
    DataContext = this;

    Location = "loading...";
    Temperature = "loading...";
    RelativeHumidity = "loading...";

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

void GotWeather(IAsyncResult result)
{
    string xml = client.EndGetWeather(result).ToString();
    XDocument xdoc = XDocument.Parse(xml);

    Location = xdoc.Descendants("Location").Single().Value;
    Temperature = xdoc.Descendants("Temperature").Single().Value;
    RelativeHumidity = xdoc.Descendants("RelativeHumidity").Single().Value;

}

These two approaches seem to perform the exact same task.

What are their advantages and disadvantages? When would you use one and not the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-08-17 09:45:07

对于远程服务的情况,我通常更喜欢使用回调而不是事件处理程序,因为它会产生更多可读/可维护的代码(只需查看服务调用调用代码,我就知道调用完成时将执行哪些代码)。此外,在使用事件处理程序时,您需要注意不要多次声明它们。

For the case of remote services, I usually prefer to use callbacks instead of event handlers since it leads to more readable/maintainable code (by just looking at the service call invokation code I know which code will be executed when the call finishes). Moreover when using event handlers you need to have care for not declaring them more than once.

薯片软お妹 2024-08-17 09:45:07

这只是品味问题。从技术角度看没有什么区别。

That's just matter of taste. No difference from technical prospective.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文