Saturday, December 19, 2015

Group Regex Replace in Visual Studio

Wow, I cannot believe that I did not know about this until now!

We all know that you can use regular expressions to search for things in a document, and we all know that you can replace those matches, but did you know that you can use group matches from your regex in the replace value? Because you can, and it is amazingly useful!

Example: Your class has properties with one attribute containing a value, and you want to add another attribute with the same value.

Find: \[Description\("(\w+)"\)\]

public class MyClass
{
    [Description("A"), DisplayName("A")]
    public string A { get; set; }
 
    [Description("B")]
    public string B { get; set; }
 
    [Description("C")]
    public string C { get; set; }
}

Replace: [Description("$1"), DisplayName("$1")

public class MyClass
{
    [Description("A"), DisplayName("A")]
    public string A { get; set; }
 
    [Description("B"), DisplayName("B")]
    public string B { get; set; }
 
    [Description("C"), DisplayName("C")]
    public string C { get; set; }
}

Enjoy,
Tom

Wednesday, December 16, 2015

2015 Retrospective

Wow! This is my 200th post, and the exact 7 year anniversary of when I started blogging!

Blog

Blogging for seven years straight has been quite a challenge, but it has also been one of most rewarding things that I have ever done. It has become a regular occurrence for me to answer questions at work with "oh, I have a blog post about that!"

I work best when I have deadlines and quotas, and I am very happy to have written three posts per month this year. I think that this pace has been perfect; enough to keep me busy and share plenty of information, but still not so much that I didn't have time to really fill out the content.

I intend to continue writing three posts per month in 2016.

QQ-Cast

Unfortunately, despite how much fun we had in 2014, the QQ-Cast got put on hiatus for most of 2015. This was not something that Jordan and I wanted to do, but as we say on the show "life got in the way." Jordan became a father, and I...well we will get to that in a moment.

Good news dear listener, we're back! My friend Zach Mayer and I have just started recording again. We are continuing the tradition of being iterative, so we will be making several tweaks to the show format. I'm very happy to be recording again, and can't wait to see how 2016 goes.

Professional

2015 was yet another crazy year for me professionally, and I wouldn't have it any other way! My company launched an amazing number of products, and I am very proud to have directly contributed to several of those launches.

My team and I have been focusing heavily automation and performance testing, and it is an absolute blast! We get to engage with engineers from around the company, we get to play with a diverse set of tech stacks, and I just love every minute of it.

At the time of writing this we have openings for C#, C++, and Node.js...so come work with us!

Personal

2015 has been a rough year for me personally. Tragedy struck my wife and I in April, and we have been struggling to recover ever since. Frankly, I cannot help but be glad that 2015 is over, and I can only hope that 2016 goes...better.

On the plus side, after being on the bench for four years, I have finally started to play soccer again! It's been an absolutely blast to just run around and kick the ball again. What's next? Rock climbing? Sailing? Spelunking? Let's find out!

Thanks again,
Tom

Sunday, December 13, 2015

WebSocket4Net Extensions: OpenAsync

I recently talked about .NET WebSocket Libraries, and how I like using WebSocket4Net as a .NET WebSocket client. That library is great, but really wanted it to have two additional features:

  1. Retry logic for opening a connection.
  2. An OpenAsync method.

...so I created an extension method that does both!

WebSocket4Net Extensions

public static class WebSocketExtensions
{
    public static async Task OpenAsync(
        this WebSocket webSocket,
        int retryCount = 5,
        CancellationToken cancelToken = default(CancellationToken))
    {
        var failCount = 0;
        var exceptions = new List<Exception>(retryCount);
 
        var openCompletionSource = new TaskCompletionSource<bool>();
        cancelToken.Register(() => openCompletionSource.TrySetCanceled());
 
        EventHandler openHandler = (s, e) => openCompletionSource.TrySetResult(true);
 
        EventHandler<ErrorEventArgs> errorHandler = (s, e) =>
        {
            if (exceptions.All(ex => ex.Message != e.Exception.Message))
            {
                exceptions.Add(e.Exception);
            }
        };
 
        EventHandler closeHandler = (s, e) =>
        {
            if (cancelToken.IsCancellationRequested)
            {
                openCompletionSource.TrySetCanceled();
            }
            else if (++failCount < retryCount)
            {
                webSocket.Open();
            }
            else
            {
                var exception = exceptions.Count == 1
                    ? exceptions.Single()
                    : new AggregateException(exceptions);
 
                var webSocketException = new WebSocketException(
                    "Unable to connect", 
                    exception);
 
                openCompletionSource.TrySetException(webSocketException);
            }
        };
 
        try
        {
            webSocket.Opened += openHandler;
            webSocket.Error += errorHandler;
            webSocket.Closed += closeHandler;
 
            webSocket.Open();
 
            await openCompletionSource.Task.ConfigureAwait(false);
        }
        finally
        {
            webSocket.Opened -= openHandler;
            webSocket.Error -= errorHandler;
            webSocket.Closed -= closeHandler;
        }
    }

Enjoy,
Tom

Monday, November 30, 2015

.NET Semaphore Slim that Supports Keys

While making a HUGE update to my CacheRepository project, I needed a way to have a dynamic number of semaphores that would lock on a specified cache key. The SemaphoreSlim is great, but I needed a wrapper around it that allowed me have one for each unique cache key being fetched.

The easiest solution was just to have a concurrent dictionary of string to semaphore, but at high load that would grow in size and I did not want to waste memory. Instead I created a class that does keep a dictionary of semaphores, but then removes them from the dictionary and stores them in a queue for reuse once there is nothing locking off on them.

Enough talking! Below is the code, and as always it comes with unit tests! :)

Sunday, November 29, 2015

Obsolete Blog Posts

I was recently asked an interesting question:

"Are there any blog posts you wrote that you no longer agree with?"

Yes there are. As I have grown as a developer there are many patterns and practices that I have changed my opinions about. Also, many of my posts are related to specific technologies that have grown, changed, or become deprecated over time.

Here is a small list of posts on my blog that I now consider to be obsolete.

Going back through these posts to write this post has made me notice a common theme as I grow: I continue to advocate simpler solutions to problems. I like that trend, and I can't wait to see what I am writing about in another 7 years.

Live and learn,
Tom

Wednesday, November 25, 2015

.NET WebSocket Libraries

WebSockets are awesome, and you should be using them. If you are working with .NET, then there are some very easy to consume libraries to help you host a WebSocket server or connect as a WebSocket client.

Below is a complete chat server and client made using ONLY these two libraries.

Saturday, October 31, 2015

How to change HttpClientHandler.AllowAutoRedirect

In the past I have talked about how the HttpClient is thread safe. This allows you to reuse the same HttpClient and be very efficient regarding how many ephemeral ports your application consumes.

Because the HttpClient and the HttpClientHandler both need to be thread safe, their properties become immutable after a request has been issued. If you are in a scenario where you need to change settings, such as whether or not the handler allows redirects, you will have to develop a little hack to work around the default behavior.

Below is a method where you can use reflection to set a private field and avoid the property setter from checking whether or not a request has been issued. This could cause thread safety issues, however with the current implementation of HttpCliehtHandler it is perfectly safe so long as only one thread is consume the client at a time.

HttpClientHandler Extension

public static class HttpClientHandlerExtensions
{
    private static readonly FieldInfo AllowAutoRedirectFieldInfo =
        typeof (HttpClientHandler).GetField(
            "allowAutoRedirect",
            BindingFlags.Instance | BindingFlags.NonPublic);
 
    public static void SetAllowAutoRedirect(this HttpClientHandler handler, bool value)
    {
        AllowAutoRedirectFieldInfo.SetValue(handler, value);
    }
}

Unit Test

public class HttpClientHandlerExtensionsTests
{
    [Fact]
    public async Task SetAllowAutoRedirectTest()
    {
        using (var handler = new HttpClientHandler())
        using (var client = new HttpClient(handler))
        {
            handler.AllowAutoRedirect = true;
 
            using (var response = await client.GetAsync("http://www.google.com"))
                response.EnsureSuccessStatusCode();
 
            Assert.Throws<InvalidOperationException>(() =>
            {
                handler.AllowAutoRedirect = false;
            });
 
            handler.SetAllowAutoRedirect(false);
        }
    }
}

Enjoy,
Tom

Sunday, October 25, 2015

Override Configuration via Command Line

In my previous blog posts I have talked about creating complex config objects from your app.config file, as well as how to have cascading configuration settings from multiple files. Now I want to build on that concept by taking in configuration from command line in a generic fashion that will override your other cascading settings.

Configuration Object

public class TestConfig
{
    public string Hello { get; set; }
    public string Goodnight { get; set; }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TestConfig.Hello" value="World" />
    <add key="TestConfig.Goodnight" value="Moon" />
  </appSettings>
</configuration>

Sunday, October 4, 2015

Throttles - Delay vs Semaphore

A while back I had talked about how to await an interval with a simple Throttle class that I had made. This is a very easy way to control how often you start an operation, but it does not ensure a limit to how many operations are happening at a given time.

Problem: You want to only make 5 requests per second to a remote service.

With the throttle class set to only allow one new call to start every 200 milliseconds you will only be able to start 5 new requests per second. However, if those calls take longer than two seconds to complete, then during the next second you will have 10 requests in flight at the same time.

Solution: Set your throttle to 200 millisecond and add a semaphore with a count of 5.

You can solve this problem by combining your throttle with a semaphore. This will ensure that you only start a new operation at your schedule frequency, and also that you never have more than a predetermined number in flight at the same time.

Please note that only using a semaphore would not solve the problem because if, in the same example, the calls take sub 200 milliseconds to complete then more than 5 requests will start each second.

Below is a set of helper classes (and tests) to help extend the throttle class to support this functionality within a using block.

Interfaces

public interface IUsableSemaphore : IDisposable
{
    Task<IUsableSemaphoreWrapper> WaitAsync();
}
 
public interface IUsableSemaphoreWrapper : IDisposable
{
    TimeSpan Elapsed { get; }
}
 
public interface IThrottle
{
    Task WaitAsync();
}

Wednesday, September 30, 2015

XDT Console Application

XML Document Transformations, or XTD, is a great way to transform your app and web config files between environments or builds. It is directly supported by Visual Studio and other third party tools, such as Octopus Deploy.

So how can you transform config files on your own? For starters uou can use great free tools like the Web.config Transformation Tester (which is open source) from AppHarbor.

Would you rather transform your files via command line? Then just do it yourself! Pull down the Microsoft.Web.Xdt package from NuGet, and then copy and paste this code to implement your own simple console app...

Program.cs

namespace XDT
{
  using System;
  using System.IO;
  using System.Text;
  using System.Xml;
 
  using Microsoft.Web.XmlTransform;
 
  public class Program
  {
    private static int Main(string[] args)
    {
      if (args.Length != 3)
      {
        Console.WriteLine("Required Arguments: [ConfigPath] [TransformPath] [TargetPath]");
        return 400;
      }
 
      var configPath = args[0];
      if (!File.Exists(configPath))
      {
        Console.WriteLine("Config not found");
        return 404;
      }
 
      var transformPath = args[1];
      if (!File.Exists(transformPath))
      {
        Console.WriteLine("Transform not found");
        return 404;
      }
 
      try
      {
        var targetPath = args[2];
        var configXml = File.ReadAllText(configPath);
        var transformXml = File.ReadAllText(transformPath);
 
        using (var document = new XmlTransformableDocument())
        {
          document.PreserveWhitespace = true;
          document.LoadXml(configXml);
 
          using (var transform = new XmlTransformation(transformXml, false, null))
          {
            if (transform.Apply(document))
            {
              var stringBuilder = new StringBuilder();
              var xmlWriterSettings = new XmlWriterSettings
              {
                Indent = true,
                IndentChars = "  "
              };
 
              using (var xmlTextWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings))
              {
                document.WriteTo(xmlTextWriter);
              }
 
              var resultXml = stringBuilder.ToString();
              File.WriteAllText(targetPath, resultXml);
              return 0;
            }
 
            Console.WriteLine("Transformation failed for unknown reason");
          }
        }
      }
      catch (XmlTransformationException xmlTransformationException)
      {
        Console.WriteLine(xmlTransformationException.Message);
      }
      catch (XmlException xmlException)
      {
        Console.WriteLine(xmlException.Message);
      }
 
      return 500;
    }
  }
}

Enjoy,
Tom

Saturday, September 26, 2015

How to only Serialize Interface Properties with Json.NET

When you serialize an object with Newtonsoft's Json.NET it will resolve the serialization contract for the type being serialized. This means that if you want to serialize an object so that it matches one of the interfaces that it implements you will need to use a customized contract resolver.

When I first tried to do this I made a completely custom JsonConverter for the type that looked up the properties via reflection and just wrote their values out manually. Unfortunately had the side effect of bypassing all of the features the Newtonsoft provides with regard to decorating classes and customizing the serialization process for that object.

There was a good topic on Stack Overflow about this that led me to the custom contract resolver solution. However the sample implementation there is hard coded to only try to serialize one hard coded type for all serialization.

Below is an implementation (with tests) that allows you to specify a list of interfaces that you want to serialize by, and then if the object being serialized does implement that interface it will fall back on it's default contract.

InterfaceContractResolver Implementation

public class InterfaceContractResolver : DefaultContractResolver
{
    private readonly Type[] _interfaceTypes;
 
    private readonly ConcurrentDictionary<Type, Type> _typeToSerializeMap;
 
    public InterfaceContractResolver(params Type[] interfaceTypes)
    {
        _interfaceTypes = interfaceTypes;
 
        _typeToSerializeMap = new ConcurrentDictionary<Type, Type>();
    }
 
    protected override IList<JsonProperty> CreateProperties(
        Type type,
        MemberSerialization memberSerialization)
    {
        var typeToSerialize = _typeToSerializeMap.GetOrAdd(
            type,
            t => _interfaceTypes.FirstOrDefault(
                it => it.IsAssignableFrom(t)) ?? t);
 
        return base.CreateProperties(typeToSerialize, memberSerialization);
    }
}

Wednesday, September 16, 2015

Add Generated Files to your Project

I recently had to work with a small code generator and needed to dynamically add generated cs files to my csproj. This actually ended up being very simple!

You will need to use ProjectCollection in the Microsoft.Build assembly to load the project, and then once you have scanned the directory for files you can easily diff the two to find new files. The only "catch" is that you need be sure to match the files to the relative paths from the csproj.

Code

Here is a very simple sample console application that adds generated files to another csproj. (See the picture to the right for the exact file structure.)

public static void Main()
{
    const string relativePath = "../../../TestProj/";
 
    var files = Directory
        .GetFiles(relativePath + "Generated", "*.cs")
        .Select(r => r.Replace(relativePath, string.Empty));
 
    using (var engine = new ProjectCollection())
    {
        var project = engine.LoadProject(relativePath + "TestProj.csproj");
 
        var newFiles = files
            .Where(f => !project.Items.Any(i => i.UnevaluatedInclude == f))
            .ToList();
 
        foreach (var newFile in newFiles)
        {
            project.AddItem("Compile", newFile);
        }
 
        project.Save();
    }
}

Enjoy,
Tom

Thursday, August 27, 2015

IEnumerable Unity Injection

A few years ago I blogged about how to add Lazy Unity Injection, and then a year after that Unity 3 added that feature. Recently I had to dust off this old code to do something similar...

I wanted to use Unity to inject a collection of registered types into one of my services. To do this directly from the container you would use named registrations and ResolveAll. However if you just try to resolve an IEnumerable of a type in a constructor, then Unity will just try to use Resolve and thus throw an InvalidOperationException.

We can easily "fix" this by registering an extension with our container.

Extension

public class EnumerableContainerExtension : UnityContainerExtension
{
    protected override void Initialize()
    {
        Context.Policies.Set<IBuildPlanPolicy>(
            new EnumerableBuildPlanPolicy(),
            typeof(IEnumerable<>));
    }
 
    private class EnumerableBuildPlanPolicy : IBuildPlanPolicy
    {
        public void BuildUp(IBuilderContext context)
        {
            if (context.Existing != null)
                return;
 
            var container = context.NewBuildUp<IUnityContainer>();
            var typeToBuild = context.BuildKey.Type.GetGenericArguments()[0];
 
            context.Existing = container.ResolveAll(typeToBuild);
 
            DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
        }
    }
}

Saturday, August 22, 2015

Ambiguous Invocation from Optional Parameter Overloads

There are many reasons for your C# compiler to give you an ambiguous invocation error. When using optional arguments you have to be sure not to create an overload that have overlapping optional parameters. Let's take a look:

public void DoStuff(int i, object o = null) { }
 
public void DoStuff(int i, string s = null) { }
 
public void DoStuff(int i, string s = null, bool b = false) { }
 
public void Test()
{
    // Ambiguous invocation!
    DoStuff(1);
}

There is no need to create methods like the ones shown above!

Instead consider that there is no reason to have two overloads being called with the potentially the same parameters. Such overloads should be combine into one method, and other overloads with different types argument can just be written as normal parameters without default values. Again, let's take a look:

public void DoStuff(int i, object o) { }
 
public void DoStuff(int i, string s = null, bool b = false) { }
 
public void Test()
{
    // No more ambiguous invocation :)
    DoStuff(1);
}

I hope that helps,
Tom

Saturday, August 15, 2015

Create objects from default constructor

Do you think that title sound redundant? If so, which method were you thinking of? Because there are several! Let's talk about those...and be sure to scroll to the bottom for a performance comparison!

Sample Class

Here is the class that we will be instantiating in our tests.

internal class TestClass
{
    public Guid Guid { get; private set; }
 
    public TestClass()
    {
        Guid = Guid.NewGuid();
    }
}

The new Keyword

This is the obviously, easier, fastest, normal way of instantiating an object...obviously.

[Fact]
public void NewDuh()
{
    var testObject = new TestClass();
    Assert.NotNull(testObject.Guid);
}

Activator.CreateInstance

This is a very simple and common way to instantiate an object from a Type object. It has an overload that takes a params object collection to let you use other, non default, constructors.

[Fact]
public void ActivatorCreateInstance()
{
    var type = typeof (TestClass);
    var instance = Activator.CreateInstance(type);
            
    var testObject = Assert.IsType<TestClass>(instance);
    Assert.NotNull(testObject.Guid);
}

Saturday, July 25, 2015

Cascading AppSettings from Multiple Config Files

In my previous blog post I talked about creating complex configuration objects from AppSettings. I really like this practice, but it can cause your config files to grow pretty large. One solution is to break your app.config into multiple files using the SectionInformation.ConfigSource property.

This also has the added side effect of allowing you to include defaults and overrides by having those settings cascade. I have created an extension method to combine a series of NameValueCollections, such as an AppSettings section. You can also grab this code from the GitHub project.

Combine Extension

public static NameValueCollection Combine(
    this NameValueCollection collection, 
    params NameValueCollection[] collections)
{
    var result = new NameValueCollection { collection };
 
    foreach (var subCollection in collections)
        foreach (var key in subCollection.AllKeys)
        {
            if (result.AllKeys.Contains(key))
                continue;
 
            var value = subCollection[key];
            result.Add(key, value);
        }
 
    return result;
}

Sunday, July 19, 2015

Create Config Objects from AppSettings

Providing configuration settings is a vital task that all applications need. Unfortunately dealing with ConfigurationSections and IConfigurationSectionHandlers can be annoying.

That is why I created a simple little extension method to create complex objects from your app settings. The extension works with a NameValueCollection, and can create primitive types, complex objects, collections, dictionaries, and even recursive properties.

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TestConfig.Int" value="1" />
    <add key="TestConfig.String" value="Hello" />
    <add key="TestConfig.DateTime" value="1/2/2015" />
    <add key="TestConfig.TimeSpan" value="00:01:00" />
    <add key="TestConfig.Enum" value="Moon" />
    <add key="TestConfig.NullableIntA" value="2" />
    <add key="TestConfig.NullableIntB" value="" />
  </appSettings>
</configuration>

Test

[Fact]
public void CreateSimpleObject()
{
    var result = ConfigurationManager.AppSettings.CreateObject<TestConfig>();
 
    Assert.NotNull(result);
    Assert.Equal(1, result.Int);
    Assert.Equal(42, result.IntWithDefault);
    Assert.Equal("Hello", result.String);
    Assert.Equal(new DateTime(2015, 1, 2), result.DateTime);
    Assert.Equal(TimeSpan.FromMinutes(1), result.TimeSpan);
    Assert.Equal(TestEnum.Moon, result.Enum);
    Assert.Equal(2, result.NullableIntA);
    Assert.Null(result.NullableIntB);
}

Enjoy,
Tom

Saturday, July 11, 2015

Common.Logging.NLog40

The Common.Logging team accepted my pull request, and there is now a Common.Logging.NLog40 package to support NLog 4.

Enjoy,
Tom

Sunday, June 28, 2015

.NET Asynchronous Batch Processor

The .NET Framework offers a series of Thread-Safe Collections that allows you to consume collections across threads. Processing the contents of these collections still requires a thread, and while there is a BlockingCollection there is unfortunately no such class to support this in an asynchronous fashion. (Please note that the always awesome Stephen Cleary did actually implement an AsyncCollection.)

What if you want to handle dynamically sized batches of data in an asynchronous manner?

You could use a series of Dataflow blocks, or if you are looking for a simple solution you can write a small class that uses an async loop to process a ConcurrentQueue. Below is an abstract base class that can help you implement this:

Base Class

public abstract class BatchProcessorBase<T> : IDisposable
{
    protected readonly int MaxBatchSize;
    private readonly ConcurrentQueue<T> _queue;
    private readonly CancellationTokenSource _cancelSource;
    private readonly object _queueTaskLock;
    private Task _queueTask;
    private bool _isDiposed;
 
    protected BatchProcessorBase(int maxBatchSize)
    {
        MaxBatchSize = maxBatchSize;
        _queue = new ConcurrentQueue<T>();
        _cancelSource = new CancellationTokenSource();
        _queueTaskLock = new object();
        _queueTask = Task.FromResult(true);
    }
        
    public void Enqueue(T item)
    {
        _queue.Enqueue(item);
        TryStartProcessLoop();
    }
 
    public void Dispose()
    {
        if (_isDiposed)
            return;
 
        _cancelSource.Cancel();
        _isDiposed = true;
    }
 
    protected abstract Task ProcessBatchAsync(
        IList<T> list, 
        CancellationToken cancelToken);
 
    private void TryStartProcessLoop()
    {
        // Lock so only one thread can manipulate the queue task.
        lock (_queueTaskLock)
        {
            // If cancellationhas been requested, do not start.
            if (_cancelSource.IsCancellationRequested)
                return;
 
            // If the loop is still active, do not start.
            if (!_queueTask.IsCompleted)
                return;
 
            // If the queue is empty, do not start.
            if (_queue.Count == 0)
                return;
 
            // Start a new task to process the queue.
            _queueTask = Task.Run(() => ProcessQueue(), _cancelSource.Token);
 
            // When the process queue task completes, check to see if
            // the queue has been populated again and needs to restart.
            _queueTask.ContinueWith(t => TryStartProcessLoop());
        }
    }
 
    private async Task ProcessQueue()
    {
        // Stay alive until the queue is empty or cancellation is requested.
        while (!_cancelSource.IsCancellationRequested && _queue.Count > 0)
        {
            var list = new List<T>();
            T item;
 
            // Dequeue up to a full batch from the queue.
            while (list.Count < MaxBatchSize && _queue.TryDequeue(out item))
                list.Add(item);
 
            // Process the dequeued items.
            await ProcessBatchAsync(list, _cancelSource.Token);
        }
    }
}

Wednesday, June 24, 2015

Capture xUnit Test Output with NLog and Common Logging

I recently blogged about How To Capture Test Output in xUnit 2.0. This is great, but how can we pass the ITestOutputHelper into our code to capture log output?

You could just wrap the xUnit helper in an ILog or ILogger, but we can also take it a step further and get all of the NLog features too! By creating an NLog target that wraps the ITestOutputHelper we can enable ourselves to use multiple targets, layouts, variables, verbosity levels, and more.

Sample Unit Test

public class NLogTests : IDisposable
{
    private readonly ILogger _logger;
 
    public NLogTests(ITestOutputHelper outputHelper)
    {
        _logger = outputHelper.GetNLogLogger();
    }
 
    public void Dispose()
    {
        _logger.RemoveTestOutputHelper();
    }
 
    [Fact]
    public void Hello()
    {
        _logger.Trace("World Trace");
        _logger.Debug("World Debug");
        _logger.Warn("World Warn");
        _logger.Error("World Error");
    }
}

Monday, June 22, 2015

How To: Kill child process when parent process is killed

UPDATE (2/15/2016): There is now a v2 of this class!

Killing child all child process spawned by a parent process is an extremely useful trick that is not directly supported by the .NET framework. Fortunately the windows operating system, more specifically Kernel32.dll, does support the ability to link one process to another on shutdown. A huge thanks to Matt Howell for sharing this solution on Stack Overflow!

I took the liberty of cleaning up a few small things in the code and creating a demo:

Saturday, May 30, 2015

.NET Generic Overloads: How to support T and IList

What happens when you want to overload a generic method where the first method accepts a single object and the other accepts an IList of that type?

It will only work when specifically try to pass in an IList. If you try to pass in a List it will fail because the compiler will identify the generic parameter overload and fail before trying to use the implicit cast to an IList.

This is because a C# compiler tries to identify overloaded methods it checks for matching parameters in the following order:

  1. Explicit type matches.
  2. Generic parameters.
  3. Implicit type matches.

Let's look at an example!

Example of what DOES NOT work.

public class OverloadSample1
{
    [Fact]
    public void Test()
    {
        // Matches Method A - Good
        ISample sample = new Sample();
        this.Method(sample);
 
        // Matches Method B - Good
        IList<ISample> samples1 = new List<ISample>();
        this.Method(samples1);
 
        // Matches Method A - BAD!
        List<ISample> samples2 = new List<ISample>();
        this.Method(samples2);
    }
 
    // A
    public void Method<T>(T sample)
        where T : ISample
    {
    }
 
    // B
    public void Method<T>(IList<T> sample)
        where T : ISample
    {
    }
}

...so, how do we solve this problem?

Saturday, May 23, 2015

How To Capture Test Output in xUnit 2.0

As of xUnit 2.0 the test framework no longer captures any native outputs, this is by design. Now the question is: where do you write your test output?

You now have to write all test output to an interface, ITestOutputHelper, that is injected into your test's constructor. This design seems to have polarized developers a bit, as not everyone enjoys being require to add a constructor to your test classes. Personally, as a huge fan of dependency injection, I really like this solution.

Spoilers: My next blog post will be about how to combine this with Common.Logging and NLog.

Sample Test

using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
 
namespace XUnitDemo
{
    public class OutputTests
    {
        private readonly ITestOutputHelper _testOutput;
 
        public OutputTests(ITestOutputHelper testOutput)
        {
            _testOutput = testOutput;
        }
 
        [Fact]
        public async Task WriteLine()
        {
            _testOutput.WriteLine("Hello");
            _testOutput.WriteLine("World");
 
            await Task.Delay(1000);
            
            _testOutput.WriteLine("Goodnight");
            _testOutput.WriteLine("Moon");
        }
    }
}

Enjoy,
Tom

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

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

Friday, January 2, 2015

File.ReadAllText with an Offset in .NET

What do you do when you need to read text from a file in .NET, but you want to start from an offset? This is a slightly niche scenario, but it does happen. Below is a solution to that problem.

To summarize the implementation, you open a file stream, seek to your offset, and then read in the bytes from there. While loading the result I read small chunks into a buffer, decoded them, and then added the decoded string to a string buffer for storage. Please note that if you are using a multibyte encoding then this helper will only work if you use the correct offset.

Helper Code

public class FileHelper
{
    private const int BufferSize = 1024;
 
    public static string ReadAllTextFromOffset(
        string path, 
        Encoding encoding, 
        int offset, 
        out int totalLength)
    {
        using (var fs = new FileStream(path, FileMode.Open))
        {
            totalLength = offset;
 
            if (offset > 0)
            {
                fs.Seek(offset, SeekOrigin.Begin);
            }
 
            var sb = new StringBuilder();
            var buffer = new byte[BufferSize];
            int readCount;
 
            do
            {
                readCount = fs.Read(buffer, 0, buffer.Length);
                totalLength += readCount;
 
                var subString = encoding.GetString(buffer, 0, readCount);
                sb.Append(subString);
            }
            while (readCount == buffer.Length);
 
            return sb.ToString();
        }
    }
}
Real Time Web Analytics