Literally every second time I run a big ugly web site project, I get an UnauthorizedAccessException, with a message pointing to a DLL, e.g. Temporary ASP.NET Files\ctheweb\0d76d363\4695c81f\App_Web_vi6bbbpy.dll' is denied.
I then stop and restart the project, and it runs fine. I do some testing, debugging, fixing, run it again, and get the error again.
I'm inclined to add a pre-build command to just clear that directory, but I always prefer to solve a problem with something other than a hammer, at least initially.
This happens a lot during development when you are constantly modifying the aspx pages, ASP.NET is trying to compile and VS is trying to compile and ASP.NET is trying to execute the files. Also, sometime the lock goes away when you reset IIS.
iisreset /stop
del "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\*.*" /Q /F /S
del “C:\windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\*.*” /Q /F /S
iisreset /start
If this happens on production then add this to you web.config.
<compilation tempDirectory = “C:\windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\Other\” />
(scroll to the side, the key is to pick some \Other\ folder than the default.
And finally, use a Deployment project to try to pre-compile everything in advance. No compilation means no attempt to replace things in the temp folder
Or you can try OS diagnostics and try to find out what process has a lock on that file and kill that process. Not worth the effort when easier solutions exist.
In my case the answer was trivial - it was never an error to begin with. For me, ASP.NET actually throws this internally every time it starts my web application, and I simply forgot the "break on all exception" setting on by accident (from another debugging session). I checked it off and everything worked fine.
For reference, here's the stacktrace:
> mscorlib.dll!System.IO.__Error.WinIOError(int errorCode, string maybeFullPath) Unknown
mscorlib.dll!System.IO.FileStream.Init(string path, System.IO.FileMode mode, System.IO.FileAccess access, int rights, bool useRights, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES secAttrs, string msgPath, bool bFromProxy, bool useLongPath, bool checkHost) Unknown
mscorlib.dll!System.IO.FileStream.FileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options, string msgPath, bool bFromProxy) Unknown
mscorlib.dll!System.IO.FileStream.FileStream(string path, System.IO.FileMode mode) Unknown
System.Web.dll!System.Web.UI.Util.HasWriteAccessToDirectory(string dir) Unknown
System.Web.dll!System.Web.HttpRuntime.SetUpCodegenDirectory(System.Web.Configuration.CompilationSection compilationSection) Unknown
System.Web.dll!System.Web.HttpRuntime.HostingInit(System.Web.Hosting.HostingEnvironmentFlags hostingFlags, System.Security.Policy.PolicyLevel policyLevel, System.Exception appDomainCreationException) Unknown
System.Web.dll!System.Web.HttpRuntime.InitializeHostingFeatures(System.Web.Hosting.HostingEnvironmentFlags hostingFlags, System.Security.Policy.PolicyLevel policyLevel, System.Exception appDomainCreationException) Unknown
System.Web.dll!System.Web.Hosting.HostingEnvironment.Initialize(System.Web.Hosting.ApplicationManager appManager, System.Web.Hosting.IApplicationHost appHost, System.Web.Configuration.IConfigMapPathFactory configMapPathFactory, System.Web.Hosting.HostingEnvironmentParameters hostingParameters, System.Security.Policy.PolicyLevel policyLevel, System.Exception appDomainCreationException) Unknown
System.Web.dll!System.Web.Hosting.HostingEnvironment.Initialize(System.Web.Hosting.ApplicationManager appManager, System.Web.Hosting.IApplicationHost appHost, System.Web.Configuration.IConfigMapPathFactory configMapPathFactory, System.Web.Hosting.HostingEnvironmentParameters hostingParameters, System.Security.Policy.PolicyLevel policyLevel) Unknown
[AppDomain (DefaultDomain, #1) -> AppDomain (/LM/W3SVC/4/ROOT-1-130624548490751465, #2)]
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) Unknown
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironmentAndReportErrors(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) Unknown
System.Web.dll!System.Web.Hosting.ApplicationManager.GetAppDomainWithHostingEnvironment(string appId, System.Web.Hosting.IApplicationHost appHost, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) Unknown
System.Web.dll!System.Web.Hosting.ApplicationManager.CreateObjectInternal(string appId, System.Type type, System.Web.Hosting.IApplicationHost appHost, bool failIfExists, System.Web.Hosting.HostingEnvironmentParameters hostingParameters) Unknown
System.Web.dll!System.Web.Hosting.ProcessHost.StartApplication(string appId, string appPath, out object runtimeInterface) Unknown
IF it is a lock caused by some external process like a virus scanner or a search indexer, then you might try using Windows permissions to lock down the rights of other users and processes to read the files. By default, the Temporary ASP.NET Files
directory is available to Users, Administrators, IIS_USR, SYSTEM, and TrustedInstaller -- which is to say, just about everyone.
Try out MatthewMartin's advice of the different compilation folder;
<compilation tempDirectory = “C:\LimitedPermissionCompilationDir\” />
And then limit the LimitedPermissionCompilationDir
folder to just the users and groups who need permission -- say, IIS_USR if you're running on IIS, or your own account if you are compiling for the file-based webserver.
Anyway, this is a relatively safe way to try things out, as you don't need to worry about affecting anything other than the site you're running.
I've had similar problems in the past due to the workstation's anti-virus program accessing the file at the "wrong" time. Another tool you can use to determine what has a file open: Process Explorer (recommended for your personal arsenal even if it doesn't prove useful here, frankly).
I found this when I was using ipersonation for another user in my web.config and the user didn't have rights. doh!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With