WCF 3.5,AsyncBridge.包裹在async-await中

Stí*_*ndr 5 .net wcf asynchronous async-await

在工作中,我目前陷入3.5,但我们正在使用asyncbridge进行async-await.我们正在使用很多旧的WCF异步调用,我想将其包装到新的async-await模式中.

我把它包装如下:

    // async is wrong
    public /*async*/ Task<ScannedDocumentResult> GetScannedDocumentsTask(String assignmentId)
    {
        TaskCompletionSource<ScannedDocumentResult> tcs = new TaskCompletionSource<ScannedDocumentResult>();
        EventHandler<GetScannedDocumentsCompletedEventArgs> handler = null;
        handler = (o, e) =>
            {
                if (e.UserState != tcs)
                    return;

                if (e.Error != null)
                    tcs.SetException(e.Error);
                else if (e.Cancelled)
                    tcs.SetCanceled();
                else
                    tcs.SetResult(e.Result);

                GetScannedDocumentsCompleted -= handler;
            };
        GetScannedDocumentsCompleted += handler;
        GetScannedDocumentsAsync(assignmentId, tcs);

        return tcs.Task;            
    }
Run Code Online (Sandbox Code Playgroud)

以下是3.5 WCF代理中的以下内容:

GetScannedDocumentsAsync GetScannedDocumentsCompleted GetScannedDocumentsEventArgs

有些东西告诉我,这可以做得更清洁,我错过了一些重要的东西吗?

此外,这个方法会执行异步吗?使用异步运算符进行编译只会生成错误.

Ste*_*ary 6

你也应该得到一个BeginGetScannedDocumentsEndGetScannedDocuments你可以包装使用的TaskFactory.FromAsync.我有一篇博客文章,展示如何使用旧学校(4.5之前的)WCF(在服务器和客户端上)使用任务包装器.