Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 362083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:12:40+00:00 2026-05-12T13:12:40+00:00

I want to load the ASPNET runtime, run one or more pages, then unload

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:12:40+00:00Added an answer on May 12, 2026 at 1:12 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load some helper in a model. How to do this? Tried
I want to load this simple something into my Editor: Write:-repeat,write(hi),nl,fail. So that it
I've one asp.net page and I want to load text into the textArea control
how to call more than one method at same time in my page load
I am developing one web application in that I want to check load time
at first sorry my bad English I want load model in my plugin controller
I want to load defined folders into my website from same system, now I
I want to load key/values configuration pairs stored in XML file. To bind a
i want to load some photos into my sd card image in the emulator.
I Want to load Image size 2550X3300 (i.e 1.7 Mb size), i have loaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.