9

Cassini/WebServer.WebDev を使用して、NUnit を使用して WebService の自動テストを実行しています。

派手なことをしているわけではなく、ただ

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-console.exe を使用して実行すると、次の出力が得られます。

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-console を実行すると、デバッグ コンソールに次の出力が表示されます。

[...]
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).

これを引き起こしている可能性のあるアイデアはありますか?

4

1 に答える 1

7

同じ問題がありましたが、Cassini を使用していませんでした。System.Net.HttpListener. _ System.Web.HttpRuntime_ System.Web.Hosting.ApplicationHost.CreateApplicationHost()_ これは基本的に Cassini の動作方法と同じですが、Cassini がソケット層で動作し、System.Net.HttpListenerそれ自体が提供する多くの機能を実装する点が異なります。

とにかく、私の問題を解決するには、System.Web.HttpRuntime.Close()NUnit にアプリケーション ドメインをアンロードさせる前に呼び出す必要がありました。Close()[SetupFixture] クラスの [TearDown] メソッドによって呼び出される新しいメソッドをホスト プロキシ クラスに公開し、そのメソッドが を呼び出すことでこれを行いましたSystem.Web.HttpRuntime.Close()

.Net Reflector を介して Cassini の実装を調べたところ、.Net Reflector を使用していますが、どこにもSystem.Web.HttpRuntime.ProcessRequest()呼び出しがないようです。System.Web.HttpRuntime.Close()

ASP.Net をホストするために Cassini によって作成されたアプリケーション ドメイン内で呼び出しを行うMicrosoft.VisualStudio.WebHost.Server必要があるため、ビルド済みの Cassini 実装 ( ) を使用し続ける方法が正確にはわかりません。System.Web.HttpRuntime.Close()

参考までに、組み込み Web ホスティングを使用した単体テストの一部を以下に示します。

WebServerHostのクラスは、によって作成されたアプリケーション ドメインに要求をマーシャリングできる非常に小さなクラスですSystem.Web.Hosting.ApplicationHost.CreateApplicationHost()

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 ホスティング ドメインからの呼び出しを自分のドメインにコールバックできるようにします。

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;
}

このクラスは、クラスを介して ASP.Net ホスティング ドメインから私のドメインにコールバックするWebServerRequest抽象クラスの単なる実装です。System.Web.HttpWorkerRequestWebServerContext

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()メソッドへの呼び出しによって閉じられます。

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 サーバーを起動し、完了時にシャットダウンするために使用されます。

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();
    }
}

これが質問への正確な回答ではないことは承知していますが、この情報がお役に立てば幸いです。

于 2009-07-20T21:26:36.153 に答える