January 7, 2010

Why am I suddenly getting type or namespace errors in my ASP.NET web application

I just spent the last 20 minutes bashing my head against this problem, and I know that at some point it's going to come back and bite me again and I'm not going to remember how I fixed it, so this blog post is really just a reminder to my future self that this is how I fixed it - hopefully it will help others too.

I just added files from our source code repository that were contained within a folder within my APP_CODE directory. No sooner as I added them and I started getting the following error at every line that referenced objects contained in other files that resided directly in my APP_CODE directory.

"The type or namespace name could not be found are you missing a using directive"

I immediately checked that the namespaces were referenced correctly - and they were. I checked that the classes I were referencing were marked as public and that I could access them directly, and indeed, they were and I should.

My colleague @JohnMacIntyre (in a rare moment of brilliance) told me to check the web.config for the compilation settings. Sure enough I found:

<compilation debug="true">
    <codeSubDirectories>
        <add directoryName="helpers"/>
    </codeSubDirectories>
    <assemblies>
        <add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    </assemblies>
</compilation>

I removed the <codeSubDirectories> section and hey presto, suddenly everything works again. The order of compilation is top-down with the APP_CODE directory being compiled last consequently if you have multiple folders defined here, code in earlier folders cannot be 'seen' by code in subsequent folders but not the other way around and none of the code in these folders can see code in your APP_CODE folder, however code in your APP_CODE folder can see all the code in those folders.

3 comments:

  1. Thanks for your concern. Please take it up with the venerable Blogger designers at Google... :)

    ReplyDelete
  2. I'd have done just that if I hadn't already used my my *rare* moment of brilliance ....

    ... jackass

    ;-)

    ReplyDelete