以xamarin形式使用asmx网络服务

时间:2020-07-16 15:27:18

标签: web-services xamarin.forms asmx

我正在Xamarin形式的登录页面上工作,我需要使用asmx Web服务才能连接到sql服务器。我使用了以下示例:https://github.com/fabiosilvalima/FSL.ConsummingAsmxServicesInXamarinForms,并尝试对我的应用程序执行相同的步骤。但是我出错了。

这是我的代码: ILogin.cs:

namespace App33.Models
{
    public interface ILogin
    {
         string Error { get; set; }
         bool ValidUser { get; set; }
    }
}

ILoginSoapService.cs

  public interface ILoginSoapService
    {
        Task<List<ILogin>> Login(string namee, string passs);
    }

在App.xaml.cs中

 private static ILoginSoapService _loginSoapService;

        public static ILoginSoapService LoginSoapService

        {

            get

            {

                if (_loginSoapService == null)

                {

                    _loginSoapService = DependencyService.Get<ILoginSoapService>();

                }



                return _loginSoapService;

            }

Main.xaml.cs

 public MainPage()
        {
            InitializeComponent();
        }
        async void OnButtonClicked(object sender, EventArgs e)
        {
            var entr_usrname = this.FindByName<Entry>("username");
            string usrname = entr_usrname.Text;
            var entr_pass = this.FindByName<Entry>("Password");
            string pass = entr_pass.Text;
            var state = await App.LoginSoapService.Login(usrname,pass);
            if (state[0].ValidUser == true)
            {
                await DisplayAlert("Alert", "You have been alerted", "OK");
            }

        }

这是针对便携式应用程序的。我的Web服务已作为LoginWs添加到Web参考中。它具有以下代码:

Result.cs:

public class Result
    {
        public string Error { get; set; }
        public bool ValidUser { get; set; }
    }

WebService1.asmx.cs:

[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 users 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;
        }

    }
}

现在在App.Android中:

Result.cs

namespace App33.Droid.LoginWs
{
    public partial class Result : ILogin

    {

    }
}

LoginSoapService.cs

[assembly: Dependency(typeof(App33.Droid.LoginSoapService))]
namespace App33.Droid
{
    public sealed class LoginSoapService :ILoginSoapService
    {
        

        LoginWs.WebService1 service;



        public LoginSoapService()

        {

            service = new LoginWs.WebService1()

            {

               // Url = "http://codefinal.com/FSL.ConsummingAsmxServicesInXamarinForms/Customers.asmx" //remote server

                 Url = "http://192.168.0.106/site2/WebService1.asmx" //localserver - mobile does not understand "localhost", just that ip address

            };

        }



        public async Task<List<ILogin>> Login( string namee,string pass)

        {

            return await Task.Run(() =>

            {

                var result = service.Login(namee,pass);

               

                return new List<ILogin>(result);

            });

        }

    }
}

我得到的错误是在这一行:返回新列表(结果);它说:错误CS1503参数1:无法从'App33.Droid.LoginWs.Result'转换为'int'。我不知道是什么问题。很抱歉,这个问题很长。任何帮助表示赞赏。

0 个答案:

没有答案
相关问题