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.Split throws OutOfMemoryException


Recently I’ve been doing a fair bit of memory profiling and fixing memory issues at work. One interesting cause of an OutOfMemoryException I’ve encountered occurred when using String.Split for several large csv files concurrently. Of course the size and number of lines in the file that triggers the OutOfMemoryException is subjected to the memory capacity of your computer. To reproduce this, it’s relatively easy, first you need to create a large csv file.

Here’s some code to do it.

Read the rest of this entry »

Posted in EdLib. Tags: . Leave a Comment »

Object Proxy


Consider this simplistic example, which uses a stream reader.

public class FileLoader
{
    private readonly StreamReader _streamReader;

    public FileLoader(StreamReader streamReader)
    {
        _streamReader = streamReader;
    }

    public IEnumerable<string> Read()
    {
        var output = new List<string>();
        string line;
        while (!string.IsNullOrEmpty(line = _streamReader.ReadLine()))
        {
            output.Add(line);
        }
        _streamReader.Close();
        _streamReader.Dispose();
        return output;
    }
}

The problem I have with this class is that it isn’t exactly unit testable. First of all, it uses a stream reader and it’s not straightforward to be able to mock a stream reader due to the lack of an interface. Secondly what I reckon most developers would do is pass a real stream reader into the class and write tests in that fashion, which would be more of an integration test.

Read the rest of this entry »

Posted in EdLib. Tags: . Leave a Comment »

EdLib


Over the years as a developer, I’ve come to realise that I have encountered similar problems and have had to write same kind of code. I’m sure most of us would have gone through that kind of experience, when we wished we had saved the code snippet or functions in a central area so we can re-use it.

I’ve decided to do exactly that. I’ve created a personal library where I want to store useful, reusable code that helps me in my everyday work, and hopefully will be useful to others too. Coming up with a name was hard, so I’ve decide to concatenate my name and the work “Lib” together, which becomes EdLib.

Read the rest of this entry »

Posted in EdLib. Tags: . 2 Comments »