Friday, January 2, 2015

File.ReadAllText with an Offset in .NET

What do you do when you need to read text from a file in .NET, but you want to start from an offset? This is a slightly niche scenario, but it does happen. Below is a solution to that problem.

To summarize the implementation, you open a file stream, seek to your offset, and then read in the bytes from there. While loading the result I read small chunks into a buffer, decoded them, and then added the decoded string to a string buffer for storage. Please note that if you are using a multibyte encoding then this helper will only work if you use the correct offset.

Helper Code

public class FileHelper
{
    private const int BufferSize = 1024;
 
    public static string ReadAllTextFromOffset(
        string path, 
        Encoding encoding, 
        int offset, 
        out int totalLength)
    {
        using (var fs = new FileStream(path, FileMode.Open))
        {
            totalLength = offset;
 
            if (offset > 0)
            {
                fs.Seek(offset, SeekOrigin.Begin);
            }
 
            var sb = new StringBuilder();
            var buffer = new byte[BufferSize];
            int readCount;
 
            do
            {
                readCount = fs.Read(buffer, 0, buffer.Length);
                totalLength += readCount;
 
                var subString = encoding.GetString(buffer, 0, readCount);
                sb.Append(subString);
            }
            while (readCount == buffer.Length);
 
            return sb.ToString();
        }
    }
}

Wednesday, December 31, 2014

2014 Retrospective

Blog

This was the first year where I wrote four blog posts every month, and the unintended side effect was that the posts become smaller. I'm not sure how much I liked that; on the one hand I got to write more quick articles with little helpful information, but to be honest I did not enjoy writing those little posts as much.

Next year I will be reducing this to only three medium length posts per month. I am hoping this will be a good compromise between quantity and quality.

QQ Cast

I absolutely loved doing the QQ-Cast with my friend, Jordan. We started out the year strong, averaging three hour long episodes a month. Unfortunately as the year continued "life got in the way" and we only recorded two episodes in the last three months of the year.

Next year Jordan and I will be following a release cadence of three times a month, but shorter half hour episodes. Obviously this is similar to the blog schedule.

Professional

It has been a crazy year for me professionally. Without going into detail, I switched teams at work and now have a completely different set of responsibilities. I made this change to in order to get out of my comfort zone and, as is a core value for my company, to learn and grow.

It has been very challenging, but very rewarding. I would highly encourage all software engineers who have been in the same roll for three years or more to consider changing positions. What you learn in the first three months of a new job is often more than you will learn the rest of the year.

Personal

I got married. I bought an FZ-09. I, as mentioned above, changed positions at work. That really sums up 2014. Next year I clearly need to focus more on having a better work / personal life balance...maybe I should take more vacations?

Happy New Year,
Tom

Sunday, December 28, 2014

Why you should use more HTTP.

I am a big fan of Ayende Rahien. He leads Hibernating Rhinos, develops RavenDB, and contributes to NHibernate. Oh, and he is an avid blogger that puts my release schedule to shame.

Ayende recently blogged about over the wire protocol design, and I wanted to echo opinion on this subject!

How to actually send it?

As applications become more complicated they frequently need to communicate with remote resources. So how should your applications talk to each other? If you had not figured it out already: I suggest HTTP!

Why use HTTP?

Here are just a few very small reasons that I love working with HTTP:

  • It is supported by every language.
  • It is supported by every platform.
  • It is supported by every device.
  • It has standards for authorization.
  • It has standards for encryption.
  • It has standard response codes.
  • There are amazing tools for it.
  • There are more amazing tools for it.
  • There are still more amazing tools for it.

...need I go on?

Why NOT use HTTP?

I always look to successful companies for inspiration with technology. If you are making a store front, I suggest you look at Amazon or Newegg. If you are creating social media features, I suggest you look at Facebook or Twitter. If you are designing APIs, I suggest you look at Google. Do you know what Larry Page did not say after Google made it big? "Man, I really wish that we hadn't invested so heavily in all of that web stuff!"

Okay seriously, depending on your application there may be reasons not to use HTTP as your communication protocol. However for the vast majority of applications I think it will do the job, and I strongly urge you to consider using it instead of reinventing the wheel again.

Just my two cents,
Tom

Wednesday, December 24, 2014

How to use Entity Framework and SQLite

SQLite is the definition of a lightweight database. Using SQLite you can run an entire database with only a 304 KB executable and a database file. It's fast, stable, and very easy to use. Entity Framework is Microsoft's official ORM, and it has support for SQLite!

SQLite Tools

To run SQLite you need only to download the precompiled SQLite binaries for windows:

You can easily manipulate the database via command line. However, if you would prefer to use a GUI, there is a wonderful Firefox plugin for managing your SQLite databases.

Entity Framework Setup

To get stated using Entity Framework you will need to add two NuGet packages to your solution:

  1. EntityFramework
  2. System.Data.SQLite (x86/x64)

After that you will need to make sure that your app.config file has properly registered the both a System.Data.SQLite provider and provider factory.

Sunday, December 14, 2014

How much does RegexOptions.Compiled improve performance in .NET?

Just how much does the RegexOptions.Compiled flag improve regular expression performance in .NET? The answer: a lot! People have spoken about this before, but below are some more numbers to how you just how much it matters!

Performance Stats

Character Count Regex Pattern
1
[a-z]
3
[b-y].[1-8]
5
[b-y].[c-x].[1-8].[2-7]
7
[b-y].[c-x].[d-w].[1-8].[2-7].[3-6]
9
[b-y].[c-x].[d-w].[e-v].[1-8].[2-7].[3-6].[4-5]

RegexOptions 1 3 5 7 9
None 234176 285067 653016 690282 687343
Compiled 193945 235213 430609 452483 454625
Percent Gain 17% 17% 34% 34% 34%

Saturday, November 29, 2014

.NET 4.5 HttpClient is Thread Safe

Good news everyone, the .NET 4.5 HttpClient is thread safe!

This means that you can share instances of your HttpClients across your entire application. This is useful in that it allows you to reuse persisted connections. One of the best ways to do this is to create a class that can manage the object lifetime of those clients for you.

Below is a simple HttpClientManager that will create one HttpClient per authority. Why per authority? Because you might have to have different settings or credentials for different websites.

Sample Unit Tests

public class HttpClientManagerTests
{
    [Fact]
    public void GetForAuthority()
    {
        using (var manager = new HttpClientManager())
        {
            var client1 = manager.GetForAuthority("http://tomdupont.net/");
            var client2 = manager.GetForAuthority("https://tomdupont.net/");
            Assert.Same(client1, client2);
 
            var client3 = manager.GetForAuthority("http://google.com/");
            Assert.NotSame(client1, client3);
        }
    }
 
    [Fact]
    public void TryRemoveForAuthority()
    {
        const string uri = "http://tomdupont.net/";
 
        using (var manager = new HttpClientManager())
        {
            Assert.False(manager.TryRemoveForAuthority(uri));
 
            manager.GetForAuthority(uri);
 
            Assert.True(manager.TryRemoveForAuthority(uri));
 
        }
    }
}

Friday, November 28, 2014

Web API - Return Correct Status Codes for Exceptions

Returning the appropriate HTTP Response Codes back from your web server is a very important best practice. Fortunately for .NET developers, Web API makes it very easy to use Exception Filters to return the appropriate response codes from your exceptions.

By implementing a custom ExceptionFilterAttribute you can generically create and return HttpResponseMessages for unhandled exceptions based on type. This is great in that you do not have to wrap all of your controller actions in try catch blocks to handle exceptions from other application layers.

Sample Controller

public class ValuesController : ApiController
{
    public string Get(int id)
    {
        switch (id)
        {
            case 1:
                throw new KeyNotFoundException("Hello World");
 
            case 2:
                throw new ArgumentException("Goodnight Moon");
 
            default:
                return "value";
        }
    }
}
Real Time Web Analytics