Skip to main content

Posts

Retry logic - revisited

My previous blog post gave a very short description of Polly  and that we were going to use it at work. The Polly library expose a set of options how to make your software more resilient. The most common (my guess) is a simple retry functionality, retry calling e.g. the database x number of times (x can also be indefinitely). Other options are timeouts, cache functionality, fallbacks and a couple more.  Our demand, however, didn’t exactly fit into the retry policy of Polly. We wanted to try x number of times (e.g. five times with two seconds of sleep between tries), then sleep for some time (e.g. 30 or 60 seconds) and then try again five times. Repeat until success. Also, we only wanted to log first time there is an error, and then when the try was successful (after retries, not if successful on first attempt).  using System; using Microsoft.Extensions.Logging; using Polly; using Polly.Retry; namespace RetrySample {
Recent posts

Retry logic

Implementing retry logic (e.g. when calling another module with a REST API) can be a bit of a mess. You start at one place in your code and everything looks nice, then after a while a need for configuration develops, and then retry-logic is needed somewhere else. On top of that, software is developed in different teams. At my work we are examining Polly  ("Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.") to see if that framework can help us. So far, I have just made simple tests and it looks promising. Stay tuned. 

Using a Brother QL-800 label printer

I'm currently working on a software project that will use a Brother QL-800 label printer. I chose to printer thanks to the presence of SDK documentation since I’m going to write a .NET Core WPF application that will handle the printer. The application is very simple, four input fields that shall be printed.  The SDK works well, it does what it’s supposed to do. The general feeling is that Brother actually created an SDK that was intended to be used. My approach was to create a template in Brother’s P-touch Editor software, and use the template (adding data to template) when printing. It’s probably possible to dynamically create the label in the API, but using a template is fast and easy. Every text field in the template is assigned an Object Name (e.g. “FieldName”). The object name is later used when printing. Below is a snippet of code which will print a label (29mm x 90mm) containing four text fields. using bpac; using System; namespace

Linq ForEach with index

Once you start using Linq in .NET you never go back... Even simple tasks as looping a list of items you still use Linq. But what happens when you actually need the index variable you get for free when using for-loop? Assume a list of objects: var values = new List<int>() { 2, 3, 4, 0x100, 74, 0xFFFF, 0x0F0F }; To iterate the list and having an index do the following: values.Select((x, i) => new { item = x, index = i }) .ToList() .ForEach(obj => { int value = obj.item; int valueIndex = obj.index; }); See also my answer on Stack Overflow .