tgerdes3609 tgerdes3609 -3 years ago 160
ASP.NET (C#) Question

ASP.NET web forms - how to asynchronously call WCF asynchronous method?

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:

Answer Source

I found that using a WCF web service for the kind of asynchronous work I needed to do (was not working from in the web service).

I switched to creating a console application that I call with arguments, and then store the results in a database. I then have the web site access the results stored in the database.

The console application context is much more friendly to completing the amount of asynchronous methods that I wanted to run at the same time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download