App_Code folder created automatically in published website
Asked Answered
S

3

7

I have a MVC4 App created in VS 2010 with Umbraco 6 too and I've created a web deploy project which is used by my Team City CI server to deploy my website to a CI environment for testing.

On the CI server the first time I load the homepage (or any page) it loads perfectly fine. However the act of loading a page creates a App_Code folder on my CI server, and after that I get the message "The directory '/App_Code/' is not allowed because the application is precompiled". Removing the App_Code folder means that it once again works for one page load and the folder is created again.

Removing the PrecompiledApp.config file causes my site to not load with a YSOD stating "Object reference not set to an instance of an object." at the following point in the stack trace "Umbraco.Web.UmbracoModule.BeginRequest(HttpContextBase httpContext) +25"

To be clear, I don't have an App_Code folder in my project, and I don't want or need one. All I want is for it to stop creating one automatically upon page load! I've used Umbraco in VS and deployed in the same way many times before, just not with Umbraco 6 and in an MVC project.

Any ideas why App_Code is being automatically created and what I can do to stop it?

Many Thanks

Richard

Spithead answered 28/6, 2013 at 16:20 Comment(0)
B
1

I seem to use Umbraco in a similar way as you do, wrapping it as a MVC 4 project. Hence it becomes a "VS Web Application" instead of a "VS Web Site".

What is important to remember is that Umbraco originally wasn't made to be run as an application and a lot of the functionality in Umbraco is first and foremost directed to using App_Code.

The internal classes AssemblyExtensions, PluginManager, TypeHelper and the public class TypeFinder in Umbraco.Core have methods that are dependent on that the App_Code folder is there. Even if you don't need an App_Code in your solution Umbraco does, if you don't want to see it simply hide it from your solution. If you really don't want it remove all references to it in the source and create your own compilation of Umbraco.

== EDIT BELOW ==

After reading your comment and post again I created a small solution for your problem. The fact that Umbraco creates the App_Code is still because of the framework initialization that won't work without App_Code existing. But recompiling and creating an own dist of Umbraco will as OP points out create some extra maintenance when upgrading and so on.

It's not the ideal but most clean way to handle this matter to allow Umbraco to create the App_Code folder but also remove the same when the application have initialized. I'd use an IApplicationEventHandler. The sample code works on my box.

using Umbraco.Core;
using Umbraco.Core.IO;

namespace YourNamespace.EventHandlers
{
    public class AppCodeEvents : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        { }

        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        { }

        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            if (System.IO.Directory.Exists(IOHelper.MapPath(IOHelper.ResolveUrl("~/App_Code"))))
            {
                System.IO.Directory.Delete(IOHelper.MapPath(IOHelper.ResolveUrl("~/App_Code")));
            }
        }
    }
}
Bascinet answered 1/7, 2013 at 16:23 Comment(4)
Thanks Eric. I've used Umbraco in a VS web app a number of times before and never had this issue. That was in older versions (4.0.x for example) so is this an issue that would only occur in newer versions? I did try excluding the empty App_Code folder from my solution but it still recreates it when the website runs after building with Team City, and throws the errors I describe originally. You say that you run things similar to I do - did you go in and start removing references within the Umbraco code? Presumably that then becomes a burden when you want to upgrade.Spithead
I agree that the code above solves the problem, but it also introduces another problem: It causes the application to re-initialize on every single request, which is a huge performance issue. For the time being, I'm going to move forward under the assumption that Umbraco sites can't be precompiled.Glossator
@KenSykora Maybe I'm missing something fundamental on IIS and .NET but everything in App_Code needs to be compiled during application start, thus this isn't an umbraco thing, it's .net.Bascinet
@EricHerlitz Yep, which is why when App_Code is deleted, Umbraco then recreates it, which triggers application start, which then this code would delete it, and the cycle continues. To see the effects in action, If you try to go to a page in Umbraco without this module and continually hit refresh, you'll notice it loads quickly each time, but with this module, it will load very slowly each time (application keeps restarting)Glossator
P
0

If you are precompiling your site using a web deploy project, I assume you have all your references separated out from the project - which is a good thing. So, I think the simple answer here is don't precompile the site, just allow the web application to be built so that it pulls in the references and deploy the built project.

Personally, I find the best way to work with Umbraco v6 is via NuGet. Create an empty MVC4 project and use NuGet to add Umbraco v6. This will automatically sort out all the references for you. This is because those fantastic guys at Umbraco created two Nuget packages, one with the project files and the other with the core DLLs.

This means that when the site is built, the references are pulled in and updating the site is easy. It is just a matter of updating via NuGet.

Presumptive answered 3/7, 2013 at 9:37 Comment(0)
K
0

This was happening me as well, turned out it was because a 'precompiledApp.config' file had somehow made its way onto the production server... not sure how that happened but once I deleted and recycled web app this stopped happening.

Kape answered 21/10, 2014 at 22:28 Comment(6)
I stopped getting the App_Code dance by deleting the precompiledApp.config file. Now I'm having all sorts of fun with compilation errors: CS0433: The type 'ASP.global_asax' exists in both 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\17b6aa3f\55a36928\assembly\dl3\1f39276e\0041f4bb_8a98d001\App_global.asax.DLL' and 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\17b6aa3f\55a36928\App_global.asax.dtv9abzr.dll'Obliquely
@OwenBlacker did you ever find a solution to that problem? I'm have the same case :)Uriiah
@Kape how did you make it work without having the precompiledApp.config present? I get the same type of errors as Owen doesUriiah
@Uriiah Erm, I really can't remember, but I would check the bin folder for any DLL files starting with "App_global.asax", compiled or otherwise, and delete 'em. They should be generated at runtime anyway.Kape
Oh gods, I'm so sorry @Squazz, but I completely don't remember. I've a horrible feeling we just let Umbraco create the "unnecessary" App_Code folder and stopped worrying about it somehow. Sorry; I wish I'd documented it here now :(Obliquely
@OwenBlacker that's ok. We ended up "concluding" that it wasn't possible to precompile Umbraco sitesUriiah

© 2022 - 2024 — McMap. All rights reserved.