2017-06-20 215 views
0

我已经创建了一个WCF服务并将其托管在IIS中。我可以通过在我的asp.net web表单应用程序中创建一个Web引用来访问WCF服务。我需要让WCF Web服务运行一个长时间运行的方法(异步)。有没有人有如何从asp.net web窗体应用程序按钮点击方法调用WCF异步方法的很好的示例代码?ASP.NET web窗体 - 如何异步调用WCF异步方法?

我看过IAsyncResult,EAP和TAP ... 当前是从ASP.NET Web窗体应用程序异步调用WCF异步方法的最佳方法?

我现在改变我的代码来使用服务引用而不是Web引用。

服务ReceiptFooterService(的ServiceContract,OperationsContract,DataContract):

using System.Collections.Generic; 
using System.Net.NetworkInformation; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Threading.Tasks; 

namespace ReceiptFooterDeploymentService 
{ 
    [ServiceContract] 
    public interface IReceiptFooterService 
    { 
     [OperationContract(Name = "GetRegisterPingResults")] 
     Task<List<PingReply>> GetRegisterPingResults(List<StoreIP> potentialIPs); 
    } 

    [DataContract] 
    public class StoreIP 
    { 
     private int _storeNumber; 
     private string _ipAddress; 

     [DataMember] 
     public int StoreNumber 
     { 
      get 
      { 
       return _storeNumber; 
      } 
      set 
      { 
       _storeNumber = value; 
      } 
     } 

     [DataMember] 
     public string IPAddress 
     { 
      get 
      { 
       return _ipAddress; 
      } 
      set 
      { 
       _ipAddress = value; 
      } 
     } 
    } 
} 

ReceiptFooterService类:

using System.Collections.Generic; 
using System.Linq; 
using System.Net.NetworkInformation; 
using System.Threading.Tasks; 

namespace ReceiptFooterDeploymentService 
{ 
    public class ReceiptFooterService : IReceiptFooterService 
    { 
     public async Task<List<PingReply>> GetRegisterPingResults(List<StoreIP> potentialIPs) 
     { 
      var tasks = potentialIPs.Select(sip => new Ping().SendPingAsync(sip.IPAddress, 1000)); 
      var results = await Task.WhenAll(tasks); 

      return results.ToList(); 
     } 
    } 
} 

ASP.NET web表单客户端:只有该方法的前几行(为了简洁)

private List<StoreDetail> PingStoreAndUpdateStatus(List<StoreDetail> storeDetails) 
{ 
    ReceiptFooterService.StoreIP[] potentialIPs = GetStoreRegisterIps(storeDetails).ToArray(); 
    ReceiptFooterService.ReceiptFooterServiceClient client = new ReceiptFooterService.ReceiptFooterServiceClient(); 
    List<PingReply> pingReplies = client.GetRegisterPingResultsAsync(potentialIPs).Result.ToList(); 

我在尝试ping大约2000个IP add异步复位。我回来了四个显示成功的结果。虽然没有其他PingReply细节显示。我需要知道什么IP地址被ping通。

我应该在等待什么地方......这就是为什么它很快返回,或者是否有错误导致它失败。任何建议,将不胜感激。

下面是我的结果的快速监视:

+0

'我需要让WCF Web服务运行一个长时间运行的方法(异步)。' - ['async' does * not * yield to control to the browser](https://msdn.microsoft.com/zh-cn/ us/magazine/dn802603.aspx),如果这就是你的想法。 –

+0

如何让浏览器在完成运行异步ping之后回调服务? – tgerdes3609

+0

我推荐使用SignalR。 –

回答

0

我发现,使用了一种异步工作方式一个WCF Web服务,我需要做的(没有在网络服务中工作)。

我切换到创建一个控制台应用程序,我用参数调用,然后将结果存储在数据库中。然后我让网站访问存储在数据库中的结果。

控制台应用程序上下文对于完成我想要同时运行的异步方法的数量要友好得多。

0

首先,使用WCF时,你为什么不使用服务引用? (Web vs Service reference) 然后,当使用SOAP客户端时,它会自动生成具有同步和asynchronous变体的每种方法的SOAP WCF客户端。
然后你只需调用这个异步方法

MyServiceClient.DummyMethodAsync(someParameters ...)

+0

如果我显示我的源代码,它可能会有所帮助。我将在稍后发布。 – tgerdes3609