我正在通过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]在 /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.IAsyncResult asyncResult)[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.IAsyncResult asyncResult)[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 \ Web References \ 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 \ Web References \ selling \ Reference.cs:102,网址为 Android.App.SyncContext + c__DisplayClass3_0.b__0()[0x00000] 在:0中 Java.Lang.Thread + RunnableImplementor.Run()[0x00008]在 :0在 Java.Lang.IRunnableInvoker.n_Run(System.IntPtr jnienv,System.IntPtr native_this)[0x00009]在: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,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;
}
}
}
可能是什么原因?