RetryTask using Task Parallel Library


Have you ever had to implement some form of retry functionality? I have, a couple of times in fact. Common scenarios would be to retry web service requests in the event of CommunicationException, or retry some time consuming method invocation. The parameters for these retry logic would be to wait a specified amount of time and to retry a specified number of times.

A very common retry logic I’ve seen to implement the retry logic is using a while loop to enforce the maximum number of retries, and a Thread.Sleep to perform the wait time. This is an example below.

Read the rest of this entry »

Posted in C#, EdLib. Tags: . Leave a Comment »

String.Format and IFormatProvider


Have you ever noticed this overload of String.Format(IFormatProvider, String, Object[])? IFormatProvider is actually a very useful interface that allows us to create custom formats to transform our inputs. The IFormatProvider link has examples on how to do it.

This is my version on how to create a custom formatter factory that understand custom formats we create, and routes to the appropriate formatter. If a non-custom format is used, the conversion will still be performed as normal. In the example, I’m trying to create custom formats “bsb” and “acctno” which will convert numbers to a “999-999” format and “1234-56789” format using the CustomFormatProvider.

Read the rest of this entry »

XPath Extensions is cool!


I’m been using XDocument and it’s API for some time, but I wasn’t aware of an XPath Extensions in the .NET Framework. Thanks to my co-worker Lucy, she’s shared with me some good tips and in turn I’m sharing it with you.

Consider this XML.

<?xml version="1.0" encoding="utf-8" ?>
<Order>
 <Product id="1" name="Nexus 5">
 <Price>400.00</Price>
 <Qty>1</Qty>
 </Product>
 <Product id="5" name="Wireless Charger">
 <Price>50.00</Price>
 <Qty>1</Qty>
 </Product>
</Order>

This would have been typically how I would dig into the XML to find certain data using XDocument, in a safe way of course.

Read the rest of this entry »

Posted in C#. Tags: , . Leave a Comment »