IIS 8.0 — How is .NET Framework Temp Folder Path Generated?

Robin Ding
3 min readDec 25, 2017

--

In IIS, as we know, once we create an ASP.NET website or ASP.NET Application, IIS will generate a temporary folder path for the dynamical compilation of *.ASPX, *.ASCX files. Most likely, you can find the temp folder:

  • 32bit OS: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\a3e47a87\663d5fba
  • 64bit OS: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a3e47a87\663d5fba

The .NET Framework version folder v.4.0.30319 is based on the version which you use for IIS website or application.

How does IIS generate this temp folder for each website or application, especially we have a lot of websites or applications which are needed to be managed in one IIS?

How to get the .NET Framework Temp Folder for a specific website or application in IIS?

Maybe the first question is that how can I get the .NET Framework Temp Folder? This is really useful when we want to troubleshoot or clear cache files.

From ASP.NET Code

In the ASP.NET code, you can use below code to get above temp folder path.

System.Threading.Thread.GetDomain().DynamicDirectory

If you want to get more detail about how ASP.NET set this value, you can take a look its source code: HttpRuntime.SetUpCodegenDirectory

How does IIS generate .NET Framework Temp Folder?

At first, let’s take a look the format this temp folder:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a3e47a87\663d5fba

=>

Temp Folder ->
{AppDomain.CurrentDomain.SetupInformation.DynamicBase}\AppDomain.CurrentDomain.SetupInformation.ApplicationName}

AppDomain.CurrentDomain.SetupInformation.DynamicBase ->
C:\Windows\Microsoft.NET\Framework{32 or 64}\{.NET Version}\Temporary ASP.NET Files\{Website?"Root":{VirtualPath Name}}\{Hash Code}

Breakdown

  • From the source code: System.Web.Hosting.AppManagerAppDomainFactory.ConstructSimpleAppName, we know:
    If it’s a website, IIS will use “root” as the folder name after “Temporary ASP.NET Files”. If it’s application, IIS will use the Virtual Path Name as the folder name.
internal static String ConstructSimpleAppName(string virtPath, bool isDevEnvironment) {
// devdiv 710164: Still repro - ctrl-f5 a WAP project might show "Cannot create/shadow copy when a file exists" error
// since the hash file lists are different, IISExpress launched by VS cannot reuse the build result from VS build.
// It deletes the build files generated by CBM and starts another round of build. This causes interruption between IISExpress and VS
// and leads to this shallow copy exception.
// fix: make the dev IISExpress build to a special drop location
// devdiv 1038337: execution permission cannot be acquired under partial trust with ctrl-f5.
// We previously use HostingEnvironment to determine whether it is under dev environment and the returned string. However,
// for partial trust scenario, this method is called before HostingEnvironment has been initialized, we thus may return a wrong value.
// To fix it, we requir the caller to pass in a flag indicating whether it is under dev environment.
if (virtPath.Length <= 1) { // root?
if (!BuildManagerHost.InClientBuildManager && isDevEnvironment)
return "vs";
else
return "root";
}
else
return virtPath.Substring(1).ToLower(CultureInfo.InvariantCulture).Replace('/', '_');
}
  • About AppDomain.CurrentDomain.SetupInformation.ApplicationName, it is AppDomainSetup.ApplicationName. There is an example how to set this.
// Prepare to create a new application domain.
AppDomainSetup setup = new AppDomainSetup();

// Set the application name before setting the dynamic base.
setup.ApplicationName = "Example";

// Set the location of the base directory where assembly resolution
// probes for dynamic assemblies. Note that the hash code of the
// application name is concatenated to the base directory name you
// supply.
setup.DynamicBase = "C:\\DynamicAssemblyDir";
Console.WriteLine("DynamicBase is set to '{0}'.", setup.DynamicBase);

AppDomain ad = AppDomain.CreateDomain("MyDomain", null, setup);

// The dynamic directory name is the dynamic base concatenated with
// the application name: <DynamicBase>\<hash code>\<ApplicationName>
string dynamicDir = ad.DynamicDirectory;
Console.WriteLine("Dynamic directory is '{0}'.", dynamicDir);

(From ApplicationHost.CreateApplicationHost)

String domainId = ConstructAppDomainId(appId);
String appName = (StringUtil.GetStringHashCode(String.Concat(appId.ToLower(CultureInfo.InvariantCulture),
physicalPath.ToLower(CultureInfo.InvariantCulture)))).ToString("x", CultureInfo.InvariantCulture);
VirtualPath virtualPath = VirtualPath.Create(appHost.GetVirtualPath());
  • About the AppDomainSetup.DynamicBase, it’s referred to an external method. Still haven’t found any detail yet.

How to customize .NET Framework Temp Folder instead of using default value?

ASP.NET is very flexible, we can configure web.config to customize .NET Framework Temp Folder:

<compilation tempDirectory="D:\MoveThemHere">

But, please remember to setup correct permission for the application identity to be able to modify above folder.

You can also find this from: https://dingyuliang.me/iis-8-0-net-framework-temp-folder-path-generated/

--

--

No responses yet