The reason that adding the Reference directives fixes the problem, is
because internally inside of the call to GetCompiledPageInstance - the
ASP.NET runtime is parsing each file seperately and compiling them (i.e. not
calling GetCompiledPageInstance again). GetCompiledPageInstance is only
intended to be called from the HandlerFactory (I am assuming this since it
obviously is ill-prepared to be called more than once).  Once compiled, an
item (ParserCacheItem) is added to the HttpRuntime's Cache.  What happens
when you call GetCompliedPageInstance more than once (although I could not
find exactly where/why in the call stack this happens) is that the second
page name (in this case Test2.aspx) is associated with the Type that was
compiled from Test1.aspx.

Another way to make this work correctly is to hit Test1.aspx and Test2.aspx
with a browser before hitting RunMe.aspx, which associates the correct Type
with the correct page name in the Cache.

I would imagine it works if you remove the classname attribute from the page
directive because it goes down a slightly different code path internally.
In any event, I don't believe GetCompiledPageInstance was intended to be
called in this manner.  Using the Reference directive is the standard way of
including another .aspx file.  When you use it - you don't have to call
GetCompiledPageInstance anymore - you can just call new:

IHttpHandler test1 = new Test1();



-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Jim Arnold
Sent: Thursday, February 27, 2003 10:17 AM
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] Problem with ASP.Net's parser


Could someone sanity check this for me?  Create two pages, Test1.aspx and
Test2.aspx, like so:

-----------------------------------------------------
<%@ Page ClassName="Test1"%>

hello from test1
-----------------------------------------------------
<%@ Page ClassName="Test2"%>

hello from test2
-----------------------------------------------------

And a third called RunMe.aspx:

-----------------------------------------------------
<script runat="server" language="C#">
 void Page_Load(object o, EventArgs e) {
  IHttpHandler test1 =
PageParser.GetCompiledPageInstance(Request.ApplicationPath,
Server.MapPath("Test1.aspx"), Context);
  test1.ProcessRequest(Context);
  IHttpHandler test2 =
PageParser.GetCompiledPageInstance(Request.ApplicationPath,
Server.MapPath("Test2.aspx"), Context);
  test2.ProcessRequest(Context);
 }
</script>
-----------------------------------------------------

Put them all in a browseable ASP.Net folder and hit RunMe.aspx.  You should
see this output:

hello from test1 hello from test1

Tests on both instances confirm that they are of type ASP.Test1, instead of
ASP.Test1 and ASP.Test2 as I would expect.  Now, add the following
directives to RunMe.aspx:

<%@ Reference Page="Test1.aspx" %>
<%@ Reference Page="Test2.aspx" %>

Now touch (edit and save) Test1.aspx and Test2.aspx to recompile them, and
hit RunMe.aspx again.  You should see:

hello from test1 hello from test2

So my questions are: why is PageParser returning instances of the same type
for two different file paths, and why does adding references to the pages
fix this?

Incidentally, if you take out the ClassName directives from Test1.aspx and
Test2.aspx, and remove the Reference directives from RunMe.aspx, PageParser
works as expected.

I am running 1.0.3705 on Windows XP Home, through Cassini.

Thanks for your time,

Jim

Reply via email to