2017-04-27 38 views
0

我在xamarin中是全新的。xamarin PCL消耗WCF:Android中未處理的異常

我使用Visual Studio 2017社區,我編寫了一個非常簡單的代碼:我想連接到基本的WCF服務。

的紐帶,以服務:

http://services.adserviceitalia.it/Service1.svc

(方法GetHello,輸入字符串,返回字符串...非常基本的方法)

我讀了很多樣品的...我添加對服務的引用,生成代理類...好!

public partial class MainPage : ContentPage 
{ 

    private wcfs.Service1Client ws; 


    public MainPage() 
    { 
     InitializeComponent(); 


     var endpoint = new EndpointAddress("http://services.adserviceitalia.it/Service1.svc"); 
     var binding = new BasicHttpBinding 
     { 
      Name = "basicHttpBinding", 
      MaxBufferSize = 2147483647, 
      MaxReceivedMessageSize = 2147483647 
     }; 
     TimeSpan timeout = new TimeSpan(0, 0, 30); 
     binding.SendTimeout = timeout; 
     binding.OpenTimeout = timeout; 
    binding.ReceiveTimeout = timeout; 

     ws = new wcfs.Service1Client(binding,endpoint); 
     ws.GetHelloCompleted += Handle_HelloWorldCompleted; 

    } 

    private void Button_Clicked(object sender, EventArgs e) 
    { 

     ws.GetHelloAsync("Mark"); 

     } 


    private void Handle_HelloWorldCompleted(object sender, wcfs.GetHelloCompletedEventArgs args) 
    { 

     label1.Text = args.Result; 
    } 

    } 
} 

它工作在UWP調試...

Connection with WCF... Hello World working in UWP

我在Android中未處理的異常...

在Android清單所有權限都cheched。 Android模擬器連接到互聯網...

每一個幫助將非常感謝!在此先感謝......並請原諒我的英語。

+0

Android中發生了什麼異常? – Jason

+0

我不知道...: - ((( 只彈出一個窗口:「未處理的異常出現」 沒有裁判 – treep

+0

命中打破 - 然後使用調試器來看看異常和堆棧跟蹤 – Jason

回答

0

Ops ...

第一:謝謝大家!

在我的日誌中,我讀到: 04-27 14:11:30.106 E/mono-rt(5308):[ERROR] FATAL UNHANDLED EXCEPTION:Android.Util.AndroidRuntimeException:只有創建視圖的原始線程層級可以觸及其觀點。

看來,問題是,我試圖直接與WCF respose更新界面Handle_HelloWorldCompleted:

label1.Text = args.Result; 

...在我這樣修改代碼:

//label1.Text = args.Result; 
System.Diagnostics.Debug.WriteLine(args.Result); 

我可以請參閱arg.Result由WCF響應「Hola Mark」填充(ergo:與服務的連接正常工作!!)。

注意我在Android調試中只有這種行爲。

如何更新該標籤1?

+0

使用Activity.RunOnUiThread – Jason

+0

謝謝Jason。 RunOnUiThread。非常不同?? – treep

+0

RunOnUIThread僅適用於Android – Jason