Tuesday, April 21, 2015

Paged List for WebAPI

One of my favorite quotes is "there is nothing as embarrassing as yesterday's code." I blogged about a paged list class a while back, but I no longer like that implementation...so here is a new one that includes WebAPI serialization support!

...but why is this useful?

You can use the simple IPagedList interface to pass paged data around all of your application, and then any object returned from your WebAPI that implements IPagedList will be automatically serialized for you. This allows you to create very consistent APIs that support paging.

IPagedList Interfaces

public interface IPagedList
    int PageIndex { get; }
    int PageSize { get; }
    int TotalCount { get; }
    IList List { get; }
public interface IPagedList<T> : IPagedList
    new IList<T> List { get; }

Thursday, April 9, 2015

xUnit.net: Extensions Config v2.0

Last year I open sourced some code that allowed you to power your xUnit theories from a custom section in your application configuration file. I have now updated that project to support xUnit 2.0, and also to allow for an optional name attribute to be set on each data set.

    <add name="SampleProject.Class1.Main">
        <add index="0" name="Optional" p0="Hello" p1="World" />
        <add index="1" name="Cows" p0="Goodnight" p1="Moon" />


Tuesday, March 31, 2015

Parallelize and Cache Role IdentityReference Group Translate

If you are working with windows authentication you might want to pull role information about your users. If you do that be careful, but the Translate method of the IdentityReference is not particularly fast. Here is some code to help parallelize the gets and cache the results:

private static readonly ConcurrentDictionary<string, string>
  GroupToTranslationMap = new ConcurrentDictionary<string, string>();
private IEnumerable<string> LoadGroupToTranslation(
    IList<IdentityReference> groups)
  var results = new ConcurrentStack<string>();
    group =>
      var translationValue = GroupToTranslationMap.GetOrAdd(
        s =>
            return group.Translate(NtAccountType).Value;
          catch (Exception)
            // TODO Log Me
          return string.Empty;
      if (!string.IsNullOrWhiteSpace(translationValue))
  return results;


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,

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:

      <add address="*" maxconnection="10000" />


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!


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