0

I'm attempting to precompile a few master pages (not update-able) to share them across multiple applications. The project I'm precompiling is a Web Site. The project that references precompiled assemblies is a Web Application. However, I'm getting a Could not load type 'ASP.xxx_master' every time i try to reference the master page from the client.

<%@ Master Language="C#" Inherits="ASP.sitebase_master" %>

My precompiled master page looks like this.

<%@ Master Language="C#" ClientIDMode="Static" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org  /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="AspNetHead" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
    <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=7" /><![endif]-->
    <asp:ContentPlaceHolder ID="MetaContent" runat="server" />
    <title>Web Portal</title>   
    <link href="/media/css/style.css" rel="stylesheet" type="text/css" />
    <link href="/media/js/plugins/colorbox/colorbox.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="StyleContent" runat="server" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript" language="javascript"></script>
    <script src="/media/js/plugins/colorbox/jquery.colorbox-min.js" type="text/javascript" language="javascript"></script>
    <script src="/media/js/plugins/filestyle/jquery.filestyle.min.js" type="text/javascript" language="javascript"></script>
    <script src="/media/js/portal.master.js" type="text/javascript" language="javascript"></script>
    <script language="javascript" type="text/javascript">
        PORTAL.debug.init();
        PORTAL.init();
    </script>
    <asp:ContentPlaceHolder ID="ScriptContent" runat="server" />
</head>
<body>
    <div id="hld">  
        <div class="wrapper">
            <form id="AspNetForm" runat="server">
                <asp:ContentPlaceHolder ID="BodyContent" runat="server" />  
            </form>
            <asp:ContentPlaceHolder ID="FooterContent" runat="server" />
        </div>
    </div>
</body>

I'm stumped. No idea why the type isn't resolved. Anybody got suggestions? Both projects (precompiled web site and client web application) are built for ASP.NET 4.0.

EDIT: Here is the list of dependencies of the precompiled assembly. No 3rd party references.

mscorlib, System, System.Web

UDPATE 1

Well, the quick fix to this issue is to specify the full path to the master page.

<%@ Master Language="C#" Inherits="ASP.sitebase_master, App_Web_sitebase.master.cdcab7d2" %>

After doing that, I'm receiving the following error:

An error occurred while try to load the string resources (FindResource failed with error -2147023083).

After doing some research, this appears to be related to the way HTML markup is parsed within the master page. Not entirely sure yet. I haven't dug much deeper into it. Overall, I can't believe this is the recommended way to share controls as it is absolutely, mindbogglingly idiotic.

UPDATE 2

I couldn't make anything of value out of this. It appears to be hating "script" tags in the head section, but I don't know why. The Master Page works great with a single script include. As soon as i start adding more I keep getting that error. After wasting a full day on this I ended up submitting a bug report to Microsoft. If anyone wants to bump it, please do.

UPDATE 3

I spent a few more days debugging this after no response from MS. Here is my findings. I initially thought that the code generated by CodeDOM provider is looking for a .NET resource that somehow did not get embedded in the assembly when it was published. I was wrong. After some investigation it appears that what's happening is after the Master Page reaches a certain size, a chunk of it is stored in the Resource Table in PE Data Directories section of the assembly. In fact, after looking at the generated assembly in PE resource viewer, i was able to confirm this by finding all my script includes in the Resource Table. Now, here is the actual problem. What's happening is that the CodeDOM provider generates a call to Win32 FindResource to pull that resource from the Resource Table. However, FindResource doesn't work on assemblies in memory, only on disk. So it fails with the above exception. I'm getting close, but still no workaround.

Sergey Akopov
  • 1,130
  • 1
  • 11
  • 25
  • Are you referencing the assembly file created for the master page in the new project? – NotMe Apr 27 '11 at 16:14
  • Yep, the client web application is referencing my precompiled web site assembly. I threw it in reflector and saw the type sitting under ASP namespace. I can instantiate the type in the code-behind for any page of the client app without issues. But not when i try to inherit it in the master page. – Sergey Akopov Apr 27 '11 at 16:16
  • Ahhh, so you're using a website instead of a web application? Really wish MS would just remove website to avoid these sort of problems. – Phill May 03 '11 at 05:13
  • @Phill, the precomp process is the same for both. I'd still run into the same issue trying to reference those precompiled MP types. It's just easier for me to precompile from a website than a web app. And all of the guides i worked with suggested a website, even msdn. If there is a better solution to this, I'm all ears. To me this looks like a fundamental design issue because UserControls and MasterPages were never meant to be compiled and reused. – Sergey Akopov May 03 '11 at 05:31
  • @Sergey - A WebSite and Web Application are not the same, and the "pre compilation" is not the same for both. The closest thing to getting a Web Application out of a WebSite is to use the Web Deployment Projects and setting it up to produce a single assembly. http://www.microsoft.com/downloads/en/details.aspx?FamilyId=0AA30AE8-C73B-4BDD-BB1B-FE697256C459 – Phill May 03 '11 at 05:58
  • @Phill, I converted to web application and using a Web Deployment Project to precompile. Thanks for bring this up :) It is much cleaner, but the I still get this when i reference the precompiled master page. – Sergey Akopov May 03 '11 at 05:59
  • Also the way I setup large applications in Visual Studio is to sepearate the projects into seperate web applications and use post-build events to copy pages/user controls into the primary application, so the process of re-using UserControls and MasterPages is perfectly acceptable, but it's an epic fail in Web Sites because of the entire way they are developed, being directory based instead of project based. – Phill May 03 '11 at 06:00
  • @Phill, No i completely agree with you on the web app part. I AM using web apps everywhere except for the project that i'm precompiling, which i now converted to the web app as well. The precompilation produces EXACTLY the same results as it did with a web site when i compare both in Reflector. I'm still seeing accessor.AddParsedSubObject(base.CreateResourceBasedLiteralControl(0x3ab, 660, true)); in the generated code, which is what causes the error above. Yes, i do not have the option to generate single assembly precompiling from a web site. I did that with ILMERGE. – Sergey Akopov May 03 '11 at 06:02
  • Well for VB that's not true, specifically when using 'non-updatable', which compiles .aspx pages that point to specific compiled assembly(ies). There is a reproducible bug in the precompilation where a Web Application works with a page inheriting the same code behind, but the Website doesn't... – Phill May 03 '11 at 06:05
  • Sorry if I sound negative, it's just every time these sort of issues pop up with Web Site's, I've pretty much given up trying to resolve them because they are issues I don't get with Web App's – Phill May 03 '11 at 06:06
  • @Phill, I didn't sense any negativity. Thanks for helping, man. I don't do anything in web sites either. All of the guides i read placed precompiled controls/master pages into a web site, so I just followed along. I'm still referencing them from a web app. The bottom line is i'm getting this exception after precompiling into a single assembly now using the Web App Deployment project. It doesn't make any difference. Something in the master page markup makes the compiler put the markup in the resource section instead of LiteralControl. And I don't understand what triggers this. – Sergey Akopov May 03 '11 at 06:14
  • Is it possible to trim everything down to two project (the website and web app) that can cause this problem, so I could take a look at it? – Phill May 03 '11 at 06:17
  • Absolutely. I opened a ticket with MS (https://connect.microsoft.com/VisualStudio/feedback/details/665280/an-error-occurred-while-try-to-load-the-string-resources-findresource-failed-with-error-2147023083#details) There is a sample project I attached to the ticket which reproduces this error. – Sergey Akopov May 03 '11 at 06:19
  • There's nothing on there I can just download to run? – Phill May 03 '11 at 06:23
  • You should find a link if your expand ticket details. You may have to be logged in though. I also uploaded it to my blog. Just hit this link: http://www.sergeyakopov.com/wp-content/uploads/2011/04/PrecompBug.zip You'll get 1 web app (client), 1 precompiled site and a folder to precompile to. – Sergey Akopov May 03 '11 at 06:26
  • Ok, downloaded it, I'm still at work, don't have VS2010 here so I'll take a look in an hour or so when I get home. – Phill May 03 '11 at 06:36

1 Answers1

2

I finally have a workaround. It's not pretty, but it resolves the issue. Apparently using LoadControl to pre-load precompiled MasterPages loads all of the resources that FindResource cannot find otherwise. So, here is all i did to make this work.

In my client application I created a dummy master page (i.e. Dummy.Master) which references my precompiled master page like so:

<%@ Master Language="C#" Inherits="ASP.sitebase_master, App_Web_sitebase.master.cdcab7d2" %>

Now, any .aspx page which references Dummy.Master needs to preload the precompiled MasterPage type like so:

        protected override void OnPreInit(EventArgs e)
        {
            ASP.sitebase_master mp = (ASP.sitebase_master)Page.LoadControl(typeof(ASP.sitebase_master), null);

            base.OnPreInit(e);
        }

I don't know why this works, but it does. This code must run before the MasterPage is resolved, so PreInit worked great. After glancing at the .NET code for a few seconds in Reflector, it appears that LoadControl actually does some assembly compiling voodoo when it attempts to load a certain control type. So perhaps something in there loads that PE resource data section. The best place to put it would be in the base class I suppose which all pages could inherit from. Also, each loaded control (master page in this case) ought to be cached. Here is a good article explaining just that.

Hopefully this helps someone as much as it helped me. It was a pretty big show stopper for me.

Sergey Akopov
  • 1,130
  • 1
  • 11
  • 25