Archive for September, 2012

26 Sep 2012

Using Source Code Pro Font With Visual Studio

7 Comments Uncategorized

You may or may not be aware but Adobe released a new font a couple of days ago. Who cares right, you are not a graphics designer, you sling code for a living. Well you should care about this font. Adobe has released a font that has been optimized for coding. And I am here to tell you it is a beautiful font.

I have been a huge fan on Consolas ever since it was released by Microsoft. And I was excited to see a new alternative font from Adobe. And to make it even more interesting, Source Code Pro is open source font on GitHub and they are accepting pull requests for it. In other words, it can only get better from here.

Given that I am always looking for the perfect coding environment, I decided to install Source Code Pro, and set it up in Visual Studio. To get started you need to do the following:

  1. Download the latest release from their GitHub page.
  2. Extract the fonts to a folder.
  3. Navigate to the folder that contains the Source Code Pro fonts.
  4. Select the fonts. (I selected all the ones with .ttf to install)
  5. Right-click the selected fonts and choose “Install.”
  6. The fonts are now installed and will appear in the font menus of your applications.
  7. Open Visual Studio.
  8. Go to Tools > Options > Fonts and Colors.
  9. Change the Font to Source Code Pro in the drop down.
  10. Restart Visual Studio.
  11. Now you are ready to start using Source Code Pro.

Here are examples of Consolas (on the top) side-by-side with Source Code Pro (on the bottom).


You can click on each image and open them up in new tabs in your browser, and switch back and forth to see the subtle changes between the two. It should also be noted that Consolas is running at 10 pt and Source Code Pro is running at 9 pt, which seem to produce the same size characters on my machine.

I am actually really digging the Source Code Pro font, and I am looking forward to updates when they are released. And if you know anything about creating fonts, please help out and contribute. Personally I enjoy looking through the source on GitHub and learning about how fonts are constructed.

20 Sep 2012

Sometimes a nanosecond makes all the difference

3 Comments FluentCassandra

In the Cassandra database there is a type known as TimeUUID. Which I have talked about a couple times on my blog and even created a pretty well received class for generating them. This type is typically used for log data, because it helps you create a unique value in the database that has an extractable timestamp. Because of how .NET creates DateTime.Now you rarely get a resolution smaller than a millisecond in the DateTime, even though DateTime supports a notion of a tick which is equivalent to 100 nanoseconds or 1/10,000 of a millisecond. As you can see there is plenty of room for more resolution, and this extra room not being used causes pain when you need a resolution smaller than milliseconds, which many high performance logging situations demand, so that all your log entries are put in order especially when you are receiving more than one in a millisecond time span.

So you may be asking yourself what does this have to do with Cassandra and TimeUUID.  Well it was brought up to me today that because of the limitations of DateTime duplicate TimeUUID were being created in FluentCassandra, which is obviously not something you want to see for a value that claims it self to be unique.  So today I set out to provide more resolution to the DateTime type, so that FluentCassandra could create unique TimeUUID’s using tick resolution instead of millisecond resolution.

And I call my creation, aptly, DateTimePrecise.

public class DateTimePrecise
{
	private const long TicksInOneSecond = 10000000L;

	private readonly double _syncSeconds;
	private readonly Stopwatch _stopwatch;
	private DateTimeOffset _baseTime;

	public DateTimePrecise(int syncSeconds)
	{
		_syncSeconds = syncSeconds;
		_stopwatch = new Stopwatch();

		Syncronize();
	}

	private void Syncronize()
	{
		lock (_stopwatch) {
			_baseTime = DateTimeOffset.UtcNow;
			_stopwatch.Restart();
		}
	}

	public DateTimeOffset GetUtcNow()
	{
		var elapsedSeconds = _stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;
			
		if (elapsedSeconds > _syncSeconds) {
			Syncronize();
			return _baseTime;
		}

		var elapsedTicks = Convert.ToInt64(elapsedSeconds * TicksInOneSecond);
		return _baseTime.AddTicks(elapsedTicks);
	}
}

DateTimePrecise uses the Stopwatch class which operates based your CPU’s frequency or clockrate. So you get very accurate measurements using Stopwatch. A very accurate measurement is exactly what we needed for our precise times. To create the precise time we create a base time of when the Stopwatch was started and then when the time is requested we add the elapsed time of the Stopwatch onto the base time. And this process is sycronized (or in other words reset) at defined intervals, so that the precise date time doesn’t diverge too radically from the system time.

You can find the source code for the above PreciseDateTime class here, which also includes some static helper methods so that you can just use the class and not have to worry about instantiating it, same way you do with DateTime.Now. A special thanks James Michael Hare needs to be made because his blog post inspired this class. Specifically the part where he shows the radical difference in precision between TimeSpan and the actual resolution of the Stopwatch.

For those interested, you can convert from Stopwatch ticks to seconds by using the Stopwatch.Frequency static property, which tells you the ration of Stopwatch ticks per second. Thus these two are (roughly, due to precision differences) the same:

// take the ElapsedTicks, then divide by Frequency to get seconds
Console.WriteLine("ElapsedTicks to sec:  {0}", sw.ElapsedTicks / (double)Stopwatch.Frequency);

// take the Elapsed property, and query total number of seconds it represents
Console.WriteLine("Elapsed.TotalSeconds: {0}", sw.Elapsed.TotalSeconds);

Which gives us results (on my machine) like:

ElapsedTicks to sec:  4.9998583024032
Elapsed.TotalSeconds: 4.9998583

Wow… look at those numbers they represent the exact same number, but there is an extra 6 digits of resolution on the ElapsedTicks.

I hope you find use in DateTimePrecise, and it will be released with the next release of FluentCassandra to NuGet.

17 Sep 2012

Could not load file or assembly ‘Microsoft.Data.Services, Version=5.0.0.0″

No Comments Uncategorized

Recently while working on an OData endpoint for a web service I am setting up I came across this error. I did a quick search on the web, but all that I came up with was a bunch of overly complicated solutions to fix this issue. So I thought I would write up a quick blog post and put an end to the insanity of this error and all the snake oil that seems to be on the web about it.

This error comes up because there is a reference in your application, either through a framework config or library reference that references the 5.0.0.0 version of the Microsoft.Data.Services assembly. This usually happens because you have included in a more recent version, usually via NuGet, and for whatever reason NuGet decided not to add the following to your config file. Either way here is all that you need to do.

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.Data.Services" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.2.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

The newVersion attribute in the bindingRedirect element needs to be set to whatever the latest version of the Microsoft.Data.Services you are running from NuGet.

Please note that this will not work in Silverlight or Windows Phone, so if you are developing in either of those frameworks please try some of the snake oil. Also please then turn to Microsoft and point them to the strong naming SemVer approach that the ASP.NET team is using to prevent issues like this from occurring. By only updating the version number on major releases of the library.