2009-11-03 12 views
4

Dans Visual Studio j'ai créé un service Web (et vérifié "générer des opérations asynchrones") sur cette URL:Comment appeler ce service Web de manière asynchrone?

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

et peut obtenir les données sur synchrone mais ce qui est la syntaxe pour obtenir les données asynchrone?

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.???); 
     } 

    } 
} 

Réponse:

Merci TLiebe, avec votre EndGetWeather suggestion que j'ai pu le faire fonctionner comme ceci:

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

    } 
} 
+0

Quelle est l'erreur? Est-ce que rien n'a imprimé? Eh bien, il ne le fera pas si le code est commenté. – leppie

+0

bien si je viens de sortie "résultat", il imprime: Obtenir le résultat météo: System.ServiceModel.Channels.ServiceChannel + SendAsyncResult, je ne sais pas où les données sont dans l'objet "résultat", je veux accéder aux données comme Je fais avec "e.Result" dans cet exemple: http://tanguay.info/web/index.php?pg=codeExamples&id=205 –

+0

quelle version .net? –

Répondre

1

Dans votre méthode GotWeather() vous devez appeler la méthode EndGetWeather(). Jetez un oeil à certains exemples de code au MSDN. Vous devez utiliser l'objet IAsyncResult pour obtenir votre méthode de délégué afin que vous puissiez appeler la méthode EndGetWeather().

7

Je suggère d'utiliser l'événement fourni par le proxy généré automatiquement au lieu de déconner avec le 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); 
} 
Questions connexes