如何确保wcf服务客户端在silverlight中完成其工作?

如何确保wcf服务客户端在silverlight中完成其工作?,silverlight,asynchronous,call,wait,Silverlight,Asynchronous,Call,Wait,我使用wcf服务客户端为silverlight项目提交数据更改。相关代码如下所示: public class DispatcherCollection : UpdatableCollection<DocumentDispatcher> { public override void SubmitChanges() { DocumentServiceClient client = new DocumentServiceClient();

我使用wcf服务客户端为silverlight项目提交数据更改。相关代码如下所示:

    public class DispatcherCollection : UpdatableCollection<DocumentDispatcher>
{
    public override void SubmitChanges()
    {
        DocumentServiceClient client = new DocumentServiceClient();
        client.NewDocumentCompleted += (s, e) =>
        {
            // (s as DocumentServiceClient).CloseAsync();
            // do something
        };
        client.UpdateColumnCompleted += (s, e) =>
        {
            // (s as DocumentServiceClient).CloseAsync();
            // do something
        };
        client.RemoveDocumentCompleted += (s, e) =>
        {
            // (s as DocumentServiceClient).CloseAsync();
            // do something
        };
        foreach (DocumentDispatcher d in this)
        {
            if (d.IsNew)
            {
                // d=>object[] data
                client.NewDocumentAsync(data);
                d.IsNew=false;
            }
            else
            {
                foreach (string propertyName in d.modifiedProperties)
                {
                    client.UpdateColumnAsync(d.ID, GetPropertyValue(propertyName));
                }
                dd.ClearModifications();
            }
        }
        foreach (DocumentDispatcher dd in removedItems)
        {
            client.RemoveDocumentAsync(dd.ID);
        }
        removedItems.Clear();
    }
}
 private void Application_Exit(object sender, EventArgs e)
    {
        Document doc = EDPViewModel.CurrentViewModel.Document;
        if (doc != null) new ServiceClient().SubmitChangesAsync(doc);
    }
类UpdateableCollection派生自ObserableCollection,我在类DocumentDispatcher和UpdateableCollection中实现了逻辑,以缓冲数据的更改,例如新创建的、修改的和删除的属性。我使用SubmitChanges方法将所有更改提交到服务器

现在我被卡住了: 1.在一次bunlde-fo异步调用之后,我不知道何时关闭客户端。我不知道哪一个回调是最后一个。 2.当用户在单击save按钮后立即关闭IE时会发生什么?这似乎是因为IE运行异步,但实际上更新线程正在努力运行

您可以保留一个计数器或使用isbusy函数来监视异步调用的回调,以确保它们都已完成。 如果用户向WCF服务发出请求,WCF服务将完成,但不会进行回调,因为应用程序将关闭。
这对我来说是个好消息,正如你所说的,通话可以像请求和遗忘模式一样工作。所以我不必太担心在提交过程中数据丢失

为了确保所有的服务电话都在申请关闭前发出,我认为计数器是一个简单而有效的想法。我将尝试在我的项目中实现它


谢谢你的帮助

我认为silverlight异步呼叫没有等待句柄,会带来不便。这是我的经验。我想检查并提交在浏览器关闭时未明确提交的数据修改。我在App_Exit中实现了如下代码:

    public class DispatcherCollection : UpdatableCollection<DocumentDispatcher>
{
    public override void SubmitChanges()
    {
        DocumentServiceClient client = new DocumentServiceClient();
        client.NewDocumentCompleted += (s, e) =>
        {
            // (s as DocumentServiceClient).CloseAsync();
            // do something
        };
        client.UpdateColumnCompleted += (s, e) =>
        {
            // (s as DocumentServiceClient).CloseAsync();
            // do something
        };
        client.RemoveDocumentCompleted += (s, e) =>
        {
            // (s as DocumentServiceClient).CloseAsync();
            // do something
        };
        foreach (DocumentDispatcher d in this)
        {
            if (d.IsNew)
            {
                // d=>object[] data
                client.NewDocumentAsync(data);
                d.IsNew=false;
            }
            else
            {
                foreach (string propertyName in d.modifiedProperties)
                {
                    client.UpdateColumnAsync(d.ID, GetPropertyValue(propertyName));
                }
                dd.ClearModifications();
            }
        }
        foreach (DocumentDispatcher dd in removedItems)
        {
            client.RemoveDocumentAsync(dd.ID);
        }
        removedItems.Clear();
    }
}
 private void Application_Exit(object sender, EventArgs e)
    {
        Document doc = EDPViewModel.CurrentViewModel.Document;
        if (doc != null) new ServiceClient().SubmitChangesAsync(doc);
    }
前提是在SubmitChangesSync方法中,找到并提交未提交的文档修改。因此,由于异步运行特性,在发送服务调用时,应用程序仍然会立即关闭。这将处理应用程序的相关资源,包括服务调用任务。因此,上述代码不起作用。我非常希望某个地方存在一种机制,可以从silverlight异步调用导出等待句柄,这样我就可以使用以下内容更新上述代码:

private void Application_Exit(object sender, EventArgs e)
    {
        Document doc = EDPViewModel.CurrentViewModel.Document;
        if (doc != null)
        {
            Task t = new TaskFactory().StartNew(() => new ServiceClient().SubmitChangesAsync(doc));
            t.Wait(); 
        }
    }
通过等待操作,我可以确定所有修改都确实提交了。那么在silverlight中有没有类似的模式可以使用呢