如何在多个方法范围内使用HttpListener?

时间:2019-06-10 18:03:35

标签: c# http asynchronous httplistener

我试图在充当控制台应用程序启动器的测试框架中找到跨多种方法使用class Test { // mock server is just a wrapper class around HttpListener MockServer mockServer; // This can set global vars in my test app SetupEnvironment() Run() { // Then I have smaller scope methods that called multiple times with // each application launch foreach (var app in apps){ SetupApplication() StartApplication() } } // clean up method that is called at end of all app executions CleanEnvironment() } 的最佳方法。

我当前的执行流程如下:SetupEnv-> Run-> CleanEnv

(伪代码)

SetupApplication

在某些情况下,我必须同时在 StartApplicationSetupApplication中监听一些HTTP请求。但是,StartApplication将在调用SetupEnvironment之前完成执行,因此我不会尝试同时监听两个方法。当前,我在mockServer中初始化HttpListener并在SetupEnvironment() { mockServer = new MockServer(); mockServer.Start(); // same as HttpListener.Start() } SetupApplication() { while(!mockServer.validationDone) { // listen at prefixes and do validation stuff } } StartApplication() { while(!mockServer.appDone) { // listen at prefixes and do start app stuff } } CleanEnvironment() { mockServer.Close(); // same as HttpListener.Close() } 包装类中监视某些标志:

HttpListener

这是跨方法利用using的最佳方法吗?还是应该在SetupEnvironment语句中包装每次使用?还是应该在HttpListener中初始化后开始监听?

我的问题基本上可以归结为:跨多个非同时执行的方法来管理fun的生存期的最佳方法是什么?

0 个答案:

没有答案