Home » excel » c# – How can I improve the performance of Excel UDF that call web services?

c# – How can I improve the performance of Excel UDF that call web services?

Posted by: admin April 23, 2020 Leave a comment

Questions:

We are developing a kind of stock lookup UDF in c# for Excel. Our UDF ends up calling a web service, which can be very slow if a sheet has, say 1000 calls to our web service.

I noticed that Excel 2010 has introduced Asynchronous UDFs to help with this http://msdn.microsoft.com/en-us/library/ff955606.aspx#odc_office14_ta_WhatsNewforOffice2010Developers_Excel2010

Has anyone tried this and have any c# examples, or have any other tips on improving the performance of web service type UDFs in Excel?

How to&Answers:

Whenever I’ve had this sort of issue, I’ve always decided to come up with a different approach to solve the problem.

In this case, if there’s any way you can batch up the calls and just make one webservice call then that would be the best thing as you’d only have one round trip instead of 1000 or whatever.

In other words, can you iterate through the 1000 cells and build up a (fairly big) request to send to the webservice and retrieve all the info at once? Then unpack it all when it returns.

If it’s your own webservice (which I’m hoping it is, since you said our webservice), then you should be able to modify it to do this. If you are calling an external webservice with no batch functionality then I’m afraid I’m not sure how best to approach it, other than to fire of numerous asynchronous UDFs on separate threads.

Answer:

You can use Excel RTD to provide asynchronous data retrieval. RTD allows you to push updates to Excel on demand, which means you can return quickly from the UDF call and asynchronously retrieve the data in the background. When the request is complete, you then notify Excel that updated values are available. You can also quite easily batch multiple UDF calls into a single web service request by simply queuing the RTD topics as they arrive and periodically forming a batched request.

Here is a link that describes how to create a basic RTD server in C#. To make the solution more user friendly, you can wrap the RTD worksheet function call in a UDF.