I have the same problem. The problem seems to be related to the soft threads of the termination ports (in ThreadPool) that WCF used to handle async IO.
When ServiceHost.Close() , it will signal all those threads that work, but they will not disappear immediately, that is, they can survive the end of the ServiceHost.Close() operation. Thus, the βshutdownβ procedure is performed with the actual unloading of the AppDomain caused by NUnit due to the end of the test run.
Basically, a simple Thread.Sleep(<a couple of seconds>) after ServiceHost.Close() "fixes" this Thread.Sleep(<a couple of seconds>)
After a long search on the Internet, I could not find a reliable solution for this problem (to select similar problems, not all due to the same reason, though, google "unit test appdomainunloadedexception") somehow suppress this warning.
I tried different bindings and transports (including NullTransport ), but to no avail.
In the end, I decided on this "solution":
static void PreventPrematureAppDomainUnloadHack() {
A timeout of 3 seconds is absolutely arbitrary, as well as a 5 ms wait between each attempt. Sometimes I get a timeout, but most of the time it works.
I am sure that this code is called once for each test assembly (i.e. via a static ctor of reference type).
As usual in such cases, YMMV.
source share