我已经创建了一个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通。
我应该在等待什么地方......这就是为什么它很快返回,或者是否有错误导致它失败。任何建议,将不胜感激。
下面是我的结果的快速监视:
'我需要让WCF Web服务运行一个长时间运行的方法(异步)。' - ['async' does * not * yield to control to the browser](https://msdn.microsoft.com/zh-cn/ us/magazine/dn802603.aspx),如果这就是你的想法。 –
如何让浏览器在完成运行异步ping之后回调服务? – tgerdes3609
我推荐使用SignalR。 –