System.AppDomainUnloadedException: 试图访问已卸载的 AppDomain

System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain

创建服务总线主题时在输出中出现以下异常 window。因此,后续构建失败,因为 vstest.executionengine.exe 进程未正确终止。必须终止此过程才能使其正常工作。如何解决这个问题?使用最新版本的 servicebus 4.0

    [TestMethod]
    public void TestMethod1()
    {

        string connectionString = "Sb-connection-string";

        var namespaceManager =
            NamespaceManager.CreateFromConnectionString(connectionString);

        if (!namespaceManager.TopicExists("TestTopic"))
        {
            namespaceManager.CreateTopic("TestTopic");
        }

    }

Exception:
System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain. This can happen if the test(s) started a thread but did not stop it. Make sure that all the threads started by the test(s) are stopped before completion.

我这边也复制了。根据异常来看,好像是一个线程没有停止。

How to resolve this?

为了确保测试启动的所有线程在完成前停止。我在代码末尾添加 Thread.Sleep(2*1000); 。 然后就解决了。

   string connectionString = "Sb-connection-string";

   var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

   if (!namespaceManager.TopicExists("TestTopic"))
   {
       namespaceManager.CreateTopic("TestTopic");
   }

   Thread.Sleep(2*1000);

我有同样的问题,我看到 vstest.executionengine.x86.exe 在测试执行后没有关闭,当 运行 测试两次时,它启动另一个进程并获得错误,我得到的解决方案是取消选中 Visual Studio

中的 Test -> Test Settings called "Keep Test Execution Engine Running"