<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Installation and Setup</title><link>http://forums.asp.net/17.aspx</link><description>All about installing and getting ASP.NET running on your system.</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>ASP.NET pages inside unit tests - AppDomainUnloadedException</title><link>http://forums.asp.net/thread/2753300.aspx</link><pubDate>Tue, 18 Nov 2008 17:56:09 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2753300</guid><dc:creator>LLook</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2753300.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=17&amp;PostID=2753300</wfw:commentRss><description>&lt;p&gt;Hello, I am experiencing a difficulty with running ASPX pages outside of IIS and even outside of HttpRuntime. These practices are not well documented, but I hope someone has already solved similar problem.&lt;/p&gt;
&lt;p&gt;My goal is simple: Somehow create an instance of an ASPX page, run it&amp;nbsp;and test what happens. Test values of some page&amp;#39;s properties, properties of its controls and so on.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s where I came:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div&gt;Create a new&amp;nbsp;application domain (ApplicationHost.CreateApplicationHost).&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Compile the page and get its type&amp;nbsp;(ClientBuildManager).&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Create a new HttpContext.&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Construct the page (Type.GetConstructor...).&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Then I can subscribe some events, if I want to test the page during its&amp;nbsp;life-cycle.&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Run the page (page.ProcessRequest).&amp;nbsp;Documentation says that this method is&amp;nbsp;&amp;quot;not intended to be used directly from your code&amp;quot;. However I didn&amp;#39;t find any other way...&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;And it works. This is the full code of a sample test fixture:&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;b id="1"&gt;1    &lt;/b&gt;&lt;span class="kwd"&gt;using&lt;/span&gt; System;
&lt;b id="2"&gt;2    &lt;/b&gt;&lt;span class="kwd"&gt;using&lt;/span&gt; System.Web;
&lt;b id="3"&gt;3    &lt;/b&gt;&lt;span class="kwd"&gt;using&lt;/span&gt; System.Web.Compilation;
&lt;b id="4"&gt;4    &lt;/b&gt;&lt;span class="kwd"&gt;using&lt;/span&gt; System.Web.Hosting;
&lt;b id="5"&gt;5    &lt;/b&gt;&lt;span class="kwd"&gt;using&lt;/span&gt; System.Web.UI;
&lt;b id="6"&gt;6    &lt;/b&gt;&lt;span class="kwd"&gt;using&lt;/span&gt; NUnit.Framework;
&lt;b id="7"&gt;7    &lt;/b&gt;&lt;span class="kwd"&gt;using&lt;/span&gt; NUnit.Framework.SyntaxHelpers;
&lt;b id="8"&gt;8    &lt;/b&gt;
&lt;b id="9"&gt;9    &lt;/b&gt;&lt;span class="kwd"&gt;namespace&lt;/span&gt; PageSharp.Incubator.Tests.Learning
&lt;b id="10"&gt;10   &lt;/b&gt;{
&lt;b id="11"&gt;11   &lt;/b&gt;    &lt;span class="cmt"&gt;/// &amp;lt;summary&amp;gt;
&lt;b id="12"&gt;12   &lt;/b&gt;    /// Test how could pages be programatically instantiated and executed.
&lt;b id="13"&gt;13   &lt;/b&gt;    /// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;b id="14"&gt;14   &lt;/b&gt;    [TestFixture]
&lt;b id="15"&gt;15   &lt;/b&gt;    &lt;span class="kwd"&gt;public class&lt;/span&gt; PageLifeCycleLearningTest : MarshalByRefObject
&lt;b id="16"&gt;16   &lt;/b&gt;    {
&lt;b id="17"&gt;17   &lt;/b&gt;        &lt;span class="kwd"&gt;private string&lt;/span&gt; _virtualDir = &lt;span class="st"&gt;&amp;quot;/&amp;quot;&lt;/span&gt;;
&lt;b id="18"&gt;18   &lt;/b&gt;        &lt;span class="kwd"&gt;private string&lt;/span&gt; _appDir;
&lt;b id="19"&gt;19   &lt;/b&gt;        &lt;span class="kwd"&gt;private&lt;/span&gt; PageLifeCycleLearningTest _host;
&lt;b id="20"&gt;20   &lt;/b&gt;
&lt;b id="21"&gt;21   &lt;/b&gt;        &lt;span class="kwd"&gt;public&lt;/span&gt; PageLifeCycleLearningTest()
&lt;b id="22"&gt;22   &lt;/b&gt;        {
&lt;b id="23"&gt;23   &lt;/b&gt;            _appDir = System.IO.Directory.GetCurrentDirectory().TrimEnd(&lt;span class="st"&gt;&amp;quot;bin&amp;quot;&lt;/span&gt;.ToCharArray());
&lt;b id="24"&gt;24   &lt;/b&gt;        }
&lt;b id="25"&gt;25   &lt;/b&gt;
&lt;b id="26"&gt;26   &lt;/b&gt;        [TestFixtureSetUp]
&lt;b id="27"&gt;27   &lt;/b&gt;        &lt;span class="kwd"&gt;public void&lt;/span&gt; SetUp()
&lt;b id="28"&gt;28   &lt;/b&gt;        {
&lt;b id="29"&gt;29   &lt;/b&gt;            _host = (PageLifeCycleLearningTest)
&lt;b id="30"&gt;30   &lt;/b&gt;                ApplicationHost.CreateApplicationHost(&lt;span class="kwd"&gt;typeof&lt;/span&gt;(PageLifeCycleLearningTest), _virtualDir, _appDir);
&lt;b id="31"&gt;31   &lt;/b&gt;        }
&lt;b id="32"&gt;32   &lt;/b&gt;
&lt;b id="33"&gt;33   &lt;/b&gt;        [Test]
&lt;b id="34"&gt;34   &lt;/b&gt;        &lt;span class="kwd"&gt;public void&lt;/span&gt; TestSomeFact()
&lt;b id="35"&gt;35   &lt;/b&gt;        {
&lt;b id="36"&gt;36   &lt;/b&gt;            _host.TestSomeFact_Run();
&lt;b id="37"&gt;37   &lt;/b&gt;        }
&lt;b id="38"&gt;38   &lt;/b&gt;
&lt;b id="39"&gt;39   &lt;/b&gt;        &lt;span class="kwd"&gt;private void&lt;/span&gt; TestSomeFact_Run()
&lt;b id="40"&gt;40   &lt;/b&gt;        {
&lt;b id="41"&gt;41   &lt;/b&gt;            var page = CreatePage(&lt;span class="st"&gt;&amp;quot;~/default.aspx&amp;quot;&lt;/span&gt;, String.Empty);
&lt;b id="42"&gt;42   &lt;/b&gt;            page.PreInit += &lt;span class="kwd"&gt;new&lt;/span&gt; EventHandler(TestSomeFact_PreInit);
&lt;b id="43"&gt;43   &lt;/b&gt;            page.ProcessRequest(HttpContext.Current);
&lt;b id="44"&gt;44   &lt;/b&gt;
&lt;b id="45"&gt;45   &lt;/b&gt;            var ctl = (System.Web.UI.WebControls.Label) page.FindControl(&lt;span class="st"&gt;&amp;quot;ctl00$ContentPlaceHolder1$WebUserControl11$Label1&amp;quot;&lt;/span&gt;);
&lt;b id="46"&gt;46   &lt;/b&gt;            Assert.That(ctl.Text, Is.EqualTo(&lt;span class="st"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;));
&lt;b id="47"&gt;47   &lt;/b&gt;        }
&lt;b id="48"&gt;48   &lt;/b&gt;
&lt;b id="49"&gt;49   &lt;/b&gt;        &lt;span class="kwd"&gt;private void&lt;/span&gt; TestSomeFact_PreInit(&lt;span class="kwd"&gt;object&lt;/span&gt; sender, EventArgs e)
&lt;b id="50"&gt;50   &lt;/b&gt;        {
&lt;b id="51"&gt;51   &lt;/b&gt;            var page = (Page)sender;
&lt;b id="52"&gt;52   &lt;/b&gt;            var ctl = (System.Web.UI.WebControls.Label)page.FindControl(&lt;span class="st"&gt;&amp;quot;ctl00$ContentPlaceHolder1$WebUserControl11$Label1&amp;quot;&lt;/span&gt;);
&lt;b id="53"&gt;53   &lt;/b&gt;            Assert.That(ctl, Is.Null);
&lt;b id="54"&gt;54   &lt;/b&gt;        }
&lt;b id="55"&gt;55   &lt;/b&gt;
&lt;b id="56"&gt;56   &lt;/b&gt;        &lt;span class="cmt"&gt;/// &amp;lt;summary&amp;gt;
&lt;b id="57"&gt;57   &lt;/b&gt;        /// Compile and instantiate a page.
&lt;b id="58"&gt;58   &lt;/b&gt;        /// &amp;lt;/summary&amp;gt;
&lt;b id="59"&gt;59   &lt;/b&gt;        /// &amp;lt;param name=&amp;quot;virtualPath&amp;quot;&amp;gt;Virtual path.&amp;lt;/param&amp;gt;
&lt;b id="60"&gt;60   &lt;/b&gt;        /// &amp;lt;param name=&amp;quot;queryString&amp;quot;&amp;gt;Query string.&amp;lt;/param&amp;gt;
&lt;b id="61"&gt;61   &lt;/b&gt;        /// &amp;lt;returns&amp;gt;Instance of the page.&amp;lt;/returns&amp;gt;&lt;/span&gt;
&lt;b id="62"&gt;62   &lt;/b&gt;        &lt;span class="kwd"&gt;private&lt;/span&gt; Page CreatePage(&lt;span class="kwd"&gt;string&lt;/span&gt; virtualPath, &lt;span class="kwd"&gt;string&lt;/span&gt; queryString)
&lt;b id="63"&gt;63   &lt;/b&gt;        {
&lt;b id="64"&gt;64   &lt;/b&gt;            var cbm = &lt;span class="kwd"&gt;new&lt;/span&gt; ClientBuildManager(_virtualDir, _appDir);
&lt;b id="65"&gt;65   &lt;/b&gt;            var type = cbm.GetCompiledType(virtualPath);
&lt;b id="66"&gt;66   &lt;/b&gt;            var writer = &lt;span class="kwd"&gt;new&lt;/span&gt; System.IO.StringWriter();
&lt;b id="67"&gt;67   &lt;/b&gt;            HttpContext.Current = &lt;span class="kwd"&gt;new&lt;/span&gt; HttpContext(&lt;span class="kwd"&gt;new&lt;/span&gt; SimpleWorkerRequest(virtualPath, queryString, writer));
&lt;b id="68"&gt;68   &lt;/b&gt;            Page page = (Page)type.GetConstructor(&lt;span class="kwd"&gt;new&lt;/span&gt; Type[] { }).Invoke(&lt;span class="kwd"&gt;new object&lt;/span&gt;[] { });
&lt;b id="69"&gt;69   &lt;/b&gt;
&lt;b id="70"&gt;70   &lt;/b&gt;            &lt;span class="kwd"&gt;return&lt;/span&gt; page;
&lt;b id="71"&gt;71   &lt;/b&gt;        }
&lt;b id="72"&gt;72   &lt;/b&gt;    }
&lt;b id="73"&gt;73   &lt;/b&gt;}
&lt;b id="74"&gt;74   &lt;/b&gt;
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;There&amp;#39;s just one remaining problem: A few moments after the test runs and passes, something throws System.AppDomainUnloadedException exception, with no stack trace available:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://img227.imageshack.us/img227/499/appdomainunloadedexceptsr5.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;I can&amp;#39;t find a way to avoid it. Probably I have to clean the AppDomain somehow, but how?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item></channel></rss>