简体   繁体   English

跨AppDomains实施引用计数的最佳方法是什么?

[英]What would be the best way to implement reference counting across AppDomains?

I feel like I may be going about something entirely wrong, so if you have an alternate approach, please let me know. 我觉得我可能会发生一些完全错误的事情,所以如果你有其他方法,请告诉我。

I'm building a service with service stack. 我正在使用服务堆栈构建服务。 I'm testing that service with Xunit and NCrunch. 我正在使用Xunit和NCrunch测试该服务。 I want my tests to run in parallel, and they currently do thanks to NCrunch... However, there is one problem. 我希望我的测试并行运行,他们目前要感谢NCrunch ...但是,有一个问题。

All of my tests rely on the fact that my service is up and running - it's a shared resource.. a dependency. 我的所有测试都依赖于我的服务启动并运行这一事实 - 它是一个共享资源......依赖。 I don't particularly care for it, but it seems that service stack does not let you spin up multiple instances for each test (plus, I'd need to do some nasty table prefixes or something inside postgresql - blek)... 我并不特别关心它,但似乎服务堆栈不允许你为每个测试旋转多个实例(另外,我需要做一些令人讨厌的表前缀或postgresql里面的东西 - blek)...

All I need is some way to start my service via code before any tests have run - and stop it when all tests have finished. 我需要的是在任何测试运行之前通过代码启动我的服务的一些方法 - 并在所有测试完成后停止它。 I need it to be via code because I don't receive code coverage information on server-side code if I have to start the service manually. 我需要通过代码,因为如果我必须手动启动服务,我不会收到服务器端代码的代码覆盖率信息。

I can manually start the service and all my tests run in parallel and pass perfectly fine - but the problem is that I can't seem to find a good hook for a 'pre-test' and 'post-test' action. 我可以手动启动服务,我的所有测试都是并行运行并且完全正常 - 但问题是我似乎找不到一个好的钩子来进行'预测试'和'后测试'动作。

I have looked into using a semaphore as a sort of 'reference counting' solution, but it's actually the opposite of what I'd need. 我已经研究过使用信号量作为一种“引用计数”解决方案,但它实际上与我需要的信号相反。 I don't want to have to specify ahead of time how many tests will be ran.. I need something similar to a dijkstra counting semaphore - but I haven't found an implementation that works based upon a named (system-wide) semaphore. 我不想提前指定将运行多少测试..我需要类似于计算信号量的dijkstra - 但我没有找到一个基于命名(系统范围)信号量工作的实现。

A Mutex won't work - because the test that happens to start the service still has to know when all of the other tests finish before stopping the service - it cannot just do so in the Dispose method. Mutex不起作用 - 因为启动服务的测试仍然必须知道在停止服务之前所有其他测试何时完成 - 它不能在Dispose方法中这样做。

A lot of the normal solutions related to synchronizing objects between threads does not apply in this situation because the tests are ran inside their own AppDomain. 许多与线程之间的对象同步相关的常规解决方案在这种情况下不适用,因为测试是在自己的AppDomain中运行的。

Thanks for all your help in advance. 感谢您的所有帮助。

I wrote a blog post about a solution to a similar problem a couple of years ago. 几年前我写了一篇关于解决类似问题的博客文章。

http://netvignettes.wordpress.com/2012/01/03/sharepoint-net-3-5-woes-and-synchronization-between-processes/ http://netvignettes.wordpress.com/2012/01/03/sharepoint-net-3-5-woes-and-synchronization-between-processes/

Scroll down to the code blocks and start reading around there, so you can skip the SharePoint cruft at the top. 向下滚动到代码块并开始在那里阅读,这样您就可以跳过顶部的SharePoint cruft。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM