简体   繁体   中英

Best approach for multiple WCF requests

I have a method that receives an array of productIds and with them i need to get a date for each one. The thing is that the service is only able to give me a date for each product. I have no access to service side... only client. Is there a better way to do the code bellow?

ChannelFactory<IService> channelFactory = null;
IService client = null;
List<DateTime> dates = new List<DateTime>();

using (channelFactory = new ChannelFactory<IService>("endpointName"))
{
    for (int i = 0; i < productIds.Length; i++)
    {
        client = channelFactory.CreateChannel();

        Request request = new Request();
        request.Id = productIds[i];

        Response response = client.execute(request);
        dates.Add(response.Date);
    }
}

Looks like you don't have to create channel on each iteration, just once before the loop. You can also try to parallelize the flow: svcutil can generate task-based async operations, I believe that there is something like that in ChannelFactory itself as well. But I'm afraid it's not possible to reduce amount of service calls without proper server-side support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM