我已經創建了一個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。 –