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>();
 
  Parallel.ForEach(
    groups,
    group =>
    {
      var translationValue = GroupToTranslationMap.GetOrAdd(
        group.Value,
        s =>
        {
          try
          {
            return group.Translate(NtAccountType).Value;
          }
          catch (Exception)
          {
            // TODO Log Me
          }
 
          return string.Empty;
        });
 
      if (!string.IsNullOrWhiteSpace(translationValue))
      {
        results.Push(translationValue);
      }
    });
 
  return results;
}

Enjoy,
Tom

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

Real Time Web Analytics