2013-02-17 36 views
2

呼吁在我的Silverlight用户控件我正在听从应用程序事件,并调用WCF服务做一些动作排队WCF Silverlight的

void SelectedCustomerEvent(string customer) 
{ 
//....... 

_wcfserviceagent.GetCustomer(customer, callback); 
} 

    void callback(ObservableCollection<CustomerType> customer) 
{ 

//do some action 

} 

在某些情况下做某些动作时会触发该事件不止一次。问题在于回调不一定是按照对WCF服务的调用次序调用的。

无论如何要确保呼叫和回调总是按顺序调用?

理想情况下,我希望以这样一种方式执行,即对于将调用服务和回调的事件,并且任何其他调用进入之间都将排队。当然,我不能阻止UI线程。

回答

1

确保调用WCF服务的顺序的唯一方法是在客户端实现自己的队列。

例如:

Queue<string> _customersQueue = new Queue<string>(); 
bool _fetching; 
void SelectedCustomerEvent(string customer) 
{ 
    _customersQueue.Enqueue(customer); 
    //....... 
    if (!_fetching) 
    { 
     DoFetchCustomer(_customersQueue.Dequeue()); 
    } 
} 

void DoFetchCustomer(string customer) 
{ 
    _fetching = true; 
    _wcfserviceagent.GetCustomer(customer, callback); 
} 

void callback(ObservableCollection<CustomerType> customer) 
{ 
    _fetching = false; 
    //do some action 
    if (_customersQueue.Count > 0) 
    { 
     DoFetchCustomer(_customersQueue.Dequeue()); 
    } 
}