2014-09-23 80 views
1

我正在開發一個使用第三方.asmx Web服務的應用程序。我在我的應用程序中使用PCL(便攜式類庫)。在pcl中使用asmx webservice

所以我想在我的應用程序中使用這些.asmx網絡服務。問題是PCL不支持傳統的Web服務,即.asmx。它支持WCF網絡服務。

我看了很多文章,他們建議我從wsdlWCF web服務。但由於所有Web服務都是第三方,因此我需要在客戶端應用程序(在調用Web服務的位置)編寫代理,以便將WCF調用轉換爲.asmx

此外,我已經嘗試使用PCL這個例子。 我使用this ASMX Web服務

public class PerformLogIn : ILogInService 
    { 
     public string LogIn(string code) 
     { 
      ServiceReference1.WeatherSoapClient obj = new ServiceReference1.WeatherSoapClient(); 
      obj.GetCityForecastByZIPAsync(code); 
      ServiceReference1.WeatherReturn get = new ServiceReference1.WeatherReturn(); 

      return (get.Temperature); 
     } 

但我沒有收到任何結果。 那麼有人有想法該怎麼做?

+0

您不必做任何轉換。就像你通常那樣使用「添加服務參考」。 – 2014-09-24 05:40:55

+0

@JohnSaunders:Thnx的回覆..我做到了......這是給我'asych'方法。其返回類型是「空白」。那麼如何得到服務的迴應? – Rohit 2014-09-24 06:37:06

回答

1

尤里卡我發現它..

使用下面的代碼片段

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

      obj.GetCityForecastByZIPAsync(code); 
      obj.GetCityForecastByZIPCompleted+=getResult; 
     } 
     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; 
     } 
    } 

所以從Web服務你的迴應被存儲在result變量。只需獲取所需的數據並將其返回給調用客戶端即可。

+0

Hi Rohit。我試圖實現類似的東西,但我有一個問題。在void函數調用和完成的事件觸發之間,存在空的異常錯誤。你可以在這裏看到更多細節,https://stackoverflow.com/questions/38708785/using-asmx-in-portable-library。有什麼建議? – 2016-08-02 16:16:56