-2

我尝试在 Visual Studio 2010 中创建演示 WCF Web 服务;我已经成功添加了 thr 服务引用,但是客户端仍然有问题。如果我尝试调用服务中的函数,则会收到错误消息。

这是我的代码:

namespace BankAccount
{   
    [ServiceContract]
    public interface IUserServices
    {
        [OperationContract]
        bool addNewUser(User user);
    }
}

namespace BankAccount
{   
    public class UserServices : IUserServices
    {
        public bool addNewUser(User user)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            try
            {
                String sql = "insert into user(username,password,isActive) values(@username,@password,@isActive)";
                conn = Connection.getConnection();
                conn.Open();
                cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(new SqlParameter("@username", user.getUsername()));
                cmd.Parameters.Add(new SqlParameter("@password", user.getPassword()));
                cmd.Parameters.Add(new SqlParameter("@password", user.getActive()));
            }
            catch (SqlException ex)
            {
                return false;
                throw ex;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
            return true;
        }
    }
}

//用户对象

public class User
    {
        private int Id;
        private String username;
        private String password;
        private Boolean isActive;

        public User()
        {

        }

        public int getId()
        {
            return this.Id;
        }

        public void setId(int id)
        {
            this.Id = id;
        }

        public String getUsername()
        {
            return this.username;
        }

        public void setUsername(String username)
        {
            this.username = username;
        }

        public String getPassword()
        {
            return this.password;
        }

        public void setPassword(String password)
        {
            this.password = password;
        }

        public Boolean getActive()
        {
            return this.isActive;
        }

        public void setActive(Boolean isActive)
        {
            this.isActive = isActive;
        }
    }

客户网站

using System.Web.UI.WebControls;
using BankClient.ServicesForUser;

namespace BankClient.userclient
{
    public partial class usersaving : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            UserServicesClient client = new UserServicesClient();
            //User on client project
            User user = new User();
            user.setUsername(txtusername.Text);
            user.setPassword(txtpassword.Text);
            user.setActive(true);

            bool result = client.addNewUser(user);
        }
    }
}

我在以下行收到错误:

client.addNewUser(user)

错误:

the best overloaded method match for 'Bank.Client.ServicesName.UserServicesClient.addNewUser(Bank.Client.ServicesName.User) has some invalid arguments

有谁知道问题可能是什么?

4

1 回答 1

3

创建服务后,您更改了用户对象。在客户端上重新生成服务合同。

您似乎使用了错误的用户对象:

        Bank.Client.ServicesName.User user = new Bank.Client.ServicesName.User();
        user.Username = txtusername.Text;
        user.Password = txtpassword.Text;
        user.Active = true;

        bool result = client.addNewUser(user);
于 2013-10-28T11:40:46.377 回答