调用WCF服务时出错

调用WCF服务时出错,wcf,windows-phone-7,Wcf,Windows Phone 7,我已经用WCF服务构建了一个服务器:IService.cs和service.cs。文件的代码如下所示: Iservice.cs [ServiceContract] public interface ITaiKhoanNguoiChoi { [OperationContract] void InsertStaff(string username); } 服务中心 public void InserStaff(string username) {

我已经用WCF服务构建了一个服务器:IService.cs和service.cs。文件的代码如下所示:

Iservice.cs

[ServiceContract]
public interface ITaiKhoanNguoiChoi
{ 
    [OperationContract]
    void InsertStaff(string username);
 }
服务中心

public void InserStaff(string username)
        {
            try
            {
                conn = new SqlConnection(strConnect);
                conn.Open();
                cmd = new SqlCommand("insert into STAFF values('" + username+ "')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch
            {

            }
        }
好的,这就是我建立的所有服务代码。然后,我将在我的客户机上调用serveice,代码如下所示:

ServiceClient proxy = new ServiceClient();
private void btnInsert_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            proxy.InsertStaffCompleted += new EventHandler<AsyncCompletedEventArgs>(InsertStaffMethod);
            proxy.InsertStaffAsync("John");            
        }

void InsertStaffMethod(object o, AsyncCompletedEventArgs e) 
        { 
            MessageBox.Show("YAY!");
        }
ServiceClient proxy=newserviceclient();
私有void btn插入\u操纵开始(对象发送方,操纵开始目标e)
{
proxy.InsertStaffCompleted+=新事件处理程序(InsertStaffMethod);
代理人:InsertStaffAsync(“约翰”);
}
void InsertStaffMethod(对象o,AsyncCompletedEventArgs e)
{ 
MessageBox.Show(“耶!”);
}
现在,在我第一次单击BTN插入时,“耶”将显示1次

在第二次单击BTN插入时,“耶”将显示2次

在第N次单击BTN插入时,“耶”将显示N次

这是我的问题。我不明白为什么只单击1次,服务器上的InsertStaff方法将被多次调用


你能为我解释一下吗。以及如何解决这个问题。我只希望InsertStaff方法在单击BTN插入1次时只执行时间。

我已经有一段时间没有使用WebForms事件生命周期了,但我认为每次单击BTN插入时都会添加EventHandler,因此每次都会触发越来越多的事件

解决办法是不要排队

proxy.InsertStaffCompleted += new EventHandler<AsyncCompletedEventArgs>(InsertStaffMethod);
cmd = new SqlCommand("insert into STAFF values('" + username+ "')", conn);