【问题标题】:I'm getting a error while consuming the WCF Webservice in my Xamarin forms(cross platform)在我的 Xamarin 表单(跨平台)中使用 WCF Web 服务时出现错误
【发布时间】:2020-08-15 14:14:54
【问题描述】:

以下是我遇到的错误

严重性代码描述项目文件行抑制状态

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

调用 Web 服务时选择的数据类型对于 Collection 类型是 "System.Collections.Generic.List",对于 Dictionary 集合类型,默认情况下是 "System.Collections.Generic.Dictionary"

下面是创建的 WCF 网络服务

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

我是移动开发的新手,我正在尝试将远程数据库连接到 Xamarin 表单以使用数据库凭据进行用户登录,请帮助我

请帮帮我 提前致谢

【问题讨论】:

  • 如果您希望我们帮助您,您必须发布相关代码
  • 请大家帮忙

标签: c# wcf xamarin.forms


【解决方案1】:

您正在调用 async 方法而不使用 await

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

应该是

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

您还需要将async 添加到您的方法签名中

private async void BtnLogin_Clicked(object sender, EventArgs e)

【讨论】:

  • 更改后出现以下错误未处理的异常:System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] :反序列化操作“LoginUserDetails”的请求消息正文时出错。 OperationFormatter 遇到无效的消息正文。预计会找到名称为“LoginUserDetails”和命名空间“tempuri.org”的节点类型“元素”。找到名称为“LoginUserDetailsAsync”和命名空间“tempuri.org”的节点类型“元素”
  • 不要使用 WCF,使用 RESTful API
  • 我不懂 MVC,所以在 Web API 上遇到困难,请帮助我
猜你喜欢
  • 2014-11-03
  • 2018-01-15
  • 2023-03-14
  • 2020-08-12
  • 2019-01-03
  • 2015-06-20
  • 2015-11-11
  • 1970-01-01
相关资源
最近更新 更多