Thursday, March 26, 2015

xUnit 2.0 has been Released!

It has been a long time coming, but xUnit 2.0 is finally here! My favorite new features:

  • Integrated Theory Support - Theories are one of my favorite xUnit features, and I could not be more excited that they are now a first class citizen of the framework!
  • Parallelism - I have not used it yet, but I am extremely excited at the prospect of quicker test runs on my build server.
  • Assert.ThrowsAsync - I write a lot of async code these days, and now I can write my tests to be async as well!
  • Migration to GitHub - Because Git!

I'm so excited,
Tom

PS: I have already updated my xUnit configuration project to support 2.0.

Sunday, March 8, 2015

HttpConnection Limit in .NET

Does your .NET application need to make a large number of HTTP requests concurrently? Be warned that the amount of simultaneous HTTP connections might get throttled by the .NET Framework. Often this can be a good thing, as it is a restriction that is designed to help protect an application from harming a larger system.

Don't worry, you can easily raise the connection limit by adding a simple Connection Management Section to your app.config or web.config:

<configuration>
  <system.net>
    <connectionManagement>
      <add address="*" maxconnection="10000" />
    </connectionManagement>
  </system.net>
</configuration>

Enjoy,
Tom

Saturday, February 28, 2015

NuGet Pack Command and ExcludeSourceCode

NuGet has an excellent feature that allows you to build a debug package that includes symbols by simply adding the "-Symbols" parameter. The somewhat controversial part of this feature is that it includes a complete copy of the source code to help with debugging.

Good News: NuGet 3.0 will include an additional command option for "-ExcludeSourceCode", allowing you to build a symbol package without including the source directory!

ExcludeSourceCode Description: "Determines if a symbols package should exclude sources when created. Only useful when included with -Symbols."

How soon will 3.0 be released? We are currently on Beta 2, so hopefully soon!

Enjoy,
Tom

Sunday, February 22, 2015

Await an Interval with a Throttle Class in .NET

Are you writing async code but need to control how often a call can be made? Just use this thread safe implementation of a throttle that will return an evenly spaced Task.Delay each time it is invoked. This will allow you to throttle that your application and control how many calls it makes.

Throttle Class

public interface IThrottle
{
    Task GetNext();
    Task GetNext(out TimeSpan delay);
}
 
public class Throttle : IThrottle
{
    private readonly object _lock = new object();
 
    private readonly TimeSpan _interval;
 
    private DateTime _nextTime;
 
    public Throttle(TimeSpan interval)
    {
        _interval = interval;
        _nextTime = DateTime.Now.Subtract(interval);
    }
 
    public Task GetNext()
    {
        TimeSpan delay;
        return GetNext(out delay);
    }
 
    public Task GetNext(out TimeSpan delay)
    {
        lock (_lock)
        {
            var now = DateTime.Now;
 
            _nextTime = _nextTime.Add(_interval);
                
            if (_nextTime > now)
            {
                delay = _nextTime - now;
                return Task.Delay(delay);
            }
 
            _nextTime = now;
 
            delay = TimeSpan.Zero;
            return Task.FromResult(true);
        }
    }
}

Saturday, February 14, 2015

Batch and Partition Extensions for .NET

Did you ever need to take a collection of items and break it into a set of batches, or divide those items into partitions? Well here are some simple extension methods to help you do that:

Extension Methods

public static class EnumerableExtensions
{
    public static IList<IList<T>> Partition<T>(
        this IEnumerable<T> items, 
        int partitionCount)
    {
        if (partitionCount == 0)
        {
            throw new ArgumentException(
                "Partition Count must be greater than zero", 
                "partitionCount");
        }
 
        return items
            .Select(
                (v, i) => new
                {
                    Group = i%partitionCount,
                    Value = v
                })
            .GroupBy(k => k.Group, v => v.Value)
            .Select(g => (IList<T>) g.ToList())
            .ToList();
    }
 
    public static IList<IList<T>> Batch<T>(
        this IEnumerable<T> items, 
        int batchSize)
    {
        if (batchSize == 0)
        {
            throw new ArgumentException(
                "Batch Size must be greater than zero", 
                "batchSize");
        }
 
        var batches = new List<IList<T>>();
        var batch = new List<T>();
 
        foreach (var item in items)
        {
            if (batch.Count == batchSize)
            {
                batches.Add(batch);
                batch = new List<T>();
            }
 
            batch.Add(item);
        }
 
        if (batch.Count > 0)
        {
            batches.Add(batch);
        }
 
        return batches;
    }
}

Saturday, January 31, 2015

Making jQuery a bit more Angular

One of my favorite features of AngularJS is the use of HTML attributes to apply controllers and directives directly to your DOM elements. Why is this so useful?

  • It is intuitive for developers to discover what code is being applied to elements.
  • It enables generic registration, removing boiler plate document ready methods.
  • It provides hierarchical scope, encouraging single responsibility controls.

jQuery plugins are already designed to be applied to collections of elements, so let's just add the ability to dynamically apply plugins via HTML attributes! This is how we can make jQuery a bit more Angular.

Sample Script

Our sample jQuery plugin is super simple; it just makes an element fade in and out continuously. This is a very simple behavior, but the point is that it is just a jQuery plugin!

(function ($) {
    $.fn.blink = function() {
        var $el = this;
        setInterval(blinkEl, 1000);
 
        function blinkEl() {
            $el.fadeToggle();
        }
    };
})(jQuery);

Capture Local Traffic for WireShark, with RawCap

Capturing local network traffic is a difficult task for many tools, including WireShark. Do not worry, there is a very simple solution to this problem!

Just use RawCap!

RawCap is a tiny (23KB) application that can capture local traffic and then write that to a pcap (packet capture) file, which can be opened by tools like WireShark! Please note that when using this you must specify your target address as 127.0.0.1, not just localhost.

Enjoy,
Tom

Real Time Web Analytics