I have created a WCF service and hosted it in IIS. I can access the WCF service by creating a web reference in my asp.net web forms application. I need to have the WCF web service run a long running method (async). Does anyone have good example code of how to call a WCF asynchronous method from an asp.net web forms application button click method?
I have looked at IAsyncResult, EAP, and TAP... What currently is the best way to make an asynchronous call to a WCF asynchronous method from a ASP.NET web forms application?
I did now change my code to use a Service Reference instead of a Web reference.
Service 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 class:
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 forms client: Only first few lines of the method (for brevity)
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();
I am attempting to ping approximately 2000 IP addresses asynchronously. I am getting back four results that show success. Although, none of the other PingReply details are showing. I need to know what IP address was pinged.
Should I be awaiting somewhere... is that why it is returning to soon, or is there an error causing it to fail. Any suggestions would be appreciated.
Below, is a QuickWatch of my results: