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;
    }
}
Real Time Web Analytics