我尝试在 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
有谁知道问题可能是什么?