Reputation: 377
I am creating a simple login android application via Xamarin. I made an asmx web service with a login method that checks if a user exists in the database found in SQL Server and hosted it on IIS server. I tried accessing it through my phone's browser and everything went okay. then I created my app and added the web service as a web reference. but when I deployed my android app on my phone this exception occurred.
I enabled the Internet property for my application.
the details of the exception are as follows:
{System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. ---> System.Net.WebException: Error: ConnectFailure (No route to host) ---> System.Net.Sockets.SocketException: No route to host at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00014] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net.Sockets/SocketAsyncResult.cs:127 at System.Net.Sockets.Socket.EndConnect (System.IAsyncResult asyncResult) [0x0002c] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net.Sockets/Socket.cs:1211 at System.Net.Sockets.SocketTaskExtensions+<>c.b__2_1 (System.IAsyncResult asyncResult) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net.Sockets/SocketTaskExtensions.cs:36 at System.Threading.Tasks.TaskFactory
1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func
2[T,TResult] endFunction, System.Action1[T] endAction, System.Threading.Tasks.Task
1[TResult] promise, System.Boolean requiresSynchronization) [0x00019] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs:538--- End of stack trace from previous location where exception was thrown ---
at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x00151] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebConnection.cs:169 --- End of inner exception stack trace --- at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x00217] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebConnection.cs:188 at System.Net.WebConnection.InitConnection (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x000cc] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebConnection.cs:259 at System.Net.WebOperation.Run () [0x00052] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebOperation.cs:268 at System.Net.WebCompletionSource
1[T].WaitForCompletion () [0x0008e] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebCompletionSource.cs:111 at System.Net.HttpWebRequest.RunWithTimeoutWorker[T] (System.Threading.Tasks.Task
1[TResult] workerTask, System.Int32 timeout, System.Action abort, System.Func`1[TResult] aborted, System.Threading.CancellationTokenSource cts) [0x000e8] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/HttpWebRequest.cs:956 at System.Net.HttpWebRequest.EndGetRequestStream (System.IAsyncResult asyncResult) [0x00019] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/HttpWebRequest.cs:901 at System.Web.Services.Protocols.SoapHttpClientProtocol.AsyncGetRequestStreamDone (System.IAsyncResult ar) [0x0000c] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System.Web.Services/System.Web.Services.Protocols/SoapHttpClientProtocol.cs:118 --- End of inner exception stack trace --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary () [0x00008] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/referencesource/System/compmod/system/componentmodel/AsyncCompletedEventArgs.cs:60 at newapplogin.selling.LoginCompletedEventArgs.get_Result () [0x00001] in C:\Users\User\source\repos\Solution1\newapplogin\Web References\selling\Reference.cs:178 at newapplogin.MainActivity.Ws_LoginCompleted (System.Object sender, newapplogin.selling.LoginCompletedEventArgs e) [0x00001] in C:\Users\User\source\repos\Solution1\newapplogin\MainActivity.cs:62
at newapplogin.selling.WebService1.OnLoginOperationCompleted (System.Object arg) [0x00016] in C:\Users\User\source\repos\Solution1\newapplogin\Web References\selling\Reference.cs:102 at Android.App.SyncContext+<>c__DisplayClass3_0.b__0 () [0x00000] in <11f101b564894ca7af6c482ddc51c698>:0 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <11f101b564894ca7af6c482ddc51c698>:0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <11f101b564894ca7af6c482ddc51c698>:0 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.15(intptr,intptr)} System.Reflection.TargetInvocationException
this is my android mainactivity code:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using System;
using Xamarin.Essentials;
using Xamarin.Android;
using Android.Content;
namespace newapplogin
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
// Get our button from the layout resource,
// and attach an event to it
Button bulogin = FindViewById<Button>(Resource.Id.bulogin);
Button buRegister = FindViewById<Button>(Resource.Id.buRegister);
//buRegister.Click += BuRegister_Click;
bulogin.Click += Bulogin_Click;
}
private void Bulogin_Click(object sender, EventArgs e)
{
var etUsername = FindViewById<EditText>(Resource.Id.etUsername);
var etPassword = FindViewById<EditText>(Resource.Id.etPassword);
selling.WebService1 ws = new selling.WebService1();
ws.LoginAsync(etUsername.Text, etPassword.Text);
ws.LoginCompleted += Ws_LoginCompleted;
}
private void Ws_LoginCompleted(object sender, selling.LoginCompletedEventArgs e)
{
if (e.Result.ValidUser)
{
Toast.MakeText(getApplicationContext(), "successful", 0).Show();
}
else
{
Toast.MakeText(getApplicationContext(), "failed", 0).Show();
}
}
private Context getApplicationContext()
{
throw new NotImplementedException();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
and this is my webservice code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
namespace WebApplication1
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public Result Login(string userName, string userPass)
{
SqlConnection conn=new SqlConnection (new DBConnection().ConnectionString);
Result result = new Result();
try
{
SqlCommand cmd = new SqlCommand("SELECT username, password FROM people where CONVERT(VARCHAR, username)=@username and CONVERT(VARCHAR, password)=@password");
cmd.Parameters.AddWithValue("username", userName);
cmd.Parameters.AddWithValue("password", userPass);
cmd.Connection = conn;
if (conn.State==System.Data.ConnectionState.Closed)
{
conn.Open();
}
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
result.ValidUser = true;
return result;
}
else
{
result.ValidUser = false;
}
}
catch(Exception ex)
{
result.Error = ex.ToString();
}
finally
{
conn.Close();
}
return result;
}
}
}
What could be the reason?
Upvotes: 0
Views: 1116
Reputation: 377
the problem was with my ip. it was changing because it was set to be dynamic. so whenever my app tries to access the webservice, it doesn't find it. so I just set my laptop's ip to be static and problem was solved. thanks for your help
Upvotes: 1