aspxファイルをローカルで実行する方法

aspxを作るのに毎回IISにコピーするのも面倒。そもそもIISを立ち上げること自体アレだ。ということでローカルでaspxを実行させたいというわけだ。

aspxをローカルで実行させるのはSystem.Web.Hosting.AplicationHostを使う。この使い方は、ApplicationHostでググればするわかる。
たとえば、以下のようなものだ。

しかし、これらは詳しくない人にとって、肝心なものが抜けており、素直にやるとエラーになり困ってしまう。

ソース

上のサンプルから、必要な部分のみを切り出したソースは以下のようになる。
ExeHost.cs

using System;
using System.IO;
using System.Web;
using System.Web.Hosting;

public class ExeHost : MarshalByRefObject {
  public void ProcessRequest(String page) {
    HttpWorkerRequest hwr = 
      new SimpleWorkerRequest(page, null, Console.Out);
    HttpRuntime.ProcessRequest(hwr);
  }
  
  public static void Main(string[] args) {
    ExeHost host = (ExeHost) ApplicationHost.CreateApplicationHost(typeof(ExeHost), "/", Directory.GetCurrentDirectory());
    foreach (String page in args) {
      host.ProcessRequest(page);
    }
  }
}

また、hello.aspxを一番シンプルな以下のようにする:



Today is <%= Now %>.


エラーが出る実行

これを同じディレクトリにおいて実行してみる。

$ ls
ExeHost.cs hello.aspx
$ csc ExeHost.cs
...
$ ls
ExeHost.cs ExeHost.exe hello.aspx
$ ./ExeHost.exe hello.aspx

Unhandled Exception: System.IO.FileNotFoundException: File or assembly name 'ExeHost, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null', or one of its dependencies, was not found.
File name: 'ExeHost, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.IO.FileNotFoundException: 指定されたファイルが見つかりません。
   at System.Web.Hosting.HostingEnvironment.CreateInstance(Type type)
   at System.Web.Hosting.ApplicationManager.CreateInstanceInNewWorkerAppDomain(Type type, String appId, String virtualPath, String physicalPath)
   at System.Web.Hosting.ApplicationHost.CreateApplicationHost(Type hostType, String virtualDir, String physicalDir)
   at ExeHost.Main(String[] args)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Unknown signal 79

このエラーが出て終わるまで、数分かかるかもしれないので注意。

正解

これはexeファイル(というかExeHostアセンブリ)を探して見つからないというエラーだ。どうやらApplicationHostは自分自身のexeを中で起動しようとし、exeを探すけど、みつからないというわけだ。
ということで、いろいろやってみたところ「カレントディレクトリ/bin」にあると見つけてくれるようだ。以下がそのトレース。

$ ls
ExeHost.cs hello.aspx
$ csc ExeHost.cs
...
$ ls
ExeHost.cs ExeHost.exe hello.aspx
$ mkdir bin

$ mv ExeHost.exe bin/

$ ls
ExeHost.cs bin/ hello.aspx

$ ls bin/
ExeHost.exe

$ ./bin/ExeHost.exe hello.aspx


Today is 2004/07/05 12:33:12.



$

こちらも起動までにはちょっと時間がかかるけど、きちんと実行されました。

bin以下にコピーしたものがあれば、「./ExeHost.exe hello.aspx」でもいいようです。