title: Why does my ASP.NET 4.0 web service fail with a compliation error with NetworkService but succeed with LocalSystem
body: <p>I started getting server-side compliation errors indicating that the identity of my web service's application pool could not access files within a temporary ASP.NET folder.  If I change the identity of the application pool from NetworkService to LocalSystem, the problem goes away.  I can't see any issue with the security settings on the folder and I reran aspnet_regiis -i to no effect.</p>
  
  <p>The complete exception as it appears in the application error log is included below, but the gist of it is:</p>
  
  <pre><code>HttpCompileException
  error CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\
  Framework\v4.0.30319\Temporary ASP.NET Files\kswsextensionservicesweb\f2efe503\
  31c03187\App_global.asax.4gcxry4d.dll' -- 'Access is denied. '
  </code></pre>
  
  <p>Can anyone explain why?  What's strange is that it had been working.  Something changed, but I can't figure out what.</p>
  
  <pre><code>Log Name:      Application
  Source:        ASP.NET 4.0.30319.0
  Date:          3/12/2011 12:41:21 PM
  Event ID:      1310
  Task Category: Web Event
  Level:         Warning
  Keywords:      Classic
  User:          N/A
  Computer:      NYDMTRAIN01.kayescholer.com
  Description:
  Event code: 3007 
  Event message: A compilation error has occurred. 
  Event time: 3/12/2011 12:41:21 PM 
  Event time (UTC): 3/12/2011 5:41:21 PM 
  Event ID: bc285ecdc00941d28070725f55ea24f1 
  Event sequence: 3 
  Event occurrence: 1 
  Event detail code: 0 
  
  Application information: 
      Application domain: /LM/W3SVC/1/ROOT/KSWSExtensionServicesWeb-2-129444252812003152 
      Trust level: Full 
      Application Virtual Path: /KSWSExtensionServicesWeb 
      Application Path: C:\inetpub\wwwroot\KSWSExtensionServicesWeb\ 
      Machine name: NYDMTRAIN01 
  
  Process information: 
      Process ID: 3988 
      Process name: w3wp.exe 
      Account name: NT AUTHORITY\NETWORK SERVICE 
  
  Exception information: 
      Exception type: HttpCompileException 
      Exception message: (0): error CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\kswsextensionservicesweb\f2efe503\31c03187\App_global.asax.4gcxry4d.dll' -- 'Access is denied. '
     at System.Web.Compilation.AssemblyBuilder.Compile()
     at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
     at System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp)
     at System.Web.Compilation.BuildManager.CompileGlobalAsax()
     at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
     at System.Web.Compilation.BuildManager.CallAppInitializeMethod()
     at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
  
  
  
  Request information: 
      Request URL: http://nydmtrain01/KSWSExtensionServicesWeb/Services/KSWSDocumentHistoryService.svc 
      Request path: /KSWSExtensionServicesWeb/Services/KSWSDocumentHistoryService.svc 
      User host address: 172.25.43.171 
      User:  
      Is authenticated: False 
      Authentication Type:  
      Thread account name: NT AUTHORITY\NETWORK SERVICE 
  
  Thread information: 
      Thread ID: 6 
      Thread account name: NT AUTHORITY\NETWORK SERVICE 
      Is impersonating: False 
      Stack trace:    at System.Web.Compilation.AssemblyBuilder.Compile()
     at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
     at System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp)
     at System.Web.Compilation.BuildManager.CompileGlobalAsax()
     at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
     at System.Web.Compilation.BuildManager.CallAppInitializeMethod()
     at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
  
  
  Custom event details: 
  
  Event Xml:
  &lt;Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"&gt;
    &lt;System&gt;
      &lt;Provider Name="ASP.NET 4.0.30319.0" /&gt;
      &lt;EventID Qualifiers="32768"&gt;1310&lt;/EventID&gt;
      &lt;Level&gt;3&lt;/Level&gt;
      &lt;Task&gt;3&lt;/Task&gt;
      &lt;Keywords&gt;0x80000000000000&lt;/Keywords&gt;
      &lt;TimeCreated SystemTime="2011-03-12T17:41:21.000Z" /&gt;
      &lt;EventRecordID&gt;127544&lt;/EventRecordID&gt;
      &lt;Channel&gt;Application&lt;/Channel&gt;
      &lt;Computer&gt;NYDMTRAIN01.kayescholer.com&lt;/Computer&gt;
      &lt;Security /&gt;
    &lt;/System&gt;
    &lt;EventData&gt;
      &lt;Data&gt;3007&lt;/Data&gt;
      &lt;Data&gt;A compilation error has occurred.&lt;/Data&gt;
      &lt;Data&gt;3/12/2011 12:41:21 PM&lt;/Data&gt;
      &lt;Data&gt;3/12/2011 5:41:21 PM&lt;/Data&gt;
      &lt;Data&gt;bc285ecdc00941d28070725f55ea24f1&lt;/Data&gt;
      &lt;Data&gt;3&lt;/Data&gt;
      &lt;Data&gt;1&lt;/Data&gt;
      &lt;Data&gt;0&lt;/Data&gt;
      &lt;Data&gt;/LM/W3SVC/1/ROOT/KSWSExtensionServicesWeb-2-129444252812003152&lt;/Data&gt;
      &lt;Data&gt;Full&lt;/Data&gt;
      &lt;Data&gt;/KSWSExtensionServicesWeb&lt;/Data&gt;
      &lt;Data&gt;C:\inetpub\wwwroot\KSWSExtensionServicesWeb\&lt;/Data&gt;
      &lt;Data&gt;NYDMTRAIN01&lt;/Data&gt;
      &lt;Data&gt;
      &lt;/Data&gt;
      &lt;Data&gt;3988&lt;/Data&gt;
      &lt;Data&gt;w3wp.exe&lt;/Data&gt;
      &lt;Data&gt;NT AUTHORITY\NETWORK SERVICE&lt;/Data&gt;
      &lt;Data&gt;HttpCompileException&lt;/Data&gt;
      &lt;Data&gt;(0): error CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\kswsextensionservicesweb\f2efe503\31c03187\App_global.asax.4gcxry4d.dll' -- 'Access is denied. '
     at System.Web.Compilation.AssemblyBuilder.Compile()
     at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
     at System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp)
     at System.Web.Compilation.BuildManager.CompileGlobalAsax()
     at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
     at System.Web.Compilation.BuildManager.CallAppInitializeMethod()
     at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
  
  &lt;/Data&gt;
      &lt;Data&gt;http://nydmtrain01/KSWSExtensionServicesWeb/Services/KSWSDocumentHistoryService.svc&lt;/Data&gt;
      &lt;Data&gt;/KSWSExtensionServicesWeb/Services/KSWSDocumentHistoryService.svc&lt;/Data&gt;
      &lt;Data&gt;172.25.43.171&lt;/Data&gt;
      &lt;Data&gt;
      &lt;/Data&gt;
      &lt;Data&gt;False&lt;/Data&gt;
      &lt;Data&gt;
      &lt;/Data&gt;
      &lt;Data&gt;NT AUTHORITY\NETWORK SERVICE&lt;/Data&gt;
      &lt;Data&gt;6&lt;/Data&gt;
      &lt;Data&gt;NT AUTHORITY\NETWORK SERVICE&lt;/Data&gt;
      &lt;Data&gt;False&lt;/Data&gt;
      &lt;Data&gt;   at System.Web.Compilation.AssemblyBuilder.Compile()
     at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
     at System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp)
     at System.Web.Compilation.BuildManager.CompileGlobalAsax()
     at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
     at System.Web.Compilation.BuildManager.CallAppInitializeMethod()
     at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
  &lt;/Data&gt;
    &lt;/EventData&gt;
  &lt;/Event&gt;
  </code></pre>