RowVersion NUnit Tests failing with Exception

Hi @mythz,

Happy New Year!

Here is a head scratcher. We’ve started adding the RowVersion property to our DB types and response DTOs for optimistic concurrency. Our test setup is async and is called via the SetUp attribute. Before adding the RowVersion property these tests ran fine after adding the RowVersion attribute we get an exception “System.InvalidOperationException : The current SynchronizationContext may not be used as a TaskScheduler.” I’ve created this repro to show the issue.

Thanks,
Deon

I’d avoid using async in NUnit setup code in general, but you would need to use [OneTimeSetUp]:

Thanks @mythz will make those calls sync since we do need them to execute before each test. Any idea why this only showed up now once we’ve added RowVersion?

Cheers

[OneTimeSetUp] was also a bust, just removed all the async calls in the setup

If I’d guess I’d say it’s the multiple dependent async calls behind the scenes which forces a context switch requiring a SynchronizationContext.

As async behaviour is dependent on an external Synchronization Context I’d only be using it where it’s explicitly supported.

1 Like