Consider killing off the TaskFactory


We are moving to NServiceBus for some of our parts of the software, and suddenly the library stopped working (connection timeouts). So after a few hours of double checking everything, I decided to download the source and try it myself.

I found that the Connect method spawns a new listener thread (ExecuteThread). In this thread (NET40), the TaskFactory is used. However, when ran in my NServiceBus node the thread nevers gets spawned though the max number of threads is not just 1.

I have changed the ExecuteThread from:

Task.Factory.StartNew(action, TaskCreationOptions.LongRunning);


ThreadPool.QueueUserWorkItem((o) => { action(); });

And now it works great again.

Now to prevent any discussion whether this is or not is a bug in NServiceBus, it seems that the TaskFactory is not able to spawn the thread where the ThreadPool is able to. I actually don't see any reason to use the TaskFactory here.
Closed Apr 4, 2013 at 4:35 PM by olegkap
Fixed in 24900


olegkap wrote Apr 4, 2013 at 4:35 PM


Thanks for this info.
I originally used TaskFactory since my understanding was that in .4.0 we should use it but I guess they still have bugs, and you right there is no difference between method to another one.

I just commited 24900 changeset so you take a look at it.


wrote Apr 4, 2013 at 4:35 PM

GeertvanHorrik wrote Apr 4, 2013 at 5:08 PM

Excellent, thanks! When will the new version be released via nuget? :-)

olegkap wrote Apr 4, 2013 at 6:13 PM

I am not sure yet but will try to make it soon,
Busy with other projects at work which leaves me almost no time other then answering some e-mails :(

GeertvanHorrik wrote Apr 6, 2013 at 10:44 AM

I understand that you are busy, but I need this fix asap. Are there other things you are waiting for? Can I help you with something?

olegkap wrote Apr 6, 2013 at 6:45 PM

I just made a new release 2013.4.7 which actually tried to do for tomorrow.
I also updated NUGET so it should be available there as well.
Please let me know if you have any further problems.


GeertvanHorrik wrote Apr 6, 2013 at 7:39 PM

Cool, thanks for your awesome support!

wrote May 16, 2013 at 1:13 PM