在我的 Silverlight 用户控件中,我正在监听来自应用程序的事件并调用 WCF 服务来执行某些操作
void SelectedCustomerEvent(string customer)
{
//.......
_wcfserviceagent.GetCustomer(customer, callback);
}
void callback(ObservableCollection<CustomerType> customer)
{
//do some action
}
在某些情况下,在执行某些操作时会多次触发事件。问题是回调不一定按照调用 WCF 服务的顺序调用。
有没有办法确保调用和回调总是按顺序调用?
理想情况下,我希望以这样一种方式执行:对于一个事件,它会调用服务和回调,而介于两者之间的任何其他调用都会排队。当然,我不能阻塞 UI 线程。
最佳答案
确保 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());
}
}
关于c# - 排队来自 silverlight 的 WCF 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14920575/