首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cassini/WebServer.WebDev、NUnit和AppDomainUnloadedException

Cassini/WebServer.WebDev、NUnit和AppDomainUnloadedException
EN

Stack Overflow用户
提问于 2009-02-18 14:58:42
回答 1查看 3.8K关注 0票数 9

我使用Cassini/WebServer.WebDev使用NUnit运行一些WebService的自动化测试。

我没做什么花哨的事,只是

代码语言:javascript
复制
public class WebService{
  Microsoft.VisualStudio.WebHost.Server _server;

  public void Start(){
    _server = new Microsoft.VisualStudio.WebHost.Server(_port, "/", _physicalPath);
  }

  public void Dispose()
  {
    if (_server != null)
    {
      _server.Stop();
      _server = null;
    }
  }
}
[TestFixture]
public void TestFixture{
  [Test]
  public void Test(){
    using(WebService webService = new WebService()){
      webService.Start();
      // actual test invoking the webservice
    }
  }
}

,但是当我使用nunit- when e.exe运行它时,我得到了以下输出:

代码语言:javascript
复制
NUnit version 2.5.0.9015 (Beta-2)
Copyright (C) 2002-2008 Charlie Poole.\r\nCopyright (C) 2002-2004 James W. Newki
rk, Michael C. Two, Alexei A. Vorontsov.\r\nCopyright (C) 2000-2002 Philip Craig
.\r\nAll Rights Reserved.

Runtime Environment -
   OS Version: Microsoft Windows NT 6.0.6001 Service Pack 1
  CLR Version: 2.0.50727.1434 ( Net 2.0.50727.1434 )

ProcessModel: Default    DomainUsage: Default
Execution Runtime: net-2.0.50727.1434
.....
Tests run: 5, Errors: 0, Failures: 0, Inconclusive: 0 Time: 28,4538451 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0


Unhandled exceptions:
1) TestCase1 : System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.
2) TestCase2 : System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.
3) TestCase3 : System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.
4) TestCase4 : System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.

如果我在调试器下运行nunit-控制台,则在调试控制台中得到以下输出:

代码语言:javascript
复制
[...]
The thread 0x1974 has exited with code 0 (0x0).
############################################################################
##############                 S U C C E S S               #################
############################################################################
Executed tests       : 5
Ignored tests        : 0
Failed tests         : 0
Unhandled exceptions : 4
Total time           : 25,7092944 seconds
############################################################################
The thread 0x1bd4 has exited with code 0 (0x0).
The thread 0x10f8 has exited with code 0 (0x0).
The thread '<No Name>' (0x1a80) has exited with code 0 (0x0).
A first chance exception of type 'System.AppDomainUnloadedException' occurred in System.Web.dll
##### Unhandled Exception while running 
System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.
   at System.Web.Hosting.ApplicationManager.HostingEnvironmentShutdownComplete(String appId, IApplicationHost appHost)
   at System.Web.Hosting.HostingEnvironment.OnAppDomainUnload(Object unusedObject, EventArgs unusedEventArgs)
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in System.Web.dll
The thread 0x111c has exited with code 0 (0x0).
The program '[0x1A64] nunit-console.exe: Managed' has exited with code -100 (0xffffff9c).

有人知道是什么原因造成的吗?

EN

回答 1

Stack Overflow用户

发布于 2009-07-20 21:26:36

我也有同样的问题,但没有使用卡西尼。相反,我有自己的基于System.Net.HttpListener的web服务器托管,通过ASP.Net支持运行在通过System.Web.Hosting.ApplicationHost.CreateApplicationHost()创建的不同应用程序域中的System.Web.HttpRuntime。这本质上就是Cassini的工作方式,只是Cassini在套接字层工作,并实现了System.Net.HttpListener本身提供的许多功能。

无论如何,为了解决我的问题,在让System.Web.HttpRuntime.Close()卸载我的应用程序域之前,我需要调用NUnit。为此,我在我的主机代理类中公开了一个新的Close()方法,该方法由我的SetupFixture类的TearDown方法调用,该方法调用System.Web.HttpRuntime.Close()

我通过.Net Reflector查看了Cassini实现,尽管它使用了System.Web.HttpRuntime.ProcessRequest(),但似乎在任何地方都没有调用System.Web.HttpRuntime.Close()

我不太确定如何继续使用预构建的Cassini实现(Microsoft.VisualStudio.WebHost.Server),因为您需要在由Cassini创建的应用程序域中调用System.Web.HttpRuntime.Close()以承载ASP.Net。

供参考,这里有一些我的工作单元测试与嵌入式网络托管。

我的WebServerHost类是一个非常小的类,它允许将请求封送到System.Web.Hosting.ApplicationHost.CreateApplicationHost()创建的应用程序域中。

代码语言:javascript
复制
using System;
using System.IO;
using System.Web;
using System.Web.Hosting;

public class WebServerHost :
    MarshalByRefObject
{
    public void
    Close()
    {
        HttpRuntime.Close();
    }

    public void
    ProcessRequest(WebServerContext context)
    {
        HttpRuntime.ProcessRequest(new WebServerRequest(context));
    }
}

WebServerContext类只是一个从System.MarshalByRefObject派生的System.Net.HttpListenerContext实例的包装器,它允许来自新的ASP.Net宿主域的调用返回到我的域中。

代码语言:javascript
复制
using System;
using System.Net;

public class WebServerContext :
    MarshalByRefObject
{
    public
    WebServerContext(HttpListenerContext context)
    {
        this.context = context;
    }

    //  public methods and properties that forward to HttpListenerContext omitted

    private HttpListenerContext
    context;
}

WebServerRequest类只是抽象System.Web.HttpWorkerRequest类的一个实现,它通过WebServerContext类从ASP.Net宿主域调用回我的域中。

代码语言:javascript
复制
using System;
using System.IO;
using System.Web;

class WebServerRequest :
    HttpWorkerRequest
{
    public
    WebServerRequest(WebServerContext context)
    {
        this.context = context;
    }

    //  implementation of HttpWorkerRequest methods omitted; they all just call
    //  methods and properties on context

    private WebServerContext
    context;
}

WebServer类是用于启动和停止web服务器的控制器。启动时,使用我的ASP.Net类作为代理创建WebServerHost托管域,以允许交互。还启动了一个System.Net.HttpListener实例,并启动了一个单独的线程来接受连接。当建立连接时,线程池中将启动一个工作线程来处理请求,同样是通过我的WebServerHost类。最后,当web服务器停止时,侦听器停止,控制器等待接受连接的线程退出,然后侦听器关闭。最后,HTTP运行时也通过调用WebServerHost.Close() 方法(.)关闭。

代码语言:javascript
复制
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Web.Hosting;

class WebServer
{
    public static void
    Start()
    {
        lock ( typeof(WebServer) )
        {
            //  do not start more than once
            if ( listener != null )
                return;

            //  create web server host in new AppDomain
            host =
                (WebServerHost)ApplicationHost.CreateApplicationHost
                (
                    typeof(WebServerHost),
                    "/",
                    Path.GetTempPath()
                );

            //  start up the HTTP listener
            listener = new HttpListener();
            listener.Prefixes.Add("http://*:8182/");
            listener.Start();

            acceptConnectionsThread = new Thread(acceptConnections);
            acceptConnectionsThread.Start();
        }
    }

    public static void
    Stop()
    {
        lock ( typeof(WebServer) )
        {
            if ( listener == null )
                return;

            //  stop listening; will cause HttpListenerException in thread blocked on GetContext()  
            listener.Stop();

            //  wait connection acceptance thread to exit
            acceptConnectionsThread.Join();
            acceptConnectionsThread = null;

            //  close listener
            listener.Close(); 
            listener = null;

            //  close host
            host.Close();
            host = null;
        }
    }

    private static WebServerHost
    host = null;

    private static HttpListener
    listener = null;

    private static Thread
    acceptConnectionsThread;

    private static void
    acceptConnections(object state)
    {
        while ( listener.IsListening )
        {
            try
            {
                HttpListenerContext context = listener.GetContext();
                ThreadPool.QueueUserWorkItem(handleConnection, context);
            }
            catch ( HttpListenerException e )
            {
                //  this exception is ignored; it will be thrown when web server is stopped and at that time
                //  listening will be set to false which will end the loop and the thread
            }
        }
    }

    private static void
    handleConnection(object state)
    {
        host.ProcessRequest(new WebServerContext((HttpListenerContext)state));
    }
}

最后,这个Initialization类被标记为NUnit SetupFixture属性,用于在单元测试启动时启动web服务器,并在测试完成后关闭它。

代码语言:javascript
复制
using System;
using NUnit.Framework;

[SetUpFixture]
public class Initialization
{
    [SetUp]
    public void
    Setup()
    {
        //  start the local web server
        WebServer.Start();
    }

    [TearDown]
    public void
    TearDown()
    {
        //  stop the local web server
        WebServer.Stop();
    }
}

我知道这并不完全是在回答这个问题,但我希望你能找到有用的信息。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/561402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档