In a series of posts I'll cover mistakes I made in the past and good techniques I learned along the way. Come back often as this list will grow over time.
Monday, January 5, 2015
Unit testing smells and best practices
One of the most difficult task I know as a software engineer is to write good unit test. Of course multi-threading and distributed systems are also challenging but in this post I want to share some of the good and bad things I've learning doing unit testing over the last decade.
In a series of posts I'll cover mistakes I made in the past and good techniques I learned along the way. Come back often as this list will grow over time.
In a series of posts I'll cover mistakes I made in the past and good techniques I learned along the way. Come back often as this list will grow over time.
Sunday, January 4, 2015
Unit testing smells: The method is not public
A recurring pattern emerge often when unit testing, you find an interesting method you want to test but that method is private. Of course you could call that method through other public methods on the class but it might not be easy to cover all cases. This usually is a design smell for a Single Responsibility Principle violation. More on this later.
For now, to properly test the private method we have the following options:
Again, if you previously read my other post on non public class and inaccessible constructor you know you should avoid using reflection in tests.
Let's take a look at the other options
This breaks encapsulation of the class and will let developers call the method directly in production code. That might not be what you intended to do but can help you in the short term. Abusing this technique will burn you in the long run so be careful with it.
Fortunately, there is a simpler way: extract the method to another class.
But what to do when the method calls other methods and use fields or properties on the class? For that we must analyze the class to extract responsibilities using the Single Responsibility Principle.
Find more about the Single Responsibility Principle in an old post of mine.
For now, to properly test the private method we have the following options:
- Use reflection
- Change the method visibility to public or internal
- Extract the method to another class
Again, if you previously read my other post on non public class and inaccessible constructor you know you should avoid using reflection in tests.
Let's take a look at the other options
Change the method visibility to public or internal
The first thing we should do is to challenge why that method is private in the first place. Why can't we just change its visibility to public or internal (and use theInternalsVisibleTo attribute)?This breaks encapsulation of the class and will let developers call the method directly in production code. That might not be what you intended to do but can help you in the short term. Abusing this technique will burn you in the long run so be careful with it.
Fortunately, there is a simpler way: extract the method to another class.
Extract the method to another class
If you are lucky, the method you want to extract is static and don't call any other method on the original class. You should be able to extract that method to a static utility class.But what to do when the method calls other methods and use fields or properties on the class? For that we must analyze the class to extract responsibilities using the Single Responsibility Principle.
Single Responsibility Principle
Can you tell what is the purpose of your class in one sentence without using words like: and, but, also, or, etc.? If not then your class is doing more than one thing. Each segments of the sentence could be in different classes that only have one responsibility each. If the method you want to extract use the same fields than other methods you may want to push them by parameter instead before extraction to the other class.Find more about the Single Responsibility Principle in an old post of mine.
Labels:
Best Practices,
Dependency,
Design,
smells,
Solid,
Testing,
unit-test
Saturday, October 4, 2014
Unit testing smells: The class constructor is not easy to call
When we want to test an instance method on a class the first challenge is to create an instance of that class. Hopefully the class constructor is easy to call but that's not always the case.
Let's review a few cases where the constructor might prevent us to write tests easily:
Also stated in testing non-public class, you should ask yourself if it would be better to test this class via another public class that use it. A private constructor usually means that there is a factory somewhere you should use to instantiate the class. You should figure out a way to use that factory or redesign it in case you have difficulties to work with it in unit tests.
When dealing with internal access modifier I usually use the
Another case is when the arguments are difficult to create or provide. Maybe that in order to create one object to pass as parameter we need to create yet more objects. Micheal Feathers calls it the Onion Parameter in his book Working Effectively with Legacy Code. In that case I usually invest into a bit of testing infrastructure like factory methods or Test Data Builder in order to help me write tests faster afterwards.
Finally some parameters could be classes that wrap services or external dependencies. In that case I abstract the dependency using an interface and the Inversion of Control Principle. First extract an interface from the class. Next change the constructor to use this interface instead of the class. Finally use a mocking framework like Moq to create a fake (or create your own by hand if you don't like mocking frameworks) and pass it to the constructor.
Let's review a few cases where the constructor might prevent us to write tests easily:
- The constructor is private or internal
- It requires too many arguments or it is too hard to create/provide those arguments
- The constructor calls external dependencies
The case of the internal or private constructor
For this we can follow the same advices than with testing non-public class. But whatever you do please don't use reflection to call the constructor.Also stated in testing non-public class, you should ask yourself if it would be better to test this class via another public class that use it. A private constructor usually means that there is a factory somewhere you should use to instantiate the class. You should figure out a way to use that factory or redesign it in case you have difficulties to work with it in unit tests.
When dealing with internal access modifier I usually use the
InternalVisibleTo attribute for my test project. [assembly: InternalsVisibleTo("Contoso.MyApp.UnitTests.dll")]
The case of the difficult constructor arguments
If the problem is that the constructor requires too many arguments then you should ask yourself if this class is too big, maybe it has too many responsibilities and don't follow the Single Responsibility Principle? In that case the class should be splitted into two or more classes. This way it will be easier to test the smaller class because its constructor should require less arguments.Another case is when the arguments are difficult to create or provide. Maybe that in order to create one object to pass as parameter we need to create yet more objects. Micheal Feathers calls it the Onion Parameter in his book Working Effectively with Legacy Code. In that case I usually invest into a bit of testing infrastructure like factory methods or Test Data Builder in order to help me write tests faster afterwards.
Finally some parameters could be classes that wrap services or external dependencies. In that case I abstract the dependency using an interface and the Inversion of Control Principle. First extract an interface from the class. Next change the constructor to use this interface instead of the class. Finally use a mocking framework like Moq to create a fake (or create your own by hand if you don't like mocking frameworks) and pass it to the constructor.
public class MyService
{
// public MyService(SomeDataAccessLayer dal, SomeExternalService externalService)
public MyService(ISomeDataAccessLayer dal, ISomeExternalService externalService)
{
// ...
}
// ...
}
The case of the external dependencies calls
When the constructor itself calls an external dependency I usually refactor the constructor to inject this dependency via a parameter instead, this is called dependency injection and usually comes with the Inversion of Control Principle. This is also a good occasion to take a look at a IoC Container like Unity.public class MyService
{
// public MyService()
public MyService(ISomeExternalService externalService)
{
// ...
// var externalService = new ExternalService();
externalService.CallService();
}
// ...
}
Saturday, September 6, 2014
Unit test smells: The non-public class
Writing unit tests for a method on a class that is not public is doable but not straight forward. It could be done using a bit of reflection like this
That is quite a bit of work. Of course you could create some test infrastructure to avoid duplication in every tests or you may find libraries online for that. But still, writing tests like this smells funny to me.
In short, you should never write tests directly against a non-public class.
Let's take a look at the 2 possible cases for this: the private class and the internal class.
In a situation like this
If you find there is no way to reach the method you want to test in
If your project is signed you will need to also provide the assembly's public key like this
Again, this is a trade-off: you give special access to your internal types only for testing but it is way better than changing the class access modifier to
var type = Type.GetType("MyProject.MyClass");
var methodInfo = type.GetMethod("TheMethod");
var classInstance = Activator.CreateInstance(type, null);
methodInfo.Invoke(classInstance, null);
That is quite a bit of work. Of course you could create some test infrastructure to avoid duplication in every tests or you may find libraries online for that. But still, writing tests like this smells funny to me.
In short, you should never write tests directly against a non-public class.
Let's take a look at the 2 possible cases for this: the private class and the internal class.
The case of the private class
For a class to be private means it is nested inside another classpublic class A
{
// ...
private class B
{
// ...
}
}
In a situation like this
class B can only be used by class A. Anything class B do is only for serving class A. If you want to test a method in class B you should find out how class A use class B and write your tests against class A public API.If you find there is no way to reach the method you want to test in
class B through class A it simply means that you just found dead code! If you found a way but find it too hard to setup a test then maybe class B is not simply a private utility class. In that case I would extract class B from inside class A and change its access modifier to internal. Unfortunately, now any class inside the same assembly could use class B. It is a trade-off I'm willing to pay because this case is really rare and C# still lack a proper access modifier scoped to the current namespace.The case of the internal class
Internal class could also be tested through reflection. Personally I prefer to use theInternalsVisibleToAttribute and give access to internals types to my unit test project. To do it you need to add the attribute to the project under test AssemblyInfo.cs file like this[assembly: InternalsVisibleTo("Contoso.MyApp.UnitTests.dll")]
If your project is signed you will need to also provide the assembly's public key like this
[assembly: InternalsVisibleTo("Contoso.MyApp.UnitTests.dll, PublicKey=1234...789")]
Again, this is a trade-off: you give special access to your internal types only for testing but it is way better than changing the class access modifier to
public. You should never change a type access modifier to public for testing!Conclusion
I always try to write tests againstpublic classes and methods. Sometime an internal class will grow to be very complex over time and become its own new component. In a solution were we create a lot of small projects we would simply create a new one for such component and expose it publicly but that is not what I do. I usually try to create the minimal number of projects in my solution so testing internal classes using the InternalsVisibleToAttribute is a good trade-off for me.
Sunday, July 13, 2014
Exploring BDDfy
BDDfy is a BDD library (part of the larger TestStack project). It's function is to turn regular unit tests to BDD style tests (using the Gherkin syntax). You can read more about BDDfy here.
BDDfy can be used with any test framework or runner.
Optionally, you can also install code samples with this package.
All the magic is done by the

BDDfy will also generate a

First, the
This will group scenarios together in the output report.
With this style of test, not only we can reuse steps between scenarios we also gain the ability to parameterize the steps.
In the past I've use SpecFlow for my BDD tests. With SpecFlow you write your specification in a text file using the Gherkin language. The tool then parse the file and execute corresponding method for each steps. Having a text file seems interesting because we could have a business analyst write those. In really, developers ends up writing the stories and scenarios anyway.
This is why I like BDDfy, it's easy to learn and gives developers a lot of control over the way we create BDD style tests.
One more for my toolbox!
I hope you enjoyed this introduction to BDDfy.
BDDfy can be used with any test framework or runner.
Acquiring
To add BDDfy to your test project via NuGet run this command in the Package Manage Console.Install-Package TestStack.BDDfy
Optionally, you can also install code samples with this package.
Install-Package TestStack.BDDfy.Samples
Exploring
Hello world
For this part I'll be using the xUnit test framework. Let's start with something simple.public class FirstTest
{
void GivenTwoAndTwo()
{
// ...
}
void WhenIAddThem()
{
// ...
}
void ThenTheAnwserShouldBe4()
{
// ...
}
[Fact]
public void ExecuteFirstTest()
{
this.BDDfy();
}
}
All the magic is done by the
BDDFy extension methods. This will scan the FirstTest class for methods starting with keywords like Given, When and Then. Next, it will run the methods in order (BDD style). Finally, we will get a nice output report like this.
BDDfy will also generate a
BDDfy.html file in the test project output folder. This is the report of all BDDFyed tests.
Using attributes to customize the test
BDDfy follow conventions when scanning a class for methods of interest, you can find a list here. If we need more control we can do it by using attributes.[Story(
AsA = "As someone lazy",
IWant = "I want the computer to add 2 number",
SoThat = "I don't have to do the math myself")]
public class TestWithAttributesToOverriteText
{
[Given("Given 2 + 2")]
void GivenTwoAndTwo()
{
// ...
}
[When(" + ")]
void WhenIAddThem()
{
// ...
}
[Then("Then the anwser = 4")]
void ThenTheAnwserShouldBe4()
{
// ...
}
void AndThenItShouldDisplayTheAnwser()
{
}
[Fact]
public void ExecuteTestWithAttributes()
{
this.BDDfy();
}
}
First, the
[Story] attribute allow us to provide the classic story definition for the test. Other attributes like [Given], [When] and [Then] allow us to provide a custom description for the steps. Also, using the attributes will allow us to name the step methods the way we want. Creating more than one scenario per story
Usually a story contains more than one test or scenario. We can do this using nested classes.[Story(
Title = "Using story attribute and setting the Title!",
AsA = "As someone learning BDDfy",
IWant = "I want to try splitting scenario in separated classes",
SoThat = "My code is cleaner")]
public class TestWithStoryAndScenarioInSeparatedClasses
{
[Fact]
public void FirstScenario()
{
new S1().BDDfy("Custom scenario title");
}
[Fact]
public void SecondScenario()
{
new S2().BDDfy();
}
private class S1
{
void GivenWhatever() { // ... }
void WhenSomethingHappens() { // ... }
void ThenProfit() { // ... }
}
private class S2
{
void GivenWhatever() { // ... }
void WhenSomethingElseHappens() { // ... }
void ThenProfit() { // ... }
}
}
This will group scenarios together in the output report.
Using the fluent API for even more control
With what we've seen previously we need to create a new test class for each scenario we have. That leads to a lot of duplicated code unless we delegate to a common test fixture. An alternative is to use BDDfy fluent API to get some code reuse between our scenarios.public class TestWithFluentApi
{
[Fact]
public void ReusingStepForScenario1()
{
new TestWithFluentApi()
.Given(s => s.GivenWhatever(), "Given some pre-condition")
.And(s => s.AndOtherGiven(54))
.When(s => s.WhenSomethingElseHappens())
.Then(s => s.ThenProfit())
.And(s => s.AndManyMore(45))
.BDDfy();
}
[Fact]
public void ReusingStepForScenario2()
{
new TestWithFluentApi()
.Given(s => s.GivenWhatever(), "Given some pre-condition")
.And(s => s.AndOtherGiven(123))
.When(s => s.WhenSomethingElseHappens())
.Then(s => s.ThenProfit())
.And(s => s.AndManyMore(321), "And {0} more things!")
.BDDfy("Scenario 2 with steps re-use");
}
void GivenWhatever() { // ... }
void AndOtherGiven(int input) { // ... }
void WhenSomethingElseHappens() { // ... }
void ThenProfit() { // ... }
void AndManyMore(int expected) { // ... }
}
With this style of test, not only we can reuse steps between scenarios we also gain the ability to parameterize the steps.
Assessment
I've only scratched the surface of what BDDfy can do. You can read more about on BDDfy usage and customization on the project web site.In the past I've use SpecFlow for my BDD tests. With SpecFlow you write your specification in a text file using the Gherkin language. The tool then parse the file and execute corresponding method for each steps. Having a text file seems interesting because we could have a business analyst write those. In really, developers ends up writing the stories and scenarios anyway.
This is why I like BDDfy, it's easy to learn and gives developers a lot of control over the way we create BDD style tests.
One more for my toolbox!
I hope you enjoyed this introduction to BDDfy.
Sunday, June 22, 2014
Visualizing Nuget packages dependencies without Visual Studio Ultimate
In my previous post I've shown the Package Visualizer tool. Unfortunately, it's only available in the Ultimate version of Visual Studio. But all is not lost because even with a Pro version with can open DGML files.
I've created a LinqPad query that analyse packages.config files and create a DGML diagram like Package Visualizer does. I've also added things like GAC libraries and normal file based library to the mix. You can get the full Gist here. Now let's take a look at some code…
I've left a few utility methods out of the inline code in this post but you can get all the code from the Gist. Feel free to grab a copy of the file and adapt it to your heart's content. It would be easy to create a small Console Application and call it from command line if you don't like LinqPad.
There is still a lot more I could add to the query like extracting projects and library versions from the DLL, dependencies between NuGet packages from .nupkg files and highlighting duplicates NuGet packages with different version. Still, it's enough for me in it's current form.
I hope this will help you figure out your NuGet packages usage and dependencies in your solution.
I've created a LinqPad query that analyse packages.config files and create a DGML diagram like Package Visualizer does. I've also added things like GAC libraries and normal file based library to the mix. You can get the full Gist here. Now let's take a look at some code…
Main
Here we set a few options for our query: some file extensions to ignore when scanning for projects and more importantly the root folder path to start scanning for project files.private string[] projectExtensionExclusions = new[] { ".vdproj", ".ndproj" };
private string rootFolder = @"C:\Users\Pascal\Dev\MyProject";
void Main()
{
LoadAllProjects();
LoadAllPackagesConfig();
GenerateDGML(Path.Combine(rootFolder, "Dependencies.dgml"));
}
Data structures to uses
Then we define some fields and basic classes to help us gather the informationprivate List<Project> projects = new List<Project>();
private List<Package> packages = new List<Package>();
private List<Library> libraries = new List<Library>();
public class Project
{
public Project()
{
this.Projects = new List<Project>();
this.Libraries = new List<Library>();
this.Packages = new List<Package>();
}
public string Path { get; set; }
public string Name { get; set; }
public List<Project> Projects { get; private set; }
public List<Library> Libraries { get; private set; }
public List<Package> Packages { get; private set; }
}
public class Package
{
public string Name { get; set; }
public string Version { get; set; }
}
public class Library
{
public string Name { get; set; }
public bool IsGAC { get; set; }
}
LoadAllProjects
Now we can start scanning for projects to load. Next we open each project files and extract all dependencies like other project, a local library or a GAC reference. We keep all this info in the project instances for later.private void LoadAllProjects()
{
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
var projectFiles = Directory.GetFiles(rootFolder, "*.*proj",
SearchOption.AllDirectories)
.Where (pf => !projectExtensionExclusions.Any(ex => pf.EndsWith(ex)));
foreach (var pf in projectFiles)
this.projects.Add(
new Project { Path = pf, Name = Path.GetFileNameWithoutExtension(pf) });
// Get all projects, local libraries and GAC references
foreach (var project in this.projects)
{
var projectDoc = XDocument.Load(project.Path);
foreach (var pr in projectDoc.Descendants(ns + "ProjectReference"))
{
var prj = projects.SingleOrDefault(p =>
p.Name == pr.Element(ns + "Name").Value);
if (prj != null)
project.Projects.Add(prj);
else
(pr.Element(ns + "Name").Value
+ " project reference not found in file " + project.Path).Dump();
}
foreach (var r in projectDoc.Descendants(ns + "Reference")
.Where (r => !r.Value.Contains(@"\packages\")))
project.Libraries.Add(GetOrCreateLibrary(
r.Attribute("Include").Value, !r.Elements(ns + "HintPath").Any()));
}
}
LoadAllPackagesConfig
Finally we scan for packages.config files, the ones responsible for maintaining the NuGet packages dependencies for a project. Again we extract the dependencies information from the files and keep it for later.private void LoadAllPackagesConfig()
{
foreach (var pk in Directory.GetFiles(rootFolder, "packages.config",
SearchOption.AllDirectories)
.Where (pc => !pc.Contains(".nuget")))
{
var project = this.projects.SingleOrDefault(p =>
Path.GetDirectoryName(p.Path) == Path.GetDirectoryName(pk));
if (project == null)
("Project not found in same folder than package " + pk).Dump();
else
{
foreach (var pr in XDocument.Load(pk).Descendants("package"))
{
var package = GetOrCreatePackage(
pr.Attribute("id").Value, pr.Attribute("version").Value);
project.Packages.Add(package);
}
}
}
}
GenerateDGML
Here we generate the final DGML file which is simply an XML file. The schema is quite simple: a root element DirectedGraph, a Nodes section and a Links section, all of which are mandatory. We also add a Styles section to colorize the different kind of nodes: projects, packages, libraries and GAC libraries.private XNamespace dgmlns = "http://schemas.microsoft.com/vs/2009/dgml";
private void GenerateDGML(string filename)
{
var graph = new XElement(dgmlns + "DirectedGraph",
new XAttribute("GraphDirection", "LeftToRight"),
new XElement(dgmlns + "Nodes",
this.projects.Select (p => CreateNode(p.Name, "Project")),
this.libraries.Select (l => CreateNode(l.Name,
l.IsGAC ? "GAC Library" : "Library", l.Name.Split(',')[0])),
this.packages.Select (p => CreateNode(p.Name + " " + p.Version, "Package")),
CreateNode("AllProjects", "Project", label: "All Projects", @group: "Expanded"),
CreateNode("AllPackages", "Package", label: "All Packages", @group: "Expanded"),
CreateNode("LocalLibraries", "Library", label: "Local Libraries", @group: "Expanded"),
CreateNode("GlobalAssemblyCache", "GAC Library", label: "Global Assembly Cache", @group: "Collapsed")),
new XElement(dgmlns + "Links",
this.projects.SelectMany(p => p.Projects.Select(pr => new { Source = p, Target = pr } ))
.Select (l => CreateLink(l.Source.Name, l.Target.Name, "Project Reference")),
this.projects.SelectMany(p => p.Libraries.Select(l => new { Source = p, Target = l } ))
.Select (l => CreateLink(l.Source.Name, l.Target.Name, "Library Reference")),
this.projects.SelectMany(p => p.Packages.Select(pa => new { Source = p, Target = pa } ))
.Select (l => CreateLink(l.Source.Name, l.Target.Name + " " + l.Target.Version, "Installed Package")),
this.projects.Select (p => CreateLink("AllProjects", p.Name, "Contains")),
this.packages.Select (p => CreateLink("AllPackages", p.Name + " " + p.Version, "Contains")),
this.libraries.Where (l => !l.IsGAC).Select (l => CreateLink("LocalLibraries", l.Name, "Contains")),
this.libraries.Where (l => l.IsGAC).Select (l => CreateLink("GlobalAssemblyCache", l.Name, "Contains"))),
// No need to declare Categories, auto generated
new XElement(dgmlns + "Styles",
CreateStyle("Project", "Blue"),
CreateStyle("Package", "Purple"),
CreateStyle("Library", "Green"),
CreateStyle("GAC Library", "LightGreen")));
var doc = new XDocument(graph);
doc.Save(filename);
}
Conclusion
All that is left is to open the Dependencies.dgml file in Visual StudioI've left a few utility methods out of the inline code in this post but you can get all the code from the Gist. Feel free to grab a copy of the file and adapt it to your heart's content. It would be easy to create a small Console Application and call it from command line if you don't like LinqPad.
There is still a lot more I could add to the query like extracting projects and library versions from the DLL, dependencies between NuGet packages from .nupkg files and highlighting duplicates NuGet packages with different version. Still, it's enough for me in it's current form.
I hope this will help you figure out your NuGet packages usage and dependencies in your solution.
Saturday, May 24, 2014
Managing NuGet packages dependencies with the Package Visualizer tool
If you
ever used NuGet on a large enough solution
you know you can get into trouble when projects reference different versions of the same NuGet
package. That happens a lot in Azure projects as the libraries/packages get
updated all the time.
I'm
really surprise when I talk to people using NuGet everyday that they don't know
about the Package
Visualizer tool. (update: I've been told
that this feature requires VS Ultimate and is not available in the Pro version.
I'm still going to show it to you but stay tune for another post with a free
alternative later)
NuGet Package Visualizer in Visual Studio
Once you open
up a solution in Visual Studio you can go to the Tools
menu, NuGet Package Manager and Package Visualizer.
This will
analyse all the packages.config files in
the solution and generate a DGML
diagram of all NuGet packages and projects of the solution. The
diagram will help us see packages usage in the solution and find the ones with
different versions. Below you can see
that I've tried this on the Roslyn (open
source C# compiler) solution.
The first
thing to note (and a surprise to me!) is that Roslyn use the XUnit testing framework and not MsUnit! More seriously we can quickly see that we have no duplicate
packages with different versions. If we
compare that to this sample solution I created we can see I'm using two
versions of the Json.NET library. Now I know I should update the ClassLibrary1
project to use the new version of the package.
Of
course, this only work for NuGet packages but it would be useful to have
something like this for regular DLL references.
I'll try to work on a LinqPad query to generate such a DGML graph with
all projects, libraries and packages.
Stay tune till next time.
Friday, April 25, 2014
Looking inside a NuGet package with NuGet Package Explorer
When I really want to learn something new (like a new tool, technology or a programming language) I do two things
I'm currently learning how to create my own NuGet packages so I'm trying to do a lot of things on my own, but I would also like to see how existing packages are made.
After installing a NuGet package in your project you can go in the packages folder and unzip the .nupkg file (yes, it's only a zip file with a different extension). Fortunately, there is an easier. NuGet Package Explorer is an open source tool available on CodePlex.
With it you can load a package from the official NuGet feed or any other feeds you want even local feeds.
Then when we open a package we can explore its content and even go inside individual files
NuGet Package Explorer also allow us to edit files and the package itself if we want.
One trick I like to do is to add my local NuGet package download cache as a feed. To do that in the Tools menu select View NuGet download cache.
This is the folder where all the packages you previously downloaded are cached (from Visual Studio, NuGet Package Explorer and any other NuGet based tools). Simply copy the path and paste it in the Package Source field like this
This way I can quickly get to a package I just installed in my solution.
NuGet Package Explorer is a powerful tool I use a lot to understand how NuGet packages are made.
I hope it will help you too.
- I try it myself
- Check out what others have done
I'm currently learning how to create my own NuGet packages so I'm trying to do a lot of things on my own, but I would also like to see how existing packages are made.
After installing a NuGet package in your project you can go in the packages folder and unzip the .nupkg file (yes, it's only a zip file with a different extension). Fortunately, there is an easier. NuGet Package Explorer is an open source tool available on CodePlex.
With it you can load a package from the official NuGet feed or any other feeds you want even local feeds.
Then when we open a package we can explore its content and even go inside individual files
NuGet Package Explorer also allow us to edit files and the package itself if we want.
One trick I like to do is to add my local NuGet package download cache as a feed. To do that in the Tools menu select View NuGet download cache.
This is the folder where all the packages you previously downloaded are cached (from Visual Studio, NuGet Package Explorer and any other NuGet based tools). Simply copy the path and paste it in the Package Source field like this
This way I can quickly get to a package I just installed in my solution.
NuGet Package Explorer is a powerful tool I use a lot to understand how NuGet packages are made.
I hope it will help you too.
Friday, April 18, 2014
Packaging and distributing tools using NuGet
NuGet is an amazing tool to manage dependencies for external libraries. As part of Visual Studio it gives us an easy way to install and update those libraries over time. Now even Microsoft use it extensively to release updates to us. But the power of NuGet doesn't stop here. Some people found imaginative ways to use NuGet like the ScriptCs project and Chocolatey.
Another usage is to package and distribute utilities via NuGet packages like FAKE and xUnit.Runners. To create your own tool package you need to author a NuSpec file like this:
The key here is to set all your file's target to tools\. Doing that the package will be considered a solution-level NuGet package and will be available solution wide instead of only for one project.
For example if I install the xUnit.Runners package to my solution like this:
You will see that only a new package.config file will be created in the .nuget folder at the root of your solution. This is where all solution-level packages will be referenced. Nothing will actually change inside your projects.
After that all the files required to run the xUnit runner from the command line, PowerShell or a build script will be available from the \packages\xunit.runners.1.9.2\tools folder.
The power of NuGet doesn't stop there but we'll check that in another blog post.
Another usage is to package and distribute utilities via NuGet packages like FAKE and xUnit.Runners. To create your own tool package you need to author a NuSpec file like this:
<package>
…
<files>
<file src="tooling\app.exe" target="tools\" />
</files>
</package>
The key here is to set all your file's target to tools\. Doing that the package will be considered a solution-level NuGet package and will be available solution wide instead of only for one project.
For example if I install the xUnit.Runners package to my solution like this:
PM> Install-Package xunit.runners
You will see that only a new package.config file will be created in the .nuget folder at the root of your solution. This is where all solution-level packages will be referenced. Nothing will actually change inside your projects.
After that all the files required to run the xUnit runner from the command line, PowerShell or a build script will be available from the \packages\xunit.runners.1.9.2\tools folder.
The power of NuGet doesn't stop there but we'll check that in another blog post.
Sunday, December 29, 2013
Implementing a Treemap in C#
I was wondering how to implement a treemap in C#. A treemap is a data visualisation technique that looks like this

I know a few examples exist online but between a very abstract paper like this one and a javascript implementation on GitHub I was wondering does one could write an algorithm from scratch using a naive approach.
Let's start with a simple example, the geometric series 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64… would give us something like this

My guess would be to use a recursive algorithm to create our treemap. First we need to order all the elements from biggest to smallest. Then we start to divide the area for each elements starting with the first element which takes half the available space. After that we divide the remaining area with the rest of the elements. We repeat this process until we have all the pieces.
Now if we take another more realistic example

Here every time we need to slice the area we need to make sure it won't be too small or won't look so great. If the element represent 50% or more of the total it won't be a problem but what about only 10% or 4%? I think we should set a minimum threshold for our slice, let say 25% for now. We will experiment with this value once we finish our algorithm.
So what happen if the largest value is below our 25% threshold? I think we should include more elements in the slice until we reach at least 25%. For example if we have 14%, 8% and 5% the total is 27%, so our first slice will be 27% of the available space. Then we need to distribute the 3 elements in that slice. This is in essence a subset of our original problem so we can repeat the process just for that slice.
By the way what orientation the first slice should be? I think we should always slice on the longest side of the area rectangle. If we have a square then it doesn't matter really.
Next, how do we divide the first slice? We have 3 elements: 14%, 8% and 5%. If we look for a solution from the start we now have to following proportions: 51.8%, 29.6% and 18.5%. We can take a new slice only for the first element. And we repeat the process for the last 2 elements. Which is now 61.5% and 38.5%. At each step we need to evaluate if the slice will be horizontal or vertical depending on the the shape of the area we have.
Finally, all we have to do is repeat this until all the elements are placed!
Here is my implementation in LinqPad in 3 parts
Slice calculation
Generating rectangles using leaf slice (slice with only one element in it)
Drawing the rectangles in WinForm
And finally to generate a Treemap in LinqPad
References:

I know a few examples exist online but between a very abstract paper like this one and a javascript implementation on GitHub I was wondering does one could write an algorithm from scratch using a naive approach.
Let's start with a simple example, the geometric series 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64… would give us something like this

My guess would be to use a recursive algorithm to create our treemap. First we need to order all the elements from biggest to smallest. Then we start to divide the area for each elements starting with the first element which takes half the available space. After that we divide the remaining area with the rest of the elements. We repeat this process until we have all the pieces.
Now if we take another more realistic example

Here every time we need to slice the area we need to make sure it won't be too small or won't look so great. If the element represent 50% or more of the total it won't be a problem but what about only 10% or 4%? I think we should set a minimum threshold for our slice, let say 25% for now. We will experiment with this value once we finish our algorithm.
So what happen if the largest value is below our 25% threshold? I think we should include more elements in the slice until we reach at least 25%. For example if we have 14%, 8% and 5% the total is 27%, so our first slice will be 27% of the available space. Then we need to distribute the 3 elements in that slice. This is in essence a subset of our original problem so we can repeat the process just for that slice.
By the way what orientation the first slice should be? I think we should always slice on the longest side of the area rectangle. If we have a square then it doesn't matter really.
Next, how do we divide the first slice? We have 3 elements: 14%, 8% and 5%. If we look for a solution from the start we now have to following proportions: 51.8%, 29.6% and 18.5%. We can take a new slice only for the first element. And we repeat the process for the last 2 elements. Which is now 61.5% and 38.5%. At each step we need to evaluate if the slice will be horizontal or vertical depending on the the shape of the area we have.
Finally, all we have to do is repeat this until all the elements are placed!
Here is my implementation in LinqPad in 3 parts
Slice calculation
public Slice<T> GetSlice<T>(IEnumerable<Element<T>> elements, double totalSize,
double sliceWidth)
{
if (!elements.Any()) return null;
if (elements.Count() == 1) return new Slice<T>
{ Elements = elements, Size = totalSize };
var sliceResult = GetElementsForSlice(elements, sliceWidth);
return new Slice<T>
{
Elements = elements,
Size = totalSize,
SubSlices = new[]
{
GetSlice(sliceResult.Elements, sliceResult.ElementsSize, sliceWidth),
GetSlice(sliceResult.RemainingElements, 1 - sliceResult.ElementsSize,
sliceWidth)
}
};
}
private SliceResult<T> GetElementsForSlice<T>(IEnumerable<Element<T>> elements,
double sliceWidth)
{
var elementsInSlice = new List<Element<T>>();
var remainingElements = new List<Element<T>>();
double current = 0;
double total = elements.Sum(x => x.Value);
foreach (var element in elements)
{
if (current > sliceWidth)
remainingElements.Add(element);
else
{
elementsInSlice.Add(element);
current += element.Value / total;
}
}
return new SliceResult<T>
{
Elements = elementsInSlice,
ElementsSize = current,
RemainingElements = remainingElements
};
}
public class SliceResult<T>
{
public IEnumerable<Element<T>> Elements { get; set; }
public double ElementsSize { get; set; }
public IEnumerable<Element<T>> RemainingElements { get; set; }
}
public class Slice<T>
{
public double Size { get; set; }
public IEnumerable<Element<T>> Elements { get; set; }
public IEnumerable<Slice<T>> SubSlices { get; set; }
}
public class Element<T>
{
public T Object { get; set; }
public double Value { get; set; }
}
Generating rectangles using leaf slice (slice with only one element in it)
public IEnumerable<SliceRectangle<T>> GetRectangles<T>(Slice<T> slice, int width,
int height)
{
var area = new SliceRectangle<T>
{ Slice = slice, Width = width, Height = height };
foreach (var rect in GetRectangles(area))
{
// Make sure no rectangle go outside the original area
if (rect.X + rect.Width > area.Width) rect.Width = area.Width - rect.X;
if (rect.Y + rect.Height > area.Height) rect.Height = area.Height - rect.Y;
yield return rect;
}
}
private IEnumerable<SliceRectangle<T>> GetRectangles<T>(
SliceRectangle<T> sliceRectangle)
{
var isHorizontalSplit = sliceRectangle.Width >= sliceRectangle.Height;
var currentPos = 0;
foreach (var subSlice in sliceRectangle.Slice.SubSlices)
{
var subRect = new SliceRectangle<T> { Slice = subSlice };
int rectSize;
if (isHorizontalSplit)
{
rectSize = (int)Math.Round(sliceRectangle.Width * subSlice.Size);
subRect.X = sliceRectangle.X + currentPos;
subRect.Y = sliceRectangle.Y;
subRect.Width = rectSize;
subRect.Height = sliceRectangle.Height;
}
else
{
rectSize = (int)Math.Round(sliceRectangle.Height * subSlice.Size);
subRect.X = sliceRectangle.X;
subRect.Y = sliceRectangle.Y + currentPos;
subRect.Width = sliceRectangle.Width;
subRect.Height = rectSize;
}
currentPos += rectSize;
if (subSlice.Elements.Count() > 1)
{
foreach (var sr in GetRectangles(subRect))
yield return sr;
}
else if (subSlice.Elements.Count() == 1)
yield return subRect;
}
}
public class SliceRectangle<T>
{
public Slice<T> Slice { get; set; }
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
Drawing the rectangles in WinForm
public void DrawTreemap<T>(IEnumerable<SliceRectangle<T>> rectangles, int width,
int height)
{
var font = new Font("Arial", 8 );
var bmp = new Bitmap(width, height);
var gfx = Graphics.FromImage(bmp);
gfx.FillRectangle(Brushes.Blue, new RectangleF(0, 0, width, height));
foreach (var r in rectangles)
{
gfx.DrawRectangle(Pens.Black,
new Rectangle(r.X, r.Y, r.Width - 1, r.Height - 1));
gfx.DrawString(r.Slice.Elements.First().Object.ToString(), font,
Brushes.White, r.X, r.Y);
}
var form = new Form() { AutoSize = true };
form.Controls.Add(new PictureBox()
{ Width = width, Height = height, Image = bmp });
form.ShowDialog();
}
And finally to generate a Treemap in LinqPad
void Main()
{
const int Width = 400;
const int Height = 300;
const double MinSliceRatio = 0.35;
var elements = new[] { 24, 45, 32, 87, 34, 58, 10, 4, 5, 9, 52, 34 }
.Select (x => new Element<string> { Object = x.ToString(), Value = x })
.OrderByDescending (x => x.Value)
.ToList();
var slice = GetSlice(elements, 1, MinSliceRatio).Dump("Slices");
var rectangles = GetRectangles(slice, Width, Height)
.ToList().Dump("Rectangles");
DrawTreemap(rectangles, Width, Height);
}
References:
Wednesday, August 28, 2013
Windows Azure Caching and transient faults
When using remote services over the wire we should always plan for transient failures. Windows Azure Caching like any services in an Azure world is prone to such problem. Out of the box making calls to the cache server will fail from time to time due to network issues. Typically you will get those kind of exceptions:
For that reason it is a best practice to implement some kind of retry logic around your code calling the cache server. We could have used the Transient Application Block to manage that. But a few months ago I found somewhere that from time to time the DataCache object lose it's internal connection to the cache server. A simple way to fix this is to re-create a DataCache instance and retry the operation.
In the implementation below I'm keeping a reference to the DataCacheFactory and DataCache objects (another best practice). The CreateDataCache factory method will come handy later.
Then I have this SafeCallFunction I use whenever I want to work with the DataCache object. Notice that the only thing I do to retry the operation is to call the factory method to re-create the DataCache object.
Finally in the rest of the class I can use the SafeCallFunction like this
So far after a few weeks of using this implementation the single retry never failed on us. Before that we had around 5-10 failures daily for about 500k calls to the cache server. I would still recommend using a more robust retry policy with Windows Azure Caching but I think it's interesting to know that simply instantiating a new DataCache can fix most failures.
Best Practices for using Windows Azure Cache
Optimization Guidance for Windows Azure Caching
The Transient Fault Handling Application Block
Microsoft.ApplicationServer.Caching.DataCacheException: ErrorCode<ERRCA0017>:SubStatus<ES0006>:There is a temporary failure. Please retry later.
Microsoft.ApplicationServer.Caching.DataCacheException: ErrorCode<ERRCA0018>:SubStatus<ES0001>:The request timed out.
Microsoft.ApplicationServer.Caching.DataCacheException: ErrorCode<ERRCA0016>:SubStatus<ES0001>:The connection was terminated, possibly due to server or network problems or serialized Object size is greater than MaxBufferSize on server.
For that reason it is a best practice to implement some kind of retry logic around your code calling the cache server. We could have used the Transient Application Block to manage that. But a few months ago I found somewhere that from time to time the DataCache object lose it's internal connection to the cache server. A simple way to fix this is to re-create a DataCache instance and retry the operation.
In the implementation below I'm keeping a reference to the DataCacheFactory and DataCache objects (another best practice). The CreateDataCache factory method will come handy later.
public class CachingService
{
private DataCacheFactory cacheFactory;
private DataCache cache;
private DataCache Cache
{
get
{
if (this.cache == null)
{
this.CreateDataCache();
}
return this.cache;
}
}
private void CreateDataCache()
{
this.cacheFactory = new DataCacheFactory();
this.cache = this.cacheFactory.GetDefaultCache();
}
// ...
}
Then I have this SafeCallFunction I use whenever I want to work with the DataCache object. Notice that the only thing I do to retry the operation is to call the factory method to re-create the DataCache object.
private object SafeCallFunction(Func<object> function)
{
try
{
return function.Invoke();
}
catch (DataCacheException)
{
// Retry by first re-creating the DataCache
try
{
this.CreateDataCache();
return function.Invoke();
}
catch (DataCacheException)
{
// Log error
}
}
return null;
}
Finally in the rest of the class I can use the SafeCallFunction like this
public object CacheGet(string key)
{
return this.SafeCallFunction(() => this.Cache.Get(key));
}
public void CachePut(string key, object cacheObject)
{
this.SafeCallFunction(() => this.Cache.Put(key, cacheObject));
}
public void CacheRemove(string key)
{
this.SafeCallFunction(() => this.Cache.Remove(key));
}
So far after a few weeks of using this implementation the single retry never failed on us. Before that we had around 5-10 failures daily for about 500k calls to the cache server. I would still recommend using a more robust retry policy with Windows Azure Caching but I think it's interesting to know that simply instantiating a new DataCache can fix most failures.
References
Caching in Windows AzureBest Practices for using Windows Azure Cache
Optimization Guidance for Windows Azure Caching
The Transient Fault Handling Application Block
Labels:
Azure,
Azure Caching,
Best Practices,
Caching,
Error Handling,
Tips
Wednesday, July 31, 2013
Using Windows Azure Caching efficiently across multiple Cloud Service roles
Windows Azure Caching is a great way to improve performance of your Azure application at no additional cost. The cache is running alongside your application in Cloud Service roles. The only thing you need to decide is how much memory of the role you want use for it (for co-located cache role). You can also dedicate the all memory of a role to caching if you want (with dedicated cache role). Roles that host caching are called cache clusters.
Starting with Azure Caching is so easy that it can be a while before you fully understand the best way to use it. On a recent project my first tough was to enable caching on all the Cloud Service roles as co-located service. This was causing us problems.
First of all, been a developer I debug my application using the local Azure compute emulator. The emulator runs one cache service for each instances of roles with cache clusters. The application has 2 web roles and 1 worker role so when I start a debugging session with multiple instances per role I need a lot of memory to run everything. More importantly, cache clusters do not share cached data between each other. This caused us to have stale data in the application.
That is when I figured out that I needed to read a bit more on Azure Caching if I was to use it efficiently.
When you enable caching on an Azure role each instance of that role will run a cache service using a portion of the memory (or all of it if it's a dedicated cache role). The cache services running on each instance of a single role are managed as a single cache cluster. Cache services can talk to each other and synchronize data but only inside the same cache cluster (same role). That is why enabling caching of many roles might not be the best thing to do.
Another thing to mention is that cache clusters can only be created on small role instances or bigger. The reason is that with extra small instance you only get 768MB of RAM which is pretty much all used up by anything you run on those instances.
Now the enable a cache cluster on your role go to the role property page on the Caching tab.
Here you will notice that I also enabled notifications which will allow us to efficiently use local caches later.
For more information on the different configuration options for cache clusters go here.
Now that we took care of the server side of caching configuration let's talk about the client side. Each instances of each roles inside the same cloud deployment can connect to a cache cluster. If you run only one cluster then you are guarantied to access the same cached data from whatever role you are inside your application (as long you have a valid configuration).
One nice feature we can enable in each role configuration is the local cache client. With this we can cache data locally in a role instance memory the data we recently fetched from the cache cluster for even faster access. Remember the Notification option we enabled on the server side? Using the configuration below in the Web.config or App.config of your role will ensure data stored in the local cache client gets updated whenever the cache server version of that data changes. Basically, the local cache client will invalidate data based on notifications received from the cache cluster.
For more information on client side configuration go here.
This post is only about an overview of the consideration of running multiple clusters versus a single one. Using Azure Caching there are a lot more configuration options you need to take a look at here. Also really important is how to use Azure Caching in your application.
I've spend a lot of time figuring out how all of this was working. I hope this post will help you with your learning experience.
Starting with Azure Caching is so easy that it can be a while before you fully understand the best way to use it. On a recent project my first tough was to enable caching on all the Cloud Service roles as co-located service. This was causing us problems.
First of all, been a developer I debug my application using the local Azure compute emulator. The emulator runs one cache service for each instances of roles with cache clusters. The application has 2 web roles and 1 worker role so when I start a debugging session with multiple instances per role I need a lot of memory to run everything. More importantly, cache clusters do not share cached data between each other. This caused us to have stale data in the application.
That is when I figured out that I needed to read a bit more on Azure Caching if I was to use it efficiently.
Understanding cache clusters
When you enable caching on an Azure role each instance of that role will run a cache service using a portion of the memory (or all of it if it's a dedicated cache role). The cache services running on each instance of a single role are managed as a single cache cluster. Cache services can talk to each other and synchronize data but only inside the same cache cluster (same role). That is why enabling caching of many roles might not be the best thing to do.
Another thing to mention is that cache clusters can only be created on small role instances or bigger. The reason is that with extra small instance you only get 768MB of RAM which is pretty much all used up by anything you run on those instances.
Now the enable a cache cluster on your role go to the role property page on the Caching tab.
Here you will notice that I also enabled notifications which will allow us to efficiently use local caches later.
For more information on the different configuration options for cache clusters go here.
Configuring roles to use cache clients
Now that we took care of the server side of caching configuration let's talk about the client side. Each instances of each roles inside the same cloud deployment can connect to a cache cluster. If you run only one cluster then you are guarantied to access the same cached data from whatever role you are inside your application (as long you have a valid configuration).
One nice feature we can enable in each role configuration is the local cache client. With this we can cache data locally in a role instance memory the data we recently fetched from the cache cluster for even faster access. Remember the Notification option we enabled on the server side? Using the configuration below in the Web.config or App.config of your role will ensure data stored in the local cache client gets updated whenever the cache server version of that data changes. Basically, the local cache client will invalidate data based on notifications received from the cache cluster.
For more information on client side configuration go here.
Other concerns
This post is only about an overview of the consideration of running multiple clusters versus a single one. Using Azure Caching there are a lot more configuration options you need to take a look at here. Also really important is how to use Azure Caching in your application.
Conclusions
I've spend a lot of time figuring out how all of this was working. I hope this post will help you with your learning experience.
Other useful links
- Caching in Windows Azure
- Optimization Guidance for Windows Azure Caching
- Best Practices for using Windows Azure Cache
Labels:
Architecture,
Azure,
Azure Caching,
Best Practices,
Caching,
Tips
Monday, July 22, 2013
Handling Azure Storage Queue poison messages
This post will talk about what to do now that we are handling poison messages in our Azure Storage Queues.
First, let's review what we'll done so far.
Messages that continuously fail to process will end up in the Error Queue. Someone asked me why do we need error queues at all? We could simply log the errors and delete the message right? Well, if you have a really efficient and pro-active DevOps team I suppose logging errors along with the original messages ought to be enough.
Someone will review why the message failed and if it was only a transient error then he could send the original message again in the queue.
We could also store failed messages into an Azure Storage Table.
Then we could simply monitor new entries in this table and act on it. Again, the original message should be stored in the table so we could send it again if we choose.
I think the best reason to use a queue for error message is if you want to have an administrative tools to monitor, review and re-send messages. In this case the queue mechanics let's you handle those messages like any other process using queues do.
For me one of the unpleasant side effect of using error queues is that all the queues in my system are now multiplied by two (one normal and one error queue). It's not too bad if your naming scheme is consistent but even then if you do operational work using a tool like Cerebrata Azure Management Studio or even from Visual Studio's Server Explorer you will feel overwhelmed by the quantity of queues.
Whatever you do, I suggest you always at least log failures properly. Later while debugging the issue you will be thankful to easily match the failure logs with the message who caused it.
First, let's review what we'll done so far.
Messages that continuously fail to process will end up in the Error Queue. Someone asked me why do we need error queues at all? We could simply log the errors and delete the message right? Well, if you have a really efficient and pro-active DevOps team I suppose logging errors along with the original messages ought to be enough.
Someone will review why the message failed and if it was only a transient error then he could send the original message again in the queue.
We could also store failed messages into an Azure Storage Table.
Then we could simply monitor new entries in this table and act on it. Again, the original message should be stored in the table so we could send it again if we choose.
I think the best reason to use a queue for error message is if you want to have an administrative tools to monitor, review and re-send messages. In this case the queue mechanics let's you handle those messages like any other process using queues do.
For me one of the unpleasant side effect of using error queues is that all the queues in my system are now multiplied by two (one normal and one error queue). It's not too bad if your naming scheme is consistent but even then if you do operational work using a tool like Cerebrata Azure Management Studio or even from Visual Studio's Server Explorer you will feel overwhelmed by the quantity of queues.
![]() |
| Managing queue messages with Cerebrata Azure Management Studio |
![]() |
| Managing queue messages with Visual Studio Server Explorer |
Whatever you do, I suggest you always at least log failures properly. Later while debugging the issue you will be thankful to easily match the failure logs with the message who caused it.
Friday, June 21, 2013
Windows Azure Storage Queue with error queues
Windows Azure Storage Queue are an excellent way to execute tasks asynchronously in Azure. For example a web application could let all the heavy processing to a Worker Role instead of doing it itself. That way requests will complete faster. Queues can also be used to decouple communication between two separated applications or two components of the same application.
One problem with asynchronous processing is what to do when the operation fails? When using synchronous patterns like a direct call we usually return an error code or a message or throw an exception. When we use queues to delegate the execution to another process we can't notify the originator directly. One thing we can do is to send a message back to the originator through another queue, like a callback. This is interesting when the process produce a result normally, an error in this case is only another kind of result.
Another way is to create an error queue (or dead letter queue) for poison messages where we put all messages that failed processing. This way we get a list of all the failing messages to review, find out what was the problem with them and figure out what to do about it. For example we can retry the message by moving it back to the main queue so it can be processed again.
Now, let see how we can implement an error queue using Windows Azure Storage Queue.
First we will initialize the queues. For each queue we also create an '<queuename>-error' queue.
Next we add a few messages with one that will cause the processing to fail (to simulate failures)
Finally the code to actually poll the queue for messages. Usually polling is done in an infinite loop but when no message is fetched it is a good practices to wait a while before polling again to prevent unnecessary transaction cost and IO (each call to GetMessages is 1 transaction). Depending on the need for the queue to react rapidly to new messages this may go between a few seconds for critical tasks to a few minutes for non critical tasks. Also I'm using a retry mechanism here, meaning that I'll try to process a message a few times before I really consider it in error (poison). If we don't delete a message after fetching it then after some time it goes back in the queue to be processed again. This mean all tasks we want to process using queues should be idempotent
First I'm fetching messages by batch of 8 in this case. In one transaction you can fetch between 1 and 32 messages. Also I set the visibilityTimeout to 10 seconds. This means the messages won't be visible to anyone during that time. Usually you want to set the visibility timeout based on how much time should be required to process all the messages of the batch. If we don't have the time to delete the messages from the queue before the timeout elapse another worker could fetch the message and start processing it again. So we should balance the time to process all the messages in one batch with how much time we want to allow between retries.
Next we process the message. If the processing is successful we simply return true so the message can be deleted from the queue. If processing failed we have two options, return false or throw an exception. I simply return false instead of throwing an exception most of the time when the failure is expected.
Finally, we check how many times we unsuccessfully tried to process the message and if we reached our limit (in this case 3 times). If we did then it's time to send that message to the error queue and delete it from the normal queue.
Next time we will look at how we want to handle the messages in the error queue. You can find this post here.
One problem with asynchronous processing is what to do when the operation fails? When using synchronous patterns like a direct call we usually return an error code or a message or throw an exception. When we use queues to delegate the execution to another process we can't notify the originator directly. One thing we can do is to send a message back to the originator through another queue, like a callback. This is interesting when the process produce a result normally, an error in this case is only another kind of result.
Now, let see how we can implement an error queue using Windows Azure Storage Queue.
Implementation of an error queue
First we will initialize the queues. For each queue we also create an '<queuename>-error' queue.
var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
var queueClient = storageAccount.CreateCloudQueueClient();
this.taskQueueReference = queueClient.GetQueueReference("task");
this.taskErrorQueueReference = queueClient.GetQueueReference("task-error");
this.taskQueueReference.CreateIfNotExists();
this.taskErrorQueueReference.CreateIfNotExists();
Next we add a few messages with one that will cause the processing to fail (to simulate failures)
this.taskQueueReference.AddMessage(
new CloudQueueMessage("Message " + DateTime.UtcNow.Ticks));
this.taskQueueReference.AddMessage(
new CloudQueueMessage("Message " + DateTime.UtcNow.Ticks));
this.taskQueueReference.AddMessage(
new CloudQueueMessage("Error " + DateTime.UtcNow.Ticks));
Finally the code to actually poll the queue for messages. Usually polling is done in an infinite loop but when no message is fetched it is a good practices to wait a while before polling again to prevent unnecessary transaction cost and IO (each call to GetMessages is 1 transaction). Depending on the need for the queue to react rapidly to new messages this may go between a few seconds for critical tasks to a few minutes for non critical tasks. Also I'm using a retry mechanism here, meaning that I'll try to process a message a few times before I really consider it in error (poison). If we don't delete a message after fetching it then after some time it goes back in the queue to be processed again. This mean all tasks we want to process using queues should be idempotent
private void PollQueue()
{
IEnumerable<CloudQueueMessage> messages;
do
{
messages = this.taskQueueReference
.GetMessages(8, visibilityTimeout: TimeSpan.FromSeconds(10));
foreach (var message in messages)
{
bool result = false;
try
{
result = this.ProcessMessage(message);
if (result) this.taskQueueReference.DeleteMessage(message);
}
catch (Exception ex)
{
this.Log(message.AsString, ex);
}
if (!result && message.DequeueCount >= 3)
{
this.taskErrorQueueReference.AddMessage(message);
this.taskQueueReference.DeleteMessage(message);
}
}
} while (messages.Any());
}
private bool ProcessMessage(CloudQueueMessage message)
{
if (message.AsString.StartsWith("Error")) throw new Exception("Error!");
return true;
}
First I'm fetching messages by batch of 8 in this case. In one transaction you can fetch between 1 and 32 messages. Also I set the visibilityTimeout to 10 seconds. This means the messages won't be visible to anyone during that time. Usually you want to set the visibility timeout based on how much time should be required to process all the messages of the batch. If we don't have the time to delete the messages from the queue before the timeout elapse another worker could fetch the message and start processing it again. So we should balance the time to process all the messages in one batch with how much time we want to allow between retries.
Next we process the message. If the processing is successful we simply return true so the message can be deleted from the queue. If processing failed we have two options, return false or throw an exception. I simply return false instead of throwing an exception most of the time when the failure is expected.
Finally, we check how many times we unsuccessfully tried to process the message and if we reached our limit (in this case 3 times). If we did then it's time to send that message to the error queue and delete it from the normal queue.
Next time we will look at how we want to handle the messages in the error queue. You can find this post here.
Thursday, May 9, 2013
Using Azure Blob Storage to store documents
Last time I wrote about Implementing a document oriented database with the Windows Azure Table Storage Service I was using the Table Storage Service to store serialized documents into an entity's property. While it is an easy way to store complex objects the table storage is usually meant to storage primitives like int, bool, date and simple strings. However there is another service in the Windows Azure Storage family who is better suited to store documents: the Blob Storage Service. The blob storage use the metaphor of files which is in essence documents.
Now I will adapt the Repository I did in my previous post to use the blob storage this time. I'll only walk through the changes I'm making here.
First, we need to create a CloudBlobContainer instance in the constructor. Please note that for blob storage container names are required to be lower-case.
Next for the Insert method, we no longer store the document in a property of the ElasticTableEntity object. Instead we want to serialize the document into the JSON format and upload it as a file to the blob storage and set the ContentType of that file to application/json. For the blob name (or path) the pattern I'm using looks like this: {document-type}/{partition-key}/{row-key}.
For the Load method we can get the blob name using the PartitionKey and RowKey then download the document from blob storage. In DownloadDocument I'm using a MemoryStream and StreamReader to get the serialized document as a string.
In the first List method we want to get all documents of the same partition. We can do that by directly using the ListBlobs method of CloudBlobDirectory. For the ListWithTasks method we still need to query the table storage first to know which documents contain at least one task. Then with the entities we'll know the RowKey value of those documents so we can simply call the Load method we just saw.
To update a document now we also need to serialize and upload the new version to blob storage.
Finally, deleting a document now requires us to call Delete on the CloudBlobContainer reference.
Using both Tables and Blobs Storage Services we can get the best of both worlds. We can query for document's properties with table storage and we can store documents larger than 64KB in blob storage. Of course now almost all operations on my Repository requires two calls to Azure. Currently those are done sequentially, waiting for the first call to complete before the doing the second call. I should fix that by using the asynchronous variants of storage service methods like the BeginDelete/EndDelete method pair on CloudBlobContainer.
I hope this post is giving you ideas on new and clever ways you can use the Windows Azure Storage Services in your projects.
- Document oriented database with Azure Table Storage Service
Now I will adapt the Repository I did in my previous post to use the blob storage this time. I'll only walk through the changes I'm making here.
Constructor
First, we need to create a CloudBlobContainer instance in the constructor. Please note that for blob storage container names are required to be lower-case.
public class ProjectRepository
{
private CloudTable table;
private CloudBlobContainer container;
public ProjectRepository()
{
var connectionString = "...";
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(connectionString);
var tableClient = storageAccount.CreateCloudTableClient();
this.table = tableClient.GetTableReference("Project");
this.table.CreateIfNotExists();
var blobClient = storageAccount.CreateCloudBlobClient();
this.container = blobClient.GetContainerReference("project");
this.container.CreateIfNotExists();
}
// ...
}
Insert
Next for the Insert method, we no longer store the document in a property of the ElasticTableEntity object. Instead we want to serialize the document into the JSON format and upload it as a file to the blob storage and set the ContentType of that file to application/json. For the blob name (or path) the pattern I'm using looks like this: {document-type}/{partition-key}/{row-key}.
public void Insert(Project project)
{
project.Id = Guid.NewGuid();
var document = JsonConvert.SerializeObject(project,
Newtonsoft.Json.Formatting.Indented);
var partitionKey = project.Owner.ToString();
var rowKey = project.Id.ToString();
UploadDocument(partitionKey, rowKey, document);
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = partitionKey;
entity.RowKey = rowKey;
entity.Name = project.Name;
entity.StartDate = project.StartDate;
entity.TotalTasks = project.Tasks.Count();
this.table.Execute(TableOperation.Insert(entity));
}
private void UploadDocument(string partitionKey, string rowKey, string document)
{
var filename = string.Format(@"project\{0}\{1}.json", partitionKey, rowKey);
var blockBlob = this.container.GetBlockBlobReference(filename);
using (var memory = new MemoryStream())
using (var writer = new StreamWriter(memory))
{
writer.Write(document);
writer.Flush();
memory.Seek(0, SeekOrigin.Begin);
blockBlob.UploadFromStream(memory);
}
blockBlob.Properties.ContentType = "application/json";
blockBlob.SetProperties();
}
Load
For the Load method we can get the blob name using the PartitionKey and RowKey then download the document from blob storage. In DownloadDocument I'm using a MemoryStream and StreamReader to get the serialized document as a string.
public Project Load(string partitionKey, string rowKey)
{
var blobName = string.Format(@"project\{0}\{1}.json", partitionKey, rowKey);
var document = this.DownloadDocument(blobName);
return JsonConvert.DeserializeObject<Project>(document);
}
private string DownloadDocument(string blobName)
{
var blockBlob = this.container.GetBlockBlobReference(blobName);
using (var memory = new MemoryStream())
using (var reader = new StreamReader(memory))
{
blockBlob.DownloadToStream(memory);
memory.Seek(0, SeekOrigin.Begin);
return reader.ReadToEnd();
}
}List
In the first List method we want to get all documents of the same partition. We can do that by directly using the ListBlobs method of CloudBlobDirectory. For the ListWithTasks method we still need to query the table storage first to know which documents contain at least one task. Then with the entities we'll know the RowKey value of those documents so we can simply call the Load method we just saw.
public IEnumerable<Project> List(string partitionKey)
{
var listItems = this.container
.GetDirectoryReference("project/" + partitionKey).ListBlobs();
return listItems.OfType<CloudBlockBlob>()
.Select(x => this.DownloadDocument(x.Name))
.Select(document => JsonConvert.DeserializeObject<Project>(document));
}
public IEnumerable<Project> ListWithTasks(string partitionKey)
{
var query = new TableQuery<ElasticTableEntity>()
.Select(new [] { "RowKey" })
.Where(TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey",
QueryComparisons.Equal, partitionKey),
TableOperators.And,
TableQuery.GenerateFilterConditionForInt("TotalTasks",
QueryComparisons.GreaterThan, 0)));
dynamic entities = table.ExecuteQuery(query).ToList();
foreach (var entity in entities)
yield return this.Load(partitionKey, entity.RowKey);
}
Update
To update a document now we also need to serialize and upload the new version to blob storage.
public void Update(Project project)
{
var document = JsonConvert.SerializeObject(project,
Newtonsoft.Json.Formatting.Indented);
var partitionKey = project.Owner.ToString();
var rowKey = project.Id.ToString();
UploadDocument(partitionKey, rowKey, document);
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = partitionKey;
entity.RowKey = rowKey;
entity.ETag = "*";
entity.Name = project.Name;
entity.StartDate = project.StartDate;
entity.TotalTasks = project.Tasks != null ? project.Tasks.Count() : 0;
this.table.Execute(TableOperation.Replace(entity));
}
Delete
Finally, deleting a document now requires us to call Delete on the CloudBlobContainer reference.
public void Delete(Project project)
{
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = project.Owner.ToString();
entity.RowKey = project.Id.ToString();
entity.ETag = "*";
this.table.Execute(TableOperation.Delete(entity));
this.DeleteDocument(entity.PartitionKey, entity.RowKey);
}
public void Delete(string partitionKey, string rowKey)
{
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = partitionKey;
entity.RowKey = rowKey;
entity.ETag = "*";
this.table.Execute(TableOperation.Delete(entity));
this.DeleteDocument(partitionKey, rowKey);
}
private void DeleteDocument(string partitionKey, string rowKey)
{
var blobName = string.Format(@"project\{0}\{1}.json", partitionKey, rowKey);
var blockBlob = this.container.GetBlockBlobReference(blobName);
blockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
}
Conclusion
Using both Tables and Blobs Storage Services we can get the best of both worlds. We can query for document's properties with table storage and we can store documents larger than 64KB in blob storage. Of course now almost all operations on my Repository requires two calls to Azure. Currently those are done sequentially, waiting for the first call to complete before the doing the second call. I should fix that by using the asynchronous variants of storage service methods like the BeginDelete/EndDelete method pair on CloudBlobContainer.
I hope this post is giving you ideas on new and clever ways you can use the Windows Azure Storage Services in your projects.
See also
- Using Azure Table Storage with dynamic table entities- Document oriented database with Azure Table Storage Service
Tuesday, April 9, 2013
Document oriented database with Azure Table Storage Service
What is a Document database?
A document store or document oriented database like RavenDB is a kind of NoSQL database where we store semi structured data in documents and use a key to retrieve existing documents.
The difference with a relational database is that a complex data structure needs to be represented by entities and relations in a RDMS which means using many tables, joints and constraints. In a document database the whole graph of entities is stored as a single document. Of course we still need to handle some relations between documents or graphs of entities. This is done by storing other documents key inside the document and acting like a foreign keys.
Another way to see documents is to think that all tables related together with a delete cascade constraints on the relations are part of the same document. If a piece of data from a table can only exists if related data from another table also exists it means both should be part of the same document.
The concept of document relates well with Aggregates of Domain Driven Design.
Implementing a document database with the Azure Table Storage Service
Looking at RavenDB I was wondering if it was possible to use similar patterns but with the Windows Azure Table Storage Service instead of the file system as RavenDB is using.
A good storage format for our documents is the JSON representation. Using a serialization library like Json.Net it will be easy to convert our data in JSON.
Using the Table Storage Service also requires us to provide a PartionKey for our document, in a typical multi-tenant database we could use this to 'partition' the data per tenant.
A document in a document store is easy to retrieve when we know the key to fetch it directly, but sometime we don't have that information. We might also want to query documents using a filter expression. In a relational database filtering in a query is easy but in a document store it requires a bit more efforts. RavenDB let us define indexes we could use to filter documents in queries. With the Table Store Service we can use additional properties to store information we want to filter on, acting like the indexes.
In order to create a very light weight Document Store with the Table Storage Service I will use my ElasticTableEntity class from a previous post.
First let me show you my domain entities for this demo. A simple Project class which may have many Tasks associated to it.
public class Project
{
public Guid Owner { get; set; }
public Guid Id { get; set; }
public string Name { get; set; }
public DateTime StartDate { get; set; }
public int Status { get; set; }
public List<Task> Tasks { get; set; }
}
public class Task
{
public string Name { get; set; }
public bool IsCompleted { get; set; }
}
Now let's take a look a typical Repository implementation for the Projects. You will need both WindowsAzure.Storage and Newtonsoft.Json packages from NuGet for this part.
public class ProjectRepository
{
private CloudTable table;
public ProjectRepository()
{
var connectionString = "...";
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(connectionString);
var client = storageAccount.CreateCloudTableClient();
this.table = client.GetTableReference("Project");
this.table.CreateIfNotExists();
}
public void Insert(Project project)
{
project.Id = Guid.NewGuid();
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = project.Owner.ToString();
entity.RowKey = project.Id.ToString();
entity.Document = JsonConvert.SerializeObject(project,
Newtonsoft.Json.Formatting.Indented);
// Additional fields for querying (indexes)
entity.Name = project.Name;
entity.StartDate = project.StartDate;
entity.TotalTasks = project.Tasks.Count();
this.table.Execute(TableOperation.Insert(entity));
}
public IEnumerable<Project> List(string partitionKey)
{
var query = new TableQuery<ElasticTableEntity>()
.Select(new [] { "Document" })
.Where(TableQuery.GenerateFilterCondition("PartitionKey",
QueryComparisons.Equal, partitionKey));
dynamic entities = table.ExecuteQuery(query).ToList();
foreach (var entity in entities)
{
var document = (string)entity.Document.StringValue;
yield return JsonConvert.DeserializeObject<Project>(document);
}
}
public IEnumerable<Project> ListWithTasks(string partitionKey)
{
var query = new TableQuery<ElasticTableEntity>()
.Select(new [] { "Document" })
.Where(TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey",
QueryComparisons.Equal, partitionKey),
TableOperators.And,
TableQuery.GenerateFilterConditionForInt("TotalTasks",
QueryComparisons.GreaterThan, 0)));
dynamic entities = table.ExecuteQuery(query).ToList();
foreach (var entity in entities)
{
var document = (string)entity.Document.StringValue;
yield return JsonConvert.DeserializeObject<Project>(document);
}
}
public Project Load(string partitionKey, string rowKey)
{
var query = new TableQuery<ElasticTableEntity>()
.Select(new [] { "Document" })
.Where(TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey",
QueryComparisons.Equal, partitionKey),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey",
QueryComparisons.Equal, rowKey)));
dynamic entity = table.ExecuteQuery(query).SingleOrDefault();
if (entity != null)
{
var document = (string)entity.Document.StringValue;
return JsonConvert.DeserializeObject<Project>(document);
}
return null;
}
public void Update(Project project)
{
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = project.Owner.ToString();
entity.RowKey = project.Id.ToString();
entity.ETag = "*";
entity.Document = JsonConvert.SerializeObject(project,
Newtonsoft.Json.Formatting.Indented);
// Additional fields for querying (indexes)
entity.Name = project.Name;
entity.StartDate = project.StartDate;
entity.TotalTasks = project.Tasks.Count();
this.table.Execute(TableOperation.Replace(entity));
}
public void Delete(Project project)
{
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = project.Owner.ToString();
entity.RowKey = project.Id.ToString();
entity.ETag = "*";
this.table.Execute(TableOperation.Delete(entity));
}
public void Delete(string partitionKey, string rowKey)
{
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = partitionKey;
entity.RowKey = rowKey;
entity.ETag = "*";
this.table.Execute(TableOperation.Delete(entity));
}
}
We could refactor the code to reduce duplication but the point was to show you how to dynamically create a Document property to store the actual serialized document and how to handle the basic CRUD operations. You can also see how to dynamically add other properties to store extra information on the document. This is useful for the LoadWithTasks method which fetch only Projects with at least one Task on it.
Finally let's take a look at a few examples on how to use the ProjectRepository itself (in LinqPad in this case)...
private void Insert()
{
var repo = new ProjectRepository();
var project = new Project()
{
Owner = Guid.Parse("8ad82668-4b08-49c9-87ef-80870bfb4b85");
Name = "My new project",
StartDate = DateTime.Now,
Status = 4,
Tasks = new List<Task>()
{
new Task { Name = "Task 1", IsCompleted = true },
new Task { Name = "Task 2" }
}
};
repo.Insert(project);
}
private void List()
{
var repo = new ProjectRepository();
var projects = repo.List("static");
projects.Dump();
}
private void Load()
{
var repo = new ProjectRepository();
var project = repo.Load("8ad82668-4b08-49c9-87ef-80870bfb4b85", "c7d5f59c-72da-48de-83ca-265d8609ec02");
project.Dump();
}
private void Update()
{
var repo = new ProjectRepository();
var project = repo.Load("8ad82668-4b08-49c9-87ef-80870bfb4b85", "c7d5f59c-72da-48de-83ca-265d8609ec02");
project.Name = "Modified name " + DateTime.Now.Ticks;
repo.Update(project);
}
private void Delete()
{
var repo = new ProjectRepository();
var project = repo.Load("8ad82668-4b08-49c9-87ef-80870bfb4b85", "c7d5f59c-72da-48de-83ca-265d8609ec02");
repo.Delete(project);
}
private void DeleteDirectly()
{
var repo = new ProjectRepository();
repo.Delete("8ad82668-4b08-49c9-87ef-80870bfb4b85", "c7d5f59c-72da-48de-83ca-265d8609ec02");
}
What I've shown you here is really basic and we do have some limitations like the fact that a serialized document can't be bigger than 64KB in size and we are limited to 251 extra properties (or indexes). Still, it is a good start for a prototype of a document store.
I'm currently working on a more self-contained library to help me use the Azure Table Storage Service as a Document Store. More on this in a future post.
If you want you can grab all the code (Gist) on GitHub here and here.
See also
- Using Azure Table Storage with dynamic table entities- Using Azure Blob Storage to store documents
Tuesday, March 12, 2013
Using Azure Table Storage with dynamic table entities
I've been working with Windows Azure for a few months now and I was trying to figure out a way to use the Azure Table Storage Service with POCOs and complex types rather than only classes inheriting from the TableEntity base class. Turns out that the only thing the CloudTableClient cares about is the ITableEntity interface. DynamicTableEntity also implements ITableEntity but is only used for querying and updating entities. You can see it in action in the example on how to query only a subset of an entity's properties.
So I started to wonder if it was possible to create class that implements ITableEntity and offer the dynamic features of an ExpandoObject. After a bit of hacking around in LinqPad I have this solution.
In this snippet I also implemented the ICustomMemberProvider which is part of the LinqPad extensions API for queries (more on this here). In Visual Studio we'll need to remove that code.
We can now use the ElasticTableEntity class like this:
Please note that you need to use the dynamic keyword to be able to define properties dynamically. You can also use the entity indexer like I did with the LastName property.
The ElasticTableEntity allows us to define properties at run time which will be added to the table when inserting the entities. Tables in the Azure Table Storage have flexible schema so we are free to store entities with different properties as long a we respect some limitations:
- Using Azure Blob Storage to store documents
So I started to wonder if it was possible to create class that implements ITableEntity and offer the dynamic features of an ExpandoObject. After a bit of hacking around in LinqPad I have this solution.
In this snippet I also implemented the ICustomMemberProvider which is part of the LinqPad extensions API for queries (more on this here). In Visual Studio we'll need to remove that code.
We can now use the ElasticTableEntity class like this:
Please note that you need to use the dynamic keyword to be able to define properties dynamically. You can also use the entity indexer like I did with the LastName property.
| Result | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||
| Result with projection | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||
The ElasticTableEntity allows us to define properties at run time which will be added to the table when inserting the entities. Tables in the Azure Table Storage have flexible schema so we are free to store entities with different properties as long a we respect some limitations:
- Entities can have no more than 252 different properties (that's for the Table)
- An Entity's data can be up to 1 MB in size
- A property must be one of the following types : byte[], bool, DateTime, double, Guid, int, long or string
- A property value can be up to 64 KB in size (for string and byte array)
- A property name is case sensitive and can be no more than 255 characters in length
You can store about any kind of data as long as it is one of the supported data type. You could also encode other kind of date type in a byte array or a string (like a json document). Just be careful to always stick to one data type for a property (yes, we can store like int, bool and string in the same column using different entities!)
That's it for now. Next time I'll show you how to use the Windows Azure Table Storage Service as a document-oriented database with the ElasticTableEntity.
See also
- Document oriented database with Azure Table Storage Service- Using Azure Blob Storage to store documents
Subscribe to:
Posts (Atom)


















