I am trying to asynchronously log some information to SQL Server inside of an MVC 4 controller action targeting .NET 4.0 using the AsyncTargetingPack. I would jump straight
Dataflow only works on .NET 4.5. The fact that you're running it on .NET 4.0 is unsupported and is likely why you're seeing spurious exceptions.
I was just having a very similar issue (NullReference from AssociateWithCurrentThread when using a logging task).
The issue I was having was that the original action did not await the completion of the logging task, so when the log finishes and tries to rejoin the original thread a nullReference is thrown because the original thread has terminated.
This was solved for me by making sure that the request controller awaited the logging function.
I had this problem with .net4.5 when my web service was invoking another WCF service in an async manner. I simply appended a short timed Wait()
as I didn't care about the response (telemetry event).
public static void Event(string key, string message) {
Telemetry.Event(key, message).Wait(100);
}