locked
Embedded Resource Functioning in IIS 5.1 and Failing in IIS 7 RRS feed

  • Question

  • User875171746 posted

    I have been struggling with this issue for a whole day now. It started when I tried to deploy my ASP.NET 2.0 web site from IIS 5.1 (Windows XP Pro SP3) to IIS 7 (Windows Server 2008 R2 x64). I have a custom control library that has 1 embedded JScript file and 1 embedded CSS file. They serve up correctly on IIS 5.1, but on IIS 7 I get a 404 not found error when navigating to the WebResource.axd URL that is output to the page:

    404 Requested Page Not Found: System.Web.HttpException: This is an invalid webresource request.
       at System.Web.Handlers.AssemblyResourceLoader.System.Web.IHttpHandler.ProcessRequest(HttpContext context)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

    I have tried many things during troubleshooting, but I am getting conflicting indications as to where the problem might lie:

    1. There is another web site that uses the same control library (older version) on the target server that is working.
    2. I moved aside the files in that other site and put this site in its place, but the problem persisted even when hosted in a different physical web site.
    3. I tried copying the old version of the DLL into the new web site - it works (although my latest updates are not included in that version of the DLL).
    4. I tried recompiling the source code at the same point in time (from source control) I compiled the working DLL, and the new DLL fails.

    I am thinking now the only thing it could be is a difference in the way the compiler functions because there was a huge .NET windows update installed on my dev machine since the working DLL was compiled, however if someone else spots something in my code that I missed, please let me know.

    A common module in the control library has this code to inject the CSS file:

        Friend Const ScriptPath As String = "Namespace.Script.min.js"
        Friend Const StylePath As String = "Namespace.Styles.min.css"
        Friend Const StyleName As String = "ml_styles"
    
        Public Sub IncludeStyles(ByVal Page As System.Web.UI.Page)
            If Not Page.ClientScript.IsClientScriptBlockRegistered(StyleName) Then
                Dim cssUrl As String = Page.ClientScript.GetWebResourceUrl(GetType(Common), StylePath)
    
                Dim css As New System.Web.UI.HtmlControls.HtmlGenericControl("link")
                With css
                    .Attributes.Add("href", cssUrl)
                    .Attributes.Add("rel", "stylesheet")
                    .Attributes.Add("type", "text/css")
                End With
    
                Page.Header.Controls.Add(css)
                Page.ClientScript.RegisterClientScriptBlock(GetType(Page), StyleName, "")
            End If
        End Sub
    

    NOTE: The RegisterClientScriptBlock code is only there to ensure my CSS reference is only added to the page once - that is why it is an empty string.

    Then each control in the library has this code to load the JScript and CSS files:

            Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)

                With Page.ClientScript
                    .RegisterClientScriptResource(GetType(Common), ScriptPath)
                    IncludeStyles(Page)
                End With

                MyBase.OnPreRender(e)

            End Sub

    The Common type is a the name of the module that contains the first code block. All of my controls share the same JScript and CSS files, so I am using this method to ensure they don't get added more than once per page.

    Finally, here is the relevant information from web.config, although I have pretty much ruled it out already because the old version of the DLL works:

    	<system.webServer>
    		<modules runAllManagedModulesForAllRequests="true">
    			<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" preCondition="managedHandler" />
    		</modules>
    		<handlers>
    			<add name="*.aspx_*" path="*.aspx" verb="*" type="URLRewriter.RewriterFactoryHandler, URLRewriter" preCondition="integratedMode,runtimeVersionv2.0" />
    		</handlers>
    		<validation validateIntegratedModeConfiguration="false" />
    		<httpErrors>
    			<remove statusCode="404" subStatusCode="-1" />
    			<error statusCode="404" prefixLanguageFilePath="" path="/PageNotFound.aspx" responseMode="ExecuteURL" />
    		</httpErrors>
    	</system.webServer>

    I tried disabling the URL Rewriting but it didn't have any effect on the problem.

    For the time being, I will probably just remove the code that injects the resources and add them manually to the web site so I can release it, but I would like to try to fix this problem eventually - it will be problematic to maintain to try to determine when to inject these files and when not to.

    Sunday, August 7, 2011 8:49 AM

All replies

  • User3866881 posted

    Hello NightOw1888:)

    Maybe this is related to IIS7's something configuration (if your proj working well on IIS5) . You can ask here:http://forums.iis.net/default.aspx?GroupID=41

     

    Tuesday, August 9, 2011 10:12 PM
  • User-16411453 posted

    You have to load embedded stuff in OnInit()

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            MyBase.OnInit(e)

            Controls.Clear()

            Dim cs As ClientScriptManager = Me.Page.ClientScript
            Dim rsType As Type = Me.GetType()

    'Register the javascript
            If (Not cs.IsClientScriptIncludeRegistered(rsType, "jquery.js")) Then
                cs.RegisterClientScriptInclude("jquery.js", cs.GetWebResourceUrl(rsType, "PD_Standard.jquery.js"))
            End If

     Select Case [Localization]
                Case 0
                    img_Add2Cart = cs.GetWebResourceUrl(rsType, "PD_Standard.img_Add2Cart_EN_US.png")
                    img_Add2Wishlist = cs.GetWebResourceUrl(rsType, "PD_Standard.img_Add2Wishlist_EN_US.png")
                    img_Add2Cart_Disabled = cs.GetWebResourceUrl(rsType, "PD_Standard.img_Add2Cart_Disabled_EN_US.png")
                    img_Add2Wishlist_Disabled = cs.GetWebResourceUrl(rsType, "PD_Standard.img_Add2Wishlist_Disabled_EN_US.png")
                    img_Modal_Add2Cart = cs.GetWebResourceUrl(rsType, "PD_Standard.hl_Add2Cart_Red_EN_US.png")
                    img_Modal_Add2Wishlist = cs.GetWebResourceUrl(rsType, "PD_Standard.hl_Add2Wishlist_Orange_EN_US.png")


    Saturday, August 20, 2011 1:45 AM
  • User875171746 posted

    Thanks for the suggestion.

    I gave it a try, but I have exactly the same issue I had before - I get a 404 not found error when trying to access the URLs generated by these controls.

    I kinda figured as much. Since I compiled the working version, I didn't change any of the code that injects the resources (I only changed the javascript in one of the resource files). I still believe this is some kind of compilation issue based on my experiments. Nothing else explains why the old version works on the 2008 server, but the new version will not (even though it works fine in DEV).

    Monday, August 22, 2011 4:19 PM
  • User-16411453 posted

    I would remark out the following and give it a spin, just to isolate the problem, and confirm that the following code really is failing.

                Dim cssUrl As String = Page.ClientScript.GetWebResourceUrl(GetType(Common), StylePath)

               
    Dim css As New System.Web.UI.HtmlControls.HtmlGenericControl("link")
               
    With css
                   
    .Attributes.Add("href", cssUrl)
                   
    .Attributes.Add("rel", "stylesheet")
                   
    .Attributes.Add("type", "text/css")
               
    End With

               
    Page.Header.Controls.Add(css)
               
    Page.ClientScript.RegisterClientScriptBlock(GetType(Page), StyleName, "")

    You do have to attach to the client script manager to access the webresource.axd

     Dim cs As ClientScriptManager = Me.Page.ClientScript
            Dim rsType As Type = Me.GetType()

    cs.GetWebResourceUrl(rsType, "PD_Standard.img_Add2Cart_EN_US.png")
    Bold is the root namespace
    Monday, August 22, 2011 6:30 PM
  • User-16411453 posted

    I've never been able to successfully embed a css file, but haven't tried in several years. I can link to an external file

     Dim CssLink As HtmlControls.HtmlLink
                CssLink = New HtmlControls.HtmlLink
                With CssLink
                    .ID = "HKB_Style"
                    .Attributes.Add("href", "~/redCopper/HKB/Css/HKB.css")
                    .Attributes.Add("type", "text/css")
                    .Attributes.Add("rel", "stylesheet")
                End With
                Me.Page.Header.Controls.Add(CssLink)

    Monday, August 22, 2011 6:37 PM
  • User875171746 posted

    Herein lies the problem: this is exactly what I did so I could get this released. There is no issue with the URLs, they are being generated and output - it is just that the resources are not being served (via WebResource.axd), at least not to the same URL that is being output. The thing is - I am using a constant to ensure that there is no way they can be different.

    I tried physically moving the files to the root directory (they are in subfolders named CSS and JavaScript). I realize that the subfolders need to be included in the name but only when using C# - in VB these are left off. I also tried renaming the files (removing the extra periods) because they are named Styles.min.css and Script.min.js, but that didn't help either.

    I also used the following method (and downloadable page) to verify whether the URL was being encrypted correctly - and it is exactly the same between DEV and TEST. http://blogs.telerik.com/blogs/posts/07-03-26/debugging_asp_net_2_0_web_resources_decrypting_the_url_and_getting_the_resource_name.aspx

    What I don't understand is why they would serve up on Windows XP Pro, but not on Windows 2008 Server R2 (x64). I also can't seem to pin down why this DLL that has been working fine in production for several months all of a sudden won't serve the resources. Note that there is a javascript file as well as a CSS file and neither works even though all of the other resources (via 3rd party and MS DLLs) on the page work. The old DLL works fine in all environments, but if I compile the exact same code that was used to make the working DLL I get a new DLL that fails, but only in the IIS7/64 bit environment.

    I ended up building a virtual machine with the same verion of Windows 2008 to try to track down some other bugs. I will try using it to compile the old code to see if I can make a DLL that will serve the resources in that environment. That should be able to give some more information on whether the windows updates really did hose my DEV environment somehow.

    Tuesday, August 23, 2011 5:48 AM
  • User-16411453 posted

    I use to think the embedded files had to be in the root of the server control, but found it was not true.

    You can make a series of folders, web/css, web/controls, web/class and it works just fine.

    You may want to consider harding coding the css

    .Style.Add(HtmlTextWriterStyle.Font-size, "0.8em")

    I can embed images, jquery, javascript, I've never been able to embed css, haven't tried in years.

    Not saying it can't be done, I just needed to get my project done, and get the paycheck.

    With Server 2008, I've had to reboot the server to clear the cache on the disk controller, to get stuff to be consistent. Plus if you set IIS 735 to cache files, the server reboot refreshes the cache files sent to the customer.

    Food for thought!

    Saturday, August 27, 2011 1:02 AM
  • User-1057499591 posted

    Hi

    Have the same problem. A resource that works fine in vs2005 and iis6 then changed to vs2008 and iis 7 and recompiled the code. No errors but it wont work. Everything looks ok but I cant get the css to work. I can se the link to the css in the code and it looks the same as in vs 2005.
    Also have js files css files in folders in the resource dll.

    This is on my dev machine. If I run the aspx page on IIS 7 it wont work but if i use the file system instead then it works.
    I think this is an issue with IIS 7 but i cant figure it out.

    Have you solved this problem ?

     

    Tuesday, November 15, 2011 2:40 AM
  • User875171746 posted

    Tuborg,

    I am sorry to report that this specific problem is still unresolved and I am currently using a separate copy of the JS and CSS files directly in my web project rather than having them served by the DLL.

    However, recently I discovered a solution to another unrelated issue which may be a clue. In my case IIS was upgraded as well as upgrading from Visual Studio 2005 to Visual Studio 2010 and I also changed source control from SourceSafe 2005 to Subversion, so I had too many variables to point to a specific change. Although the problem only presents itself in IIS7, I still suspect it is a compile issue. The project and all DLLs are still being compiled in .NET 2.0. Furthermore, I am using the "Publish" function of VS.NET 2010 to build the project.

    The unrelated issue: Basically, I have a file in the App_Browsers specifying a control adapter to use. It works fine in DEV but when published it quits working. The problem only happens when using VS2010, a version of .NET prior to 4.0, and using the publish command. Details here: http://connect.microsoft.com/VisualStudio/feedback/details/557301/precompiled-app-browsers-dll-not-working-from-visual-studio-2010. I found another post somewhere that suggested deleting the __browserCapabilitiesCompiler.compiled file from the bin directory and that fixed the problem for me, although this is one more step I have to do every time I deploy.

    Like I said, the other issue is unrelated. However, it is proof that compiling .NET 2.0 in VS2010 is not the same as compiling .NET 2.0 in VS2005. Although it is just an untested theory, I suspect that changing the version of .NET (the DLL, the web project, or perhaps both) to match the version of Visual Studio will also fix the issue described in this post. For example, if you are using VS2008, try changing the project version to 3.5 and if you are using VS2010, try changing it to .NET 4.0.

    Keep in mind, my old DLL that was working (even in IIS7) was compiled with VS2005 and the new one (and recompile of the old code) was done under VS2010.

    Please report back your findings. I would still like to find a solution to this issue and I am sure there are others who will stumble on the issue as well.

     

     

    Tuesday, November 15, 2011 7:06 AM
  • User-16411453 posted

    IIS7 and IIS7.5 is not as forgiving as IIS 5.5 or IIS6.

    Sometimes you have to load vs2010 on a server 2008 test server, and run the code in debug to finalize your code modules.  All kinds of things will come to light.

    Saturday, November 26, 2011 1:54 AM