简体   繁体   English

在双工WCF服务的客户端中使用基于任务的异步模式时出错

[英]Error when I use the Task-based Asynchronous Pattern in the client of duplex WCF service

I develop download manager application that consists of two parts: 1) Duplex WCF service that performs downloading and sends downloading status data to client in real-time. 我开发的下载管理器应用程序包括两个部分:1)双工WCF服务,该服务执行下载并将下载状态数据实时发送到客户端。 2) WPF Client that receives downloading status data from the service and displays in DataGrid. 2)WPF客户端,该客户端从服务接收下载状态数据并显示在DataGrid中。 In my duplex WCF service there is a callback interface 我的双工WCF服务中有一个回调接口

 [ServiceContract(CallbackContract = typeof(IDownloadManagerServiceCalback))] 
 public interface IDownloadManagerServiceCalback
 {
     /// <summary>
     /// Returns changed downloading status to client.
     /// </summary>
     /// <returns>Downloading which has changed status</returns>
     [OperationContract(IsOneWay = true)]
     void UpdateSelectedDownload(DownloadStatus p_SelectedDownload);
 }

On the client side I implement this interface: 在客户端,我实现此接口:

 class CallbackHandler : IDownloadManagerServiceCallback
 {
     /// <summary>
     /// "Download status changed" event.
     /// </summary>
     public event EventHandler<DownloadStatusChangedEventArgs> DownloadStatusChanged;

     public async Task UpdateSelectedDownload(DownloadStatus p_UpdatedDownload)
     {
         await Task.Run(() =>
         {
             // If handler was subscribed to event:
             if (DownloadStatusChanged != null)
             {
                  DownloadStatus updatedDownload = p_UpdatedDownload;
                  DownloadStatusChangedEventArgs updatedDownloadArgs = new DownloadStatusChangedEventArgs();
                  updatedDownloadArgs.Download = updatedDownload;
                  DownloadStatusChanged(this, updatedDownloadArgs);
             }
         });
    }
 }

When I build the solution I have the following error (text of error message i translate from Russian to English because my Visual Studio 2013 is Russianize): 生成解决方案时,我遇到以下错误(错误消息的文本我从俄语翻译成英语,因为我的Visual Studio 2013是俄语):

DownloadManager_Client.CallbackHandler doesn't implement member "DownloadManager_Client.DownloadManager_ServiceReference.IDownloadManagerServiceCallback.UpdateSelectedDownload(DownloadManager_Client.DownloadManager_ServiceReference.DownloadStatus)". DownloadManager_Client.CallbackHandler没有实现成员“ DownloadManager_Client.DownloadManager_ServiceReference.IDownloadManagerServiceCallback.UpdateSelectedDownload(DownloadManager_Client.DownloadManager_ServiceReference.DownloadStatus)”。 "DownloadManager_Client.CallbackHandler.UpdateSelectedDownload(DownloadManager_Client.DownloadManager_ServiceReference.DownloadStatus)" can't be implemented "DownloadManager_Client.DownloadManager_ServiceReference.IDownloadManagerServiceCallback.UpdateSelectedDownload(DownloadManager_Client.DownloadManager_ServiceReference.DownloadStatus)", because it doesn't contain appropriate returned “void” type. 无法实现“ DownloadManager_Client.CallbackHandler.UpdateSelectedDownload(DownloadManager_Client.DownloadManager_ServiceReference.DownloadStatus)”。

Here DownloadManager_Client is the name of WPF client project, DownloadManager_ServiceReference is the name of service reference to WCF service in the client project. 这里,DownloadManager_Client是WPF客户端项目的名称,DownloadManager_ServiceReference是对客户端项目中WCF服务的服务引用的名称。 How can I correct this error? 我该如何纠正该错误?

The interface should be defined as returning Task not void since your implementation is an async method returning a Task . 接口应定义为返回Task而不是void,因为您的实现是一个返回Task异步方法

EDIT: You are in a pickle because you want to use async which require a Task to be returned however your method is marked as IsOneWay = true - you can't have both. 编辑:您在泡菜中,因为您想使用需要返回Task的异步方法,但是您的方法被标记为IsOneWay = true-您不能同时使用两者。 Either IsOneWay = false and keep the async nature or keep one-way but remove the async. IsOneWay = false并保持异步性质,或者保持单向但删除异步。

Example 1 - Async method 示例1-异步方法

 [ServiceContract(CallbackContract = typeof(IDownloadManagerServiceCalback))] 
 public interface IDownloadManagerServiceCalback
 {
     /// <summary>
     /// Returns changed downloading status to client.
     /// </summary>
     /// <returns>Downloading which has changed status</returns>
     [OperationContract(IsOneWay = false)]
     Task UpdateSelectedDownload(DownloadStatus p_SelectedDownload);
 }

Then keep your original implementation returning Task 然后让您的原始实现返回Task

Example 2 - One-way method 示例2-单向方法

 [ServiceContract(CallbackContract = typeof(IDownloadManagerServiceCalback))] 
 public interface IDownloadManagerServiceCalback
 {
     /// <summary>
     /// Returns changed downloading status to client.
     /// </summary>
     /// <returns>Downloading which has changed status</returns>
     [OperationContract(IsOneWay = true)]
     void UpdateSelectedDownload(DownloadStatus p_SelectedDownload);
 }

In your implementation remove any await; 在您的实现中,删除所有等待的内容; async; 异步; tasks. 任务。

General 一般

Async WCF methods should return Task or Task < T > . 异步WCF方法应返回TaskTask <T> The only time you have an async void is during an event handler which is not applicable here. 唯一的异步无效时间是在事件处理程序期间,此处不适用。

As a general rule with async methods - avoid async void like the plague because an exception thrown in a try catch inside such a method can not be caught by a regular try-catch. 作为使用异步方法的一般规则,请避免像瘟疫一样避免 异步无效 ,因为在这种方法内部的try catch中抛出的异常无法通过常规try-catch捕获。 The only exception (no pun intended is during event handlers). 唯一的例外(在事件处理程序中无双关语)。

Async void methods have different error-handling semantics. 异步void方法具有不同的错误处理语义。 When an exception is thrown out of an async Task or async Task method, that exception is captured and placed on the Task object. 从异步Task或异步Task方法抛出异常时,将捕获该异常并将其放置在Task对象上。 With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started. 使用异步void方法时,没有Task对象,因此从异步void方法抛出的任何异常都将直接在启动异步void方法时处于活动状态的SynchronizationContext上引发。 More... 更多...

Would you like to know more? 你想知道更多吗?

Best Practices in Asynchronous Programming 异步编程最佳实践

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

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