简体   繁体   English

在我的 Xamarin 表单(跨平台)中使用 WCF Web 服务时出现错误

[英]I'm getting a error while consuming the WCF Webservice in my Xamarin forms(cross platform)

Below is the error which I'm getting下面是我得到的错误

Severity Code Description Project File Line Suppression State严重性代码描述项目文件行抑制State

Error CS1061 'Task<List<string>>' does not contain a definition for 'ToList' and no accessible extension method 'ToList' accepting a first argument of type 'Task<List<string>>' could be found (are you missing a using directive or an assembly reference?) LoginForm C:\Users\test\source\repos\LoginForm\LoginForm\LoginForm\MainPage.xaml.cs 26 Active

The data type selected while calling a web service was "System.Collections.Generic.List" for Collection type and for Dictionary collection type it was by default "System.Collections.Generic.Dictionary" .调用 web 服务时选择的数据类型是集合类型的"System.Collections.Generic.List" ,而字典集合类型默认为"System.Collections.Generic.Dictionary"

Below is WCF webservice created下面是创建的 WCF webservice

public List<string> LoginUserDetails(UserDetails userInfo)
        {
            List<string> usr = new List<string>();
            SqlConnection con = new SqlConnection("Data Source=Test; Initial Catalog=crm_db; User ID=sa;  Password=****");
            con.Open();
            SqlCommand cmd = new SqlCommand("select username,password from crm_tbl_admin where username=@UserName and password=@Password", con);
            cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
            cmd.Parameters.AddWithValue("@Password", userInfo.Password);

            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read() == true)
            {
                usr.Add(dr[0].ToString());
            }
            con.Close();
            return usr;
        }
Below is the code for calling wcf in Xamarin's MainPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using ServiceReference1;
using System.ServiceModel;

namespace WCF_LoginApp
{
    public partial class MainPage : ContentPage
    {
        Service1Client obj = new Service1Client();
        private static EndpointAddress endPoint = new EndpointAddress("http://localhost/ServiceLogin11/Service1.svc");
        private static NetTcpBinding binding;
        public MainPage()
        {
            InitializeComponent();
            binding = CreateBasicHttpBinding();
        }
        private static NetTcpBinding CreateBasicHttpBinding()
        {
            NetTcpBinding binding = new NetTcpBinding
            {
                Name = "netTcpBinding",
                MaxBufferSize = 2147483647,
                MaxReceivedMessageSize = 2147483647
            };

            TimeSpan timeout = new TimeSpan(0, 0, 30);
            binding.SendTimeout = timeout;
            binding.OpenTimeout = timeout;
            binding.ReceiveTimeout = timeout;
            return binding;
        }
        private void BtnLogin_Clicked(object sender, EventArgs e)
        {
            try
            {
                UserDetails userinfo = new UserDetails();
                userinfo.UserName = usernameEntry.Text;
                userinfo.Password = passwordEntry.Text;
                List<string> msg =  obj.LoginUserDetailsAsync(userinfo).ToList();
                //var result = obj.LoginUserDetailsAsync(userinfo);
                //string msg = result.ToString();
                messageLabel.Text = "Employee Name = " + msg.ElementAt(0);

            }
            catch (Exception ex)
            {
                // messageLabel.Text = "Wrong Id Or Password";
                throw ex;
            }
        }
    }
}

I'm new to mobile development, Here I'm trying to connect the remote database to Xamarin forms for user login using database credentials, Please help me out我是移动开发的新手,在这里我正在尝试将远程数据库连接到 Xamarin forms 以使用数据库凭据进行用户登录,请帮帮我

Please help me Thanks in advance请帮助我提前谢谢

you are calling an async method without using await您正在调用async方法而不使用await

List<string> msg =  obj.LoginUserDetailsAsync(userinfo).ToList();

should be应该

var data =  await obj.LoginUserDetailsAsync(userinfo);
List<string> msg = data.ToList();

you will also need to add async to your method signature您还需要将async添加到您的方法签名中

private async void BtnLogin_Clicked(object sender, EventArgs e)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM