简体   繁体   English

从异步方法返回值

[英]Return value from async Method

I have asmx webservice and I am using in it in PCL, so all of us know that now PCL no longer supports asmx webservice, it supports wcf webservices. 我有asmx webservice,并且在PCL中使用它,所以我们所有人都知道现在PCL不再支持asmx webservice,它支持wcf webservices。 I somehow manage to call asmx webservice through PCL by using silverlight plugin. 我通过使用silverlight插件设法通过PCL调用asmx Web服务。 But it creates async method and converts WSDL to wcf. 但是它创建了一个异步方法,并将WSDL转换为wcf。

Now problem is I need to return some values from webmethods which are now async method. 现在的问题是,我需要从现在是异步方法的web方法中返回一些值。 If anybody know solution for this please help me. 如果有人知道解决方案,请帮助我。

I am using following code, 我正在使用以下代码,

public class PerformLogIn
{
     string temp;
     public string checkTemp(string code)
     {
         ServiceReference1.WeatherSoapClient obj = new ServiceReference1.WeatherSoapClient(
                                    new BasicHttpBinding(),
                                    new EndpointAddress("http://wsf.cdyne.com/WeatherWS/Weather.asmx"));

         obj.GetCityForecastByZIPCompleted+=getResult;
         obj.GetCityForecastByZIPAsync(code);

         return temp;
     }
     void getResult(Object sender,GetCityForecastByZIPCompletedEventArgs e)
     {
         string error = null;

         if (e.Error != null)
             error = e.Error.Message;
         else if (e.Cancelled)
             error = "cancelled";
         var result = e.Result; 
         temp=result.temprature;
     }

} }

So when I run it, value of temp is null. 因此,当我运行它时, temp值为null。 When I debugged it, I found that when getResult occures, it enters into void getResult by that time it has reached to return statement. 当我调试它时,我发现当发生getResult时,到达return语句的时间它进入了void getResult

The problem here is, when GetCityForecastByZIPAsync method which is async gets called, it doesn't wait for others' operation to complete, it just finishes its execution and returns. 这里的问题是,当GetCityForecastByZIPAsync async GetCityForecastByZIPAsync方法时,它不等待其他人的操作完成,而只是完成执行并返回。 Now being having a webservice which gets invoked only on occurrence of event getResult (in this case), invocation of event getResult (in this case) is based on network, server response which will have a little delay that we can't control. 现在是具有webservice它获取只在事件发生时调用getResult (在这种情况下),事件的调用getResult (在这种情况下)是基于网络的,这将有一个小的延迟是我们无法控制的服务器响应。 So till that time GetCityForecastByZIPAsync method finishes its execution. 因此,直到那个时候, GetCityForecastByZIPAsync方法才完成其执行。 And after some time when getResult event occurs, it executes but does not return to the calling method viz. 并且在一段时间后发生getResult事件时,它会执行,但不会返回到调用方法viz。 checkTemp method. checkTemp方法。

So how do I get data from getResult event and put it back to checkTemp method so that I can use it for my purpose. 因此,如何从getResult事件获取数据并将其放回checkTemp方法,以便可以将其用于我的目的。

If anybody knows, please help me out. 如果有人知道,请帮助我。

FYI: the entire first paragraph of your question is pretty much gobbledygook to most of your audience, and does not seem relevant to the question (or at least, would not be relevant if you'd provided the actual important details in a more broadly-comprehensible way). 仅供参考:您的问题的整个第一段对您的大多数受众来说都是很愚蠢的,而且似乎与该问题无关(或者至少,如果您在更广泛的范围内提供了实际的重要细节,则该问题将不相关,可理解的方式)。

As far as the question goes… 就问题而言……

The two most obvious problems with your code are that a) you are subscribing your getResult event handler after you initiate the asynchronous operation, which means the operation could complete before you are set up to be notified of the completion, and b) your checkTemp method doesn't wait for the completion before it returns the result. 代码的两个最明显的问题是:a) 启动异步操作之后,您正在订阅getResult事件处理程序,这意味着该操作可以在设置完成之前通知完成,并且b)您的checkTemp方法在返回结果之前不等待完成。

The first issue is easy to fix. 第一个问题很容易解决。 Just reverse the order of the two statements, so that they look like this: 只需颠倒这两个语句的顺序,就可以使它们看起来像这样:

     obj.GetCityForecastByZIPCompleted += getResult;
     obj.GetCityForecastByZIPAsync(code);

Fixing the second part is harder to comment on, mainly because you haven't provided enough context in your question. 修复第二部分更难评论,主要是因为您在问题中没有提供足够的上下文。 If the GetCityForecastByZIPAsync method is actually a C# async method, then the best approach is to get rid of the getResult method altogether (as well as subscribing it to the ...Completed event), and await the call to GetCityForecastByZIPAsync . 如果GetCityForecastByZIPAsync方法实际上是C# async方法,则最好的方法是完全摆脱getResult方法(以及将其订阅到...Completed事件),然后awaitGetCityForecastByZIPAsync的调用。 Otherwise, the best approach is for checkTemp to not actually return the value, but rather for that method to be void and for your getResult method to handle whatever actually needs to happen when the operation completes (eg by invoking code that displays the result in the UI). 否则,最好的方法是让checkTemp实际不返回值,而是使该方法void并让您的getResult方法处理操作完成时实际需要发生的一切(例如,通过在代码中显示结果的代码) UI)。

Without more context, it's impossible to say. 没有更多的上下文,这是不可能说的。 See https://stackoverflow.com/help/mcve for information on how to post a better question. 有关如何发布更好的问题的信息,请参见https://stackoverflow.com/help/mcve

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM