简体   繁体   English

WCF已完成事件被多次调用

[英]WCF Completed Event is getting called multiple times

I am making an asynchronous call to wcf service methods and generated a Completed event on Button click: 我正在asynchronous调用wcf服务方法,并在Button单击时生成了Completed事件:

private void OnSearchProductClick(object sender, RoutedEventArgs e)
{
    service.GetProductsCompleted += new EventHandler<GetProductsCompletedEventArgs>(webService_GetProductsCompleted);
    ProductType productType = (ProductType)cboProductType.SelectedItem;
    _productTypeID = productType.ProductTypeID;
    service.GetProductsAsync(txtName.Text, txtCode.Text, _productTypeID);
}

Problem is, the webService_GetProductsCompleted event gets called multiple times. 问题是, webService_GetProductsCompleted事件被多次调用。 When click the Button for first time it gets called once, when I click second time gets called twice when click third time gets called thrice and so on. 第一次click按钮时,它会被调用一次,当我单击第二次按钮时,会被调用两次,而第三次单击按钮时会被调用三次,依此类推。 This is a very unusual behavior. 这是非常不寻常的行为。 Why is it happening and how can I resolve it? 为什么会发生,我该如何解决?

Here is the webService_GetProductsCompleted event: 这是webService_GetProductsCompleted事件:

public void webService_GetProductsCompleted(object sender, CatalogueServiceReference.GetProductsCompletedEventArgs e)
{
    if (e.Result.Count != 0)
    {
        PagedCollectionView pagingCollection = new PagedCollectionView(e.Result);
        pgrProductGrids.Source = pagingCollection;
        grdProductGrid.ItemsSource = pagingCollection;
        pgrProductGrids.Visibility = Visibility.Visible;
    }
}

The problem is this line: 问题是这一行:

service.GetProductsCompleted += new EventHandler<GetProductsCompletedEventArgs>(webService_GetProductsCompleted);

You should call it from the form Load event, not here. 您应该从表单Load事件而不是此处调用它。 Because every time you call these methods (OnSearchProductClick) you add the same handler again so it gets executed multiple times. 因为每次调用这些方法(OnSearchProductClick)时,您都会再次添加相同的处理程序,因此它将多次执行。

Other option is un-register it first and then register it again. 另一种选择是先取消注册,然后再次注册。

problem is here. 问题在这里。

    private void OnSearchProductClick(object sender, RoutedEventArgs e)
    {
        service.GetProductsCompleted += new EventHandler<GetProductsCompletedEventArgs>(webService_GetProductsCompleted);
    }

By doing this, you are subscribing event in click event, thus on every click you are making new subscription. 这样,您就可以在点击事件中订阅事件,因此,每次点击都会产生新的订阅。

Instead of this, you should be subscribing service's event once (before this click event, usually in load event of form or in constructor as per your convince). 取而代之的是,您应该订阅一次服务的事件(在此click事件之前,通常是在窗体的load事件中,或者在您确信的情况下在构造函数中)。

But one thing you should keep in mind, this "service" object should be the same while subscribing event and calling API. 但是您应该记住一件事,订阅事件并调用API时,此“服务”对象应该相同。

following link may clear this idea https://msdn.microsoft.com/en-us/library/ms366768.aspx 以下链接可能会清除此想法https://msdn.microsoft.com/en-us/library/ms366768.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM