0

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:

2 Answers2

0

First, when using WCF why don't you use service reference? (Web vs Service reference) Then, when using SOAP client, it automatically generates SOAP WCF client with each method with synchronous and asynchronous variant.
Then you just call this async method

MyServiceClient.DummyMethodAsync(someParameters...)

Tomas Chabada
  • 2,869
  • 1
  • 17
  • 18
0

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.