我正在通过Xamarin创建一个简单的登录android应用程序。我使用登录方法制作了一个asmx Web服务,该方法检查SQL Server中找到的数据库中是否存在用户并将其托管在IIS服务器上。我尝试通过手机的浏览器访问它,然后一切正常。然后我创建了我的应用,并将Web服务添加为Web参考。但是当我在手机上部署Android应用程序时,发生了此异常。
我为我的应用程序启用了Internet属性。
例外的详细信息如下:
{System.Reflection.TargetInvocationException:发生异常在操作过程中,使结果无效。检查InnerException有关异常的详细信息。 ---> System.Net.WebException:错误:ConnectFailure(无路由到主机)--->System.Net.Sockets.SocketException:主机没有路由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在System.Net.Sockets.Socket.EndConnect(System.IAsyncResultasyncResult)[0x0002c]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net.Sockets/Socket.cs:1211,位于System.Net.Sockets.SocketTaskExtensions + <> c。 b__2_1(System.IAsyncResult asyncResult)[0x00000]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net.Sockets/SocketTaskExtensions.cs:36在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]答应,System.Boolean requireSynchronization)[0x00019]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs:538-从上一个引发异常的位置开始的堆栈跟踪---
位于System.Net.WebConnection.Connect(System.Net.WebOperation操作,System.Threading.CancellationToken取消令牌)[0x00151]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebConnection.cs:169---内部异常堆栈跟踪的结尾---在System.Net.WebConnection.Connect(System.Net.WebOperation操作,System.Threading.CancellationToken cancellingToken)[0x00217]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebConnection.cs:188在System.Net.WebConnection.InitConnection(System.Net.WebOperation操作,System.Threading.CancellationToken取消令牌)[0x000cc]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebConnection.cs:259在System.Net.WebOperation.Run()[0x00052]中/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/WebOperation.cs:268在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超时,System.Action中止,System.Func`1 [TResult]中止,System.Threading.CancellationTokenSource cts)[0x000e8]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/HttpWebRequest.cs:956在System.Net.HttpWebRequest.EndGetRequestStream(System.IAsyncResultasyncResult)[0x00019]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/System.Net/HttpWebRequest.cs:901在System.Web.Services.Protocols.SoapHttpClientProtocol.AsyncGetRequestStreamDone(System.IAsyncResult ar)[0x0000c]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System.Web.Services/System.Web.Services.Protocols/SoapHttpClientProtocol.cs:118-内部异常堆栈跟踪的结尾--在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在newapplogin.selling.LoginCompletedEventArgs.get_Result()[0x00001]在C:\ Users \ User \ source \ repos \ Solution1 \ newapplogin \ WebReferences \ selling \ Reference.cs:178,网址为newapplogin.MainActivity.Ws_LoginCompleted(System.Object发送者,newapplogin.selling.LoginCompletedEventArgs e)[0x00001]在C:\ Users \ User \ source \ repos \ Solution1 \ newapplogin \ MainActivity.cs:62在newapplogin.selling.WebService1.OnLoginOperationCompleted(System.Object arg)[0x00016]在C:\ Users \ User \ source \ repos \ Solution1 \ newapplogin \ WebReferences \ selling \ Reference.cs:102,网址为Android.App.SyncContext + <> c__DisplayClass3_0.b__0()[0x00000]在<11f101b564894ca7af6c482ddc51c698>:0中Java.Lang.Thread + RunnableImplementor.Run()[0x00008]在<11f101b564894ca7af6c482ddc51c698>:0在Java.Lang.IRunnableInvoker.n_Run(System.IntPtr jnienv,System.IntPtrnative_this)[0x00009]在<11f101b564894ca7af6c482ddc51c698>:0中(包装器动态方法)Android.Runtime.DynamicMethodNameCounter.15(intptr,intptr)} System.Reflection.TargetInvocationException
这是我的android mainactivity代码:
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);
}
}
}
这是我的网络服务代码:
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;
}
}
}
可能是什么原因?