A proper introduction to NetMQ requires more than one post and is well beyond the format of this series of the posts. Instead, in a spirit of the series, this is an awareness raising post, to bring additional visibility to the great library.

NetMQ is a managed .NET port of ZeroMQ library. The idea of the both libraries is not to directly compete with the messaging solutions like RabbitMQ, ActiveMQ or NServiceBus, which provide rich high level feature set out of the box; but to provide a relatively low level API which allows to build complex solutions tailored to the specific business and functional needs.

Unlike the mentioned solutions, NetMQ does not include a central server or broker. NetMQ is based on a socket idea. Each side of communication require to open a socket and use it for communication. NetMQ, following ZeroMQ, supports many different type of sockets and each socket type has a unique behavior.

The simplest pair of sockets is RequestSocket/ResponseSocket. These two sockets, when connected to each other, allows to build synchronous client server communications.

Server code example

using (var responseSocket = new ResponseSocket("@tcp://*:5555"))
                while (true)
                    // receive a request message
                    var msg = responseSocket.ReceiveFrameString();

                    Console.WriteLine("Request received: " + msg);

                    // send a canned response
                    responseSocket.SendFrame("Response for " + msg);

Client code example

using (var requestSocket = new RequestSocket(">tcp://localhost:5555"))
                var message = requestSocket.ReceiveFrameString();
                Console.WriteLine("requestSocket : Received '{0}'", message);


Samples demonstrate creation of the sockets for server and client and sending the request/response between the applications.

The real power of NetMQ comes from the ability to craft your own protocol using multi-frame messages and from the variety of the available sockets. They include async sockets, pub/sub sockets, in-proc sockets. These features differentiate ZeroMQ and NetMQ and allows to build very complex solutions, however they bring lots of complexity and require some learning. ZeroMQ guide may be a good start, even if you will end up using NetMQ – it helps to understand main principles and pattern.

Exploring .NET Open Source ecosystem: simplifying object to object mapping with AutoMapper

Well-designed software applications are typically layered to provide a maximum isolation between the logical parts of application and often require transformation of data from layer to layer.

AutoMapper facilitates such transformations providing a convention-first approach for mapping an object of one type into an object of some other type. Convention-first, in this case, means zero configuration for the cases when the source and the target classes has properties with the same names. For other cases, AutoMapper provides a rich API to customize mapping configuration.

Use of AutoMapper eliminates routine, repetitive and error prone list of copy instructions. Automapper allows to define a mapping, which can be reused in the code multiple times to do a one line transformation. Common use cases for the Automapper library include mapping between Data Transfer Model and Model or between Model and ViewModel.

When two types are aligned (properties which are required to be transferred have the same names), configuration is as simple as the following one line of code


The usage is simple as well

OrderDto dto = Mapper.Map(order);

While the usage code stays the same all the time, configuration may become more complicated. For example, the following code demonstrates an action which will be called before the mapping and a custom mapping rule for one of the class members.

                          .BeforeMap((order, dt)=> {order.DateTime = DateTime.UtcNow;})
                          .ForMember(o=>o.CustomerName, x=>x.UseValue("admin")));

AutoMapper supports automatic flattening – mapping between a property and a class hierarchy. For example, the value of a Name property of an object referenced by a Customer property of Order may be automatically mapped (if compatible) to a CustomerName property of OrderDto.

Any time, when conventions do not satisfy the needs of the application, they can be replaced by the explicit configuration.

Exploring .NET Open Source ecosystem: making Unit Tests more robust with Moq

To continue the topic of unit testing, started in the previous post, let’s review Moq, a mocking library for .NET.

While the integration tests are easier to develop, and allows to reach higher code coverage in a shorter period of time, in long term low level, granular unit tests are more valuable. They allow to control the system behavior more precisely and catch any deviation as soon as it occurs. From other side, unit tests require more work and, sometimes, application’s architecture makes such tests extremely difficult to write. The common examples of such architectures are monolithic applications or tightly coupled components.

If architecture is fully monolithic – there is no magic, tools will be able to resolve it for the developer. However, if components are coupled but can be instantiated individually, using some variant of Inversion of Control (IoC) pattern, Moq can help with testing of such components. The main idea of Moq is to allow developer to use a mock object, configured to behave in a predictable way, instead of the real object. For the code consuming the mock object, there will be no difference between the mock and the real object.

For example, an application for parsing logs may include a log reader, log analyzer and log visualizer components. If the log reader implements an ILogReader interface, the log analyzer’s constructor can accept instance of the reader as a parameter. In this case, the log reader can be mocked to provide test input into the analyzer, instead of reading files from the disk.

Typical Moq usage pattern include three steps: create a mock, setup the mock behavior, call tested code, with provided mock object. Here are these stems, using the same Log Parser example:

 public void Test_one_entry()
     var logReader = new Mock();
     logReader.Setup(x => x.NextEntry())
         .Returns("2016-05-12 12:01pm [VRB] Initializing visual subsystem...");
     var logAnalyzer = new LogAnalyzer(logReader.Object);
     var entry = logAnalyzer.AnalyzeNextEntry();
     entry.Message.Should().Be("Initializing visual subsystem...");

The same as with the FluentAssertions library, possibilities of Moq go far beyond this simple example. Moq object can be configured to input parameters

logReader.Setup(x => x.SkipEntriesAndGet(10))
           .Returns("2016-05-12 12:01pm [VRB] Initializing visual subsystem...");

Or even use placeholder like It.IsAny() when the argument is not important. Moq can handle async calls, callbacks and many other scenarios. Check it out at github.com/Moq/moq4/

Exploring .NET Open Source ecosystem: simplifying unit testing with FluentAssertions

Unit Tests are useful. Almost every developer, with a least some experience in commercial software development, will agree with this statement, in general. Unfortunately, far less developers will agree that Unit Tests are useful enough to spend additional time and efforts to write them. In this situation, any tools or practices, which can simplify unit testing are welcome.

FluentAssertion library is an example of such tool. Goal of this library is to simplify the assertion part of the unit test by providing a more expressive way to define the asserts and by reporting the assertion failures in a friendly way.

Here is a basic example

double result = 19.99;
result.Should().BeInRange(99.99, 199.99, "because we filtered values for this range");

It demonstrates an expressive, highly readable syntax of FluentAssertion. This test will, obviously, fail and it will do it with the following friendly message.

Expected value to be between 99.99 and 199.99 because we filtered values for this range, but found 19.99

As it is expected from the any assertion library, FluentAssertions can handle exceptions, combinations of conditions and validating complex objects. It can also can validate metadata, events, XML and execution time.

Here is a more complicated sample where a Customer object compared to a CustomerDTO. All properties existing in both classes should match, all nested objects should be ignored, as well as all public fields

                options => options.ExcludingFields()

This is just a short list of the library features, and to not repeat the documentation, visit FluentAssertions web site.

Exploring .NET Open Source ecosystem: manipulating HTML with HtmlAgilityPack

Based on my experience, the need of parsing and manipulating HTML appearing surprisingly often. It may be required to clean a HTML file created by tools like Word or FrontPage (these tools are great for the end users, but inject lots of unnecessary information). Or parsing a webpage, or trying to construct a HTML page programmatically.

In all these cases, HtmlAgilityPack may be a handy tool. It allows to load, parse and modify a “real-world” HTML – HTML files which are not necessary clean and well formatted. Even better, for the parsed files, it builds a XML-like DOM which supports XPath and LINQ.

It is easy to learn and the simple example looks like

var doc = new HtmlDocument();
var docNode = doc.DocumentNode;
var content = docNode.Descendants()
                .First(x => x.GetAttributeValue("class", "")

This sample code returns content for the first item with the “icon” class.

This is a simple, but very useful library, so check it out at htmlagilitypack.codeplex.com

Exploring .NET Open Source ecosystem: handling exceptions with Polly

It is unusual for the modern applications to be disconnected from the outside world. Remote servers, distributed databases, external services – all those technologies enrich the application. However, networks split, databases crush and servers reboot. When introduced without an adequate control, these services may become an additional point of failure.

Polly is a .NET library that helps to handle transient errors such us described above. In the .NET applications these issues usually lead to exceptions and Polly provides a way to define exception handling policies.

For example, HttpClient may throw HttpRequestException when network is temporary unavailable. In this case Polly can be configured to retry the request. Here is how.

First, we need to install Poly NuGet package by executing the following commang in the Package Management Console

PM> Install-Package Polly

Next step is to define a policy: provide a list of exceptions to handle and the policy behavior

var policy = Polly.Policy.Handle()
                         .WaitAndRetryAsync(5, i=>TimeSpan.FromSeconds(i));

This sample policy instructs Polly to retry failed operation five times, waiting before the each retry with the increasing time interval. After the five retries any new exception will be re-thrown to the caller.

When policy is defined, it can be used any number of times to execute similar operations

var result = await policy.ExecuteAsync(() => httpClient.GetStringAsync("http://lunarfrog.com/blog"));

As you can see from the example, Polly supports async/await semantics, but it also can be called in a fully synchronous way, if needed. Policies can be nested and support filtering on the exception properties.

In addition to the simple Retry, WaitAndRetry and RetryForever patterns, Polly also support mode advanced patterns such as Circuit Braker. This patterns allows to handle the situations of real (non-transient) failures and prevent the system from spending cycles on useless retries.

Current version of Polly supports .NET 3.0 to 4.6 and .NET Core / .NET Standard support coming shortly.

.NET OSS community and Open Source project of the week

.NET community was never known as a strong proponent of the open source software. Traditionally, it gravitates towards the full stack solutions provided by Microsoft or well-known vendors of the commercial components.

With introduction of NuGet, open sourcing .NET, Roslyn and intentional push to Open Source from Microsoft, situation seems changing. However, Open Source is not a way of life for the majority of the .NET developers. To verify that, I did a couple of OSS-related presentations for the local .NET user groups. I was not (unfortunately) surprised to see that besides some really well-known libraries, not many OSS projects are known for the attendees.

To spread the good words about existing .NET Open Source projects, from time to time I am going to introduce one OSS library which I found useful.

Project of the week

For the first week, instead of the library I’d like to introduce two initiatives – First Timers Only and Up For Grabs. Both projects aim to help to find a project for the first contribution and based on the tags assigned by the repository maintainers. The idea is to suggest a simple isolated feature/defect fix to help newcomers to learn the submission process.

If you are a project maintainer, check your repository and mark the appropriate stories with tags. If you never contributed to an open source project – it’s time to try!

Will talk next week.

How to save WriteableBitmap as PNG file, solution for a UserVoice request

API to save WriteableBitmap to PNG is one of the top UserVoice requests for Universal Windows Platform. While such API does not exist in the same way as WriteableBitmap SaveJpeg() extension method, there are two workarounds.

If you are allowed (or willing) to use a third-party library, WriteableBitmapEx provides ability to save bitmap as PNG.

In case you’d like to stay free of external libraries, you can use the following solution, based on the UWP APIs:

WriteableBitmap bitmap = new WriteableBitmap(50, 50);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
                                 new Uri("ms-appx:///Assets/MyBitmap.jpg"));

using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
    await bitmap.SetSourceAsync(fileStream);

FileSavePicker picker = new FileSavePicker();
picker.FileTypeChoices.Add("PNG File", new List { ".png" });
StorageFile destFile = await picker.PickSaveFileAsync();

using (IRandomAccessStream stream = await destFile.OpenAsync(FileAccessMode.ReadWrite))
    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    Stream pixelStream = bitmap.PixelBuffer.AsStream();
    byte[] pixels = new byte[pixelStream.Length];
    await pixelStream.ReadAsync(pixels, 0, pixels.Length);

    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                (uint) bitmap.PixelWidth, (uint) bitmap.PixelHeight, 96.0, 96.0, pixels);
    await encoder.FlushAsync();

This code creates WritebaleBitmap from a JPG image loaded from the assets. Than it requests a name for the converted file and uses BitmapEncoder to convert bitmap to PNG and save it.

Usage of Clipboard in Universal Windows apps

Clipboard access was one of the features missed during the transition of Windows Phone 8.x development platform from Silverlight to Windows Runtime. As the result, it was one of the most requested feature on the Windows Platform UserVoice site.

Universal Windows Platform bring this feature back for both desktop and phone apps and it is facilitated by Clipboard class.

Adding information to the clipboard require two steps – creation of DataPackage object and assignment of this object as a clipboard content.

var dataPackage = new DataPackage();

DataPackage supports various data formats, including plain text, HTML, links and images. The same DataPackage class is used to share data between the Universal apps, and in this scenario, it allows setting a data provider, callback function that will be called when data is requested. In case of the clipboard, data provider function can be setup too, but it will be called as soon as the Clipboard.SetContent() function is invoked.

The same Clipboard class is used to retrieve the clipboard content.

DataPackageView dataPackageView = Clipboard.GetContent();
if (dataPackageView.Contains("Text"))
    string text = await dataPackageView.GetTextAsync();

In this case GetContentMethod returns an instance of DataPackageView class, a read-only DataPackage view. Available types can be evaluated using Contains function (as in the example) or by iterating AvailableFormats property.

That is it – Clipboard API is simple and straightforward to use; it allow to share information not only between the Universal apps but also between them and the Classic desktop apps.

What is new for file system access in Windows 10

With the upcoming release of Windows 10, Windows developers will get an access to Universal Windows Platform (UWP) that extends WinRT platform with new capabilities and UIs. Previously, I described several places on the local drive and external storages simply accessible by modern Windows apps. UWP apps inherit all that capabilities but also can be enhanced by utilizing new folders.

Publisher Cache Folder

From now, apps from the same publisher can share files between each other using ApplicationData.Current.GetPublisherCacheFolder. This folder shall not be used for critical data, as it is not backedup or roamed and at any moment can be cleared by the user. Before use, shared folder shall be registered in the app manifest. Files created in the cache stays there until at least one publisher app still installed.

Files are physically located in %user_folder%\AppData\Local\Publishers\%PublisherID%\%folder%

Local Cache Folder

Another folder, ApplicationData.Current.LocalCacheFolder, allows caching files locally, for particular app, without including them into backup or roaming operations. Files are physically located in %user_folder%\AppData\Local\Publishers\%ID%\LocalCache\

Bonus: Showing folder in File Explorer

New Windows.System.Launcher method - LaunchFolderAsync(IStorageFolder) – allows launching of File Explorer with the content of the provided folder. Overloaded version of LaunchFolderAsync accepts second parameter which let developers to control Explorer’s display options.

Universal Windows Platform includes multiple new and improved UI controls - you can join Canadian Dev Connection blog to learn more. Also, don’t forget to join Windows 10 release celebration at a Microsoft Store near you!

Next posts Previous posts