Thursday, May 14, 2015

Split your App.config into Multiple Files

What do you do when any file in your project becomes too big? You break it apart into multiple files!

Did you know that you can split your .NET configuration files into multiple files as well? Here is how...

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="customSection" 
    type="ConfigSectionDemo.CustomConfigurationSection, ConfigSectionDemo" />
  </configSections>
  <appSettings configSource="AppSettings.config" />
  <customSection configSource="CustomSection.config" />
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

AppSettings.config

<appSettings>
  <add key="Hello" value="World"/>
</appSettings>

CustomSection.config

<customSection goodnight="Moon">
</customSection>

Thursday, April 30, 2015

Persistent Cache Repository using SQLite

A while back I blogged about an ICacheRepository. I still really like that concept, although admittedly there a few updates that I should make to the implementation.

The basic implementation that I shared before was for an in memory cache. There are plenty of document databases that I would recommend for you to use as a persistent cache mechanism, and they would also have the added benefit of being distributed across services.

What do you do if you need a persistent cache, but you don't have any of those document databases available? Just use files! By which I mean SQLite.

Would this stand up under load? Probably not.
Would I recommend that production systems use this? No.
Is it super simple to setup? Yes, yes it is!

Enjoy,
Tom

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.

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

Enjoy,
Tom

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