views:

219

answers:

1

I want to load the ASPNET runtime, run one or more pages, then unload it. This is for testing purposes. It's not UI testing; I'm really just testing the use of a library in an ASPNET context.

Normally this kind of thing is done with a call to System.Web.Hosting.ApplicationHost.CreateApplicationHost .

This is how I have it currently:

public class Manager : System.MarshalByRefObject
{
    private void HostedDomainHasBeenUnloaded(object source, System.EventArgs e)
    {
        aspNetHostIsUnloaded.Set();
    }

    private ManualResetEvent aspNetHostIsUnloaded;


    public void Run(string[] pages)
    {
        bool cleanBin = false;
        MyAspNetHost host = null;

        try
        {
            if (!Directory.Exists("bin"))
            {
                cleanBin = true;
                Directory.CreateDirectory("bin");
            }

            var a = System.Reflection.Assembly.GetExecutingAssembly();
            string destfile= Path.Combine("bin", Path.GetFileName(a.Location));
            File.Copy(a.Location, destfile, true);

            host = 
                (MyAspNetHost) System.Web.Hosting.ApplicationHost.CreateApplicationHost
                ( typeof(MyAspNetHost), 
                  "/foo",   // virtual dir
                  System.IO.Directory.GetCurrentDirectory() // physical dir
                  );

            aspNetHostIsUnloaded = new ManualResetEvent(false);

            host.GetAppDomain().DomainUnload += this.HostedDomainHasBeenUnloaded;

            foreach (string page in pages)
                host.ProcessRequest(page);

        }
        finally
        {
            // tell the host to unload
            if (host!= null)
            {
                AppDomain.Unload(host.GetAppDomain());

                // wait for it to unload
                aspNetHostIsUnloaded.WaitOne();

                // remove the bin directory
                if (cleanBin)
                {
                    Directory.Delete("bin", true);
                }
            }
        }
    }

}

This is the custom ASP.NET host:

public class MyAspNetHost : System.MarshalByRefObject 
{

    public void ProcessRequest(string page)
    {
        var request = new System.Web.Hosting.SimpleWorkerRequest(page,               // page being requested
                                                                 null,               // query
                                                                 System.Console.Out  // output
                                                                 );
        System.Web.HttpRuntime.ProcessRequest(request);
    }


    public AppDomain GetAppDomain()
    {
        return System.Threading.Thread.GetDomain();
    }    
}

This works ok. But I'd like to avoid the creation of the bin directory, and the copying of the assembly into it, if possibble.

Is it possible to use CreateApplicationHost, and tell ASPNET to load assemblies from the current directory, or an arbitrary directory, rather than bin?

EDIT :: simplified the code a bit. EDIT2 :: I looked at womp's answer but it seems like doing a lot of work to avoid a little. Any other ideas?

A: 

This is a great question. I remember having to deal with a similar issue a couple years ago (I wanted to have an applicationhost use a custom config file) and bookmarked this blog post which I used to get me started.

It's probably way overkill for what you actually want, but perhaps it can point you in the right direction.

womp
Wow, new process, environment variables, GAC... It sounds like a ton of work to avoid me creating and removing a directory. It's the right idea though... I'll have to see if I can simplify it.
Cheeso