views:

233

answers:

2

Hey all,

I have written a VirtualPathProvider to change how aspx pages are loaded into my ASP.Net application. As part of this process I have removed Code-Behind files and I am simply ascerting that my page inherits from a particular page class. eg:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Namespace.PageClass" %>

If I do this I get the following exception:

HttpParseException Could not load type 'Namespace.PageClass'

I have also tried the following:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Namespace.PageClass, AssemblyName" %>

This produces the following error:

HttpParseException Could not load the assembly 'AssemblyName'. Make sure that it is compiled before accessing the page.

When the application starts I load the required Assembly into the current App domain:

AppDomain.Current.Load(...)

At the moment I am assuming the problem lies with the BuildManager's ability to resolve the Namespace/Assembly reference.... but if honest... that's a guess :-)

Is anyone able to shed some light on this?

A: 

Possible causes:

  1. If you refer to any code behind module in .aspx pages or Global.asax page and the web application has'nt been built then you get this error. Just build the application again and make sure that the type Namespace.PageClass is available in one of the web application assemblies.

    Use @Assembly directive to link the assembly to the aspx page at compile time.

    @ Assembly Name="assemblyname" Src="pathname" makes all the assembly's classes and interfaces available for use.

2.Another reason for such an error could be a wrong version of ASP.NET configured in IIS. To select the correct version of ASP.NET in IIS

  1. Go to Start Menu, click on Run (alternatively use Win Key + R )

  2. Type INetMgr and press enter to open Internet Information Services Application

  3. Expand the tree node displaying local computer name and navigate to Web Sites-->Default Web Site

  4. Right click on Default Web Site node and select the popup menu option Properties

    5.Navigate to ASP.NET tab in the properties page and set the correct/latest version.

Pradeepneo
Pradeepneo - thanks but unfortunately that didn't do it.These assemblies are loaded dynamically at run time and so I can't using the @Assembly directive to point them to the correct place.Equally isn't the @Assembly directive used to resolve child control references - not page inheritance?I currently load the Assembly dynamically at Application_Start using: AppDomain.Current.Load(...).I assumed that was all I would needed to do.
Gavin Osborn
+1  A: 

Hi there :)

Your page must use the full specified class location - i.e. Inherits="MyNamespace.MyClass, MyAssembly".

Then loading the assembly into the appDomain is not going to help AppDomain resolve it. It doesn't walk the dynamically loaded assemblies. So you must subscribe for the AppDomain.ResolveAssembly event.

private Assembly myDynamicAssembly = null;

protected void Application_Start( object sender, EventArgs e )
{
      myDynamicAssembly = Assembly.LoadFrom( Server.MapPath( "MyLocation/MyAssembly.dll" ) );

      AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler( CurrentDomain_AssemblyResolve );
}

Assembly CurrentDomain_AssemblyResolve( object sender, ResolveEventArgs args )
{
      if ( args.Name == "MyAssembly" )
      {
           return myDynamicAssembly;
      }

      return null;
}

And you're done. Now the runtime knows how to resolve classes from this dynamically loaded assembly.

Ivan Zlatanov
Ivan, Thanks - that moved me a step forward - but only a small step! I am now getting a compilation error: CS0400: The type or namespace name 'Namespace' could not be found in the global namespace (are you missing an assembly reference?)public class default_aspx : global::Namespace.PageClassAny thoughts?
Gavin Osborn
Can you post the Source of the error, please. I am not able to duplicate this.
Ivan Zlatanov
Hi Ivan, I have created another question (with full source code example) to take this further - thanks for you help so far. The new questions is here: http://stackoverflow.com/questions/1206634/dynamically-loading-page-class-assembly-type-or-name-could-not-be-found-in-the-g
Gavin Osborn