1
Answer

WCF Service Connection Failure In Xamarin Android

Photo of Rupesh Kahane

Rupesh Kahane

8y
457
1
I have created an WCF Service and hosted it on Server.
This application having simple functionality like Get record from a SQL Server database.
Now I am executing this WCF Service on button click event in Xamarin Android of MainActivity.cs
But this is not working. Give me some solution. If I run the same service code in Normal C# application then it is working fine.
Bellow is my code.
  1. public class MainActivity : Activity  
  2.     {  
  3.         TextView myLabel;  
  4.         EditText txtUserName;  
  5.         Button myButton;  
  6.         HttpClient Client;  
  7.         private string URIString;  
  8.   
  9.         protected override void OnCreate(Bundle bundle)  
  10.         {  
  11.             Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();  
  12.             base.OnCreate(bundle);  
  13.             SetContentView(Resource.Layout.Main);  
  14.             myLabel = FindViewById<TextView>(Resource.Id.myLabel);  
  15.             txtUserName = FindViewById<EditText>(Resource.Id.txtUserName);  
  16.             myButton = FindViewById<Button>(Resource.Id.myButton);  
  17.             myButton.Click += myButton_Click;  
  18.         }  
  19.   
  20.         void myButton_Click(object sender, EventArgs e)  
  21.         {  
  22.             var textview = FindViewById<TextView>(Resource.Id.textView3);            
  23.             var s = GetData();  
  24.             var finalResult = s;  
  25.             textview.Text = finalResult.ToString();  
  26.         }  
  27.         public string GetData()  
  28.         {  
  29.             try  
  30.             {  
  31.                 var request = HttpWebRequest.Create("http://xxxxx:2498/MyRestService.svc/GetEmployee/1");  
  32.                 request.ContentType = "application/json";  
  33.                 request.Method = "GET";  
  34.                 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
  35.                 {  
  36.                     using (StreamReader reader = new StreamReader(response.GetResponseStream()))  
  37.                     {  
  38.                         var content = reader.ReadToEnd();  
  39.                         var cleanValue = System.Text.RegularExpressions.Regex.Replace(content, @"[^a-zA-Z0-9]""");  
  40.                         return cleanValue.ToString();  
  41.                     }  
  42.                 }  
  43.             }  
  44.             catch (Exception ex)  
  45.             {  
  46.                 return ex.Message.ToString();  
  47.             }  
  48.             var t = "error occured !!!";  
  49.             return Convert.ToString(t);  
  50.         }  
  51.   }  
 Getting error : Connection failure (Access denied)

Give me some solution.
Thanks

Answers (1)

0
Photo of Rupesh Kahane
NA 10.6k 866.8k 8y

I am new in Xamarin Development.

Very Important => if you are using any Web Service for your App then do not forget to give permissions to access Internet.

Steps:
1. Right-click on your android project and select Properties.
2. Select Android Manifest in the window that opens.
3. Check the permission that you want to require in the list of permissions i.e check the checkbox of "Internet"