我尝试在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
有谁知道问题可能是什么?
答案 0 :(得分: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);