Archive for the ‘ F# ’ Category

Giving your Windows Service lots of time to finish (F#)

Normally, your Windows services should finish within seconds of Windows telling them that it’s time to quit. However, in some (hopefully rare) cases, you might need several minutes to make a clean exit, much to the disgust of your users. Since ServiceBase.RequestAdditionalTime may not cut it, you must follow the example of some Microsoft services, like TrustedInstaller and Windows Update, who use a different approach.

Read more

Minimal F# Windows Service

I’ll soon publish a post about how to give your Windows Service lots of time to finish cleanly (by the way, needing all that time is something you should avoid unless you have an exceptionally good excuse to do so). Before doing so, I thought it a good idea to write this post with a minimal “Hello World”-ish service, which can be compared to an equally minimal service in terms of payload, but with all the ceremony needed to make Windows grant it a big chunk of time to finish. Read more

F# SQLProvider and SQLite now work better together

If you used SQLite with SQLProvider in the recent past, just adding the System.Data.SQLite.Core NuGet package wasn’t enough. Due to SQLProvider wanting to access databases already during edit and compile time, you also had to copy subdirectories with System.Data.SQLite native interop DLLs for x64 and x86 to a certain directory. This operation had to be repeated when you updated or restored the NuGet package.

Starting with the just (yesterday) released System.Data.SQLite.Core Version 1.0.104, this is no longer needed. Joe Mistachkin, the package author, has added a configuration file especially for us F# type provider users. It allows the interop DLLs to be loaded during edit/compile time without prior copying (see this feature request for details). In short: to use SQLProvider with SQLite now, just add the System.Data.SQLite.Core NuGet package, and it will just work.

A big thanks to Joe Mistachkin for this F# type provider adaption!

(The SQLProvider documentation for SQLite will be updated to reflect this change.)

F# SQLProvider+SQLite on the Raspberry Pi

fspi3Both the SQLite database engine and the Raspberry Pi are lean, mean little things, so they are a perfect match for each other.

Using F# under Mono, we get a high level way to tap into the low cost power of the Pi (though everything in this post should work on anything else running a Debian based Linux), and the SQLProvider type provider does precisely what type providers are supposed to: it combines the convenience of data access in a dynamic language with the type safety of a static one.

In this post, we’ll create an SQLite database and a minimal F# project that reads from and writes to it, on a Windows PC. Once we’ve got that running, we’ll get it running on a Raspberry Pi as well (I’ll assume that you’ve got Raspbian up and running on a Pi).

Read more

Mpir.NET – NuGet package for high performance large integers

mpirnetlogoMpir.NET is a .NET wrapper for the high performance multi precision integer library MPIR, which in turn is a Windows friendly fork of GMP.

As noted in my previous post, High performance large integers in .NET, it would be highly desirable to have a large integer library that combines the speed of MPIR with the usability of Emil Stefanov’s “GMP for .NET”. The obvious way of doing this, would be to make a fusion of Sergey Bochkanov’s MPIR wrapper for .NET, X-MPIR, and GMP for .NET. Since both Stefanov and Bochkanov have released their code under the LGPL license, this turned out to be possible with a couple of modifications to both code bases. Read more

High performance large integers in .NET

BigNumBench01ThumbWhen you’re playing with the sort of big integers used in, for example, cryptography, 64 bits just aren’t enough. In this post, I’ll compare and benchmark some of the alternatives that take .NET numerics beyond 64 bits. The benchmark results turn out to be a tale of the unexpected, to paraphrase Roald Dahl.

The test case is RSA signature verification, which uses the formula c=me mod n, where c, m and n are the same size as the RSA key (the public RSA key consists of n and e), typically 1024, 2048 or 3072 bits, while e usually is a mere 32 bits (usually holding the value 17 or 65537). In the test, I’ll use a 1024 bit key, because that conveniently enables me to reuse some F# code I wrote to verify RSA signed data written by EU digital tachographs. Read more

MissingMethodException when migrating from F# 3.0 to F# 3.1

If you retarget an F# 3.0 project to F# 3.1 in Visual Studio 2013, you might end up with a System.MissingMethodException when you rebuild and run your project. This is because you need to manually update the App.config file after you retarget to F# 3.1.

Read more

Adding binding options to Morozov’s WPF MVC framework

Inspired by Dmitry Morozov’s MVC series (among the best F# articles I’ve ever seen!), I wanted to extend his data binding DSL to allow the setting of binding properties individually for each model property. The end result I was after, was to be able to write binding expression quotations like

  view.Control.LastName.Text <- model.LastName |> UpdateSourceOnChange
  view.Control.Bitness.Text  <- model.Bitness |> string |> Mode BindingMode.OneTime

Read more

Simple F# producer-consumer pattern

The producer-consumer pattern allows a producer thread to generate data (e.g loaded from disk or read from a web site) and put in a queue, concurrently with a thread that consumes the data from the queue.

A typical application of this is the processing of multiple disk files. When done in a single threaded, sequential manner, the CPU mostly sits idle while waiting for the disk during file load. The disk, in turn, doesn’t do anything useful while waiting for the CPU to finish processing a file before the next one is loaded. Using the producer-consumer pattern, the files are loaded from the disk and queued while the CPU processes files already in the queue, speeding up the processing of the second and all subsequent files. To boot, you get the possibility of running multiple producers and multiple consumers concurrently, as well as the smug feeling that goes with it. Read more

Detecting the CPU architecture in F#

If you google for methods of detecting the cpu architecture, you’ll find lots of pages about how to use WMI and ManagementObjectSearcher. This is an immensely slow operation which takes more than a second to complete (at least when I’ve timed it on a 2.8 GHz 4 core machine), so don’t use it. A quicker method is to use PInvoke to call the GetNativeSystemInfo function of the Windows API directly.

Read more

.NET RSAParameters endianness

Ever had to access or set the RSAParameters structure’s fields? Well, if you do, you will find that the MSDN documentation on them doesn’t specify their endianness (byte order). Googling isn’t too helpful either, as I found different sources claiming different endianness. So, I wrote a little test in F# that shows that the RSAParameters fields are indeed big-endian.

Read more

F# values, functions and a little bit of both

Judging from a few questions on stackoverflow and hubFS, programmers new to F# sometimes confuse value assignments with functions that don’t take arguments. This confusion is easily straightened out. In fact, it’s so easily straightened out, that it’s not really enough for a blog post. Thus, we spice things up a bit by letting the value assignment assign a function value, and throw in closures and something about the practical differences between stack and heap values for good measure.

Read more

F# ASCII85 module

Sometimes, you need to store binary data in places where only text data is welcome, such as XML files. The quick & dirty solution is to code your data into hexadecimal strings, which you can easily get away with when storing a few bytes. However, sometimes you want better space efficiency and this is where ASCII85 comes to the rescue.

Read more