What is it?
ASP.NET 4.5 introduces a new feature called Assembly Interning allowing you to run a single instance of a shared assembly in memory. This approach provides performance improvements on cold start and reduces overall server memory usage.
Normally, if you use a common same shared assembly within multiple applications/projects, each shared assembly instance is treated separately and will be loaded into memory when required. This may lead to several copies of the same shared assembly being loaded into memory even though they are the same byte-for-byte.
The aspnet_intern.exe command line utility is part of the Visual Studio SDK (in my case it is located in C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6). This utility will scan the specified source directory (i.e. sourcedir) to see if there are any shared assemblies that are good candidates for consolidating. The shared assemblies will be moved to a common location (i.e. interndir). A symbolic link will be created pointing to the interndir location in place of the relocated assembly.
By default, the utility will only consolidate a given assembly if there are more than 3 references to it, although this behaviour can be overridden.
The utility provides various modes including the analyze and execute modes. The analyze mode will run the utility in a read-only mode i.e. it will NOT make any actual changes to the file system. The execute mode will apply the changes to the file system.
Here's a quick demo of using the intern utility.
First, create a class library project called MyCommonLib, This project will be later referenced by our other projects.
Next I'm going to create 3 simple console applications called MyConsoleApp1, MyConsoleApp2 and MyConsoleApp3 which will reference the MyCommonLib code library.
By default, the aspnet_intern.exe utility will only consolidate any shared dll's it finds if there are more than 3 references to it, that's why I've created 3 console applications here.
Run the aspnet_intern.exe in analyze mode to see what it will do. The source directory points to the path of my above visual studio solution. In this case, the intern utility has found several references to the MyCommonLib assembly I created earlier.
aspnet_intern.exe -mode analyze -sourcedir "<SOURCE_DIRECTORY>" –v
Next I'll run the intern utility in execute mode. Make sure you do this in a command window with administrative privileges.
The interndir points to the path where I want to store my shared assemblies (this can be anywhere you want).
aspnet_intern.exe -mode exec -sourcedir "<SOURCE_DIRECTORY>" -interndir "<COMMON_ASSEMBLIES_DIRECTORY>"
After running the above command, a new folder has been created in the interndir location with the same name as my code library MyCommonLib. The aspnet_intern.exe utility has moved the MyCommonLib assembly to the interndir location and created a symbolic link in place of the MyCommonLib assembly in the sourcedir location.
That's it, that's all there is to it 🙂
One issue I did find along the way, if you run the aspnet_intern.exe utility and find that the symbolic link has NOT been created, you probably forgot to run the utility in a command window with administrative privileges. Run the command in verbose mode (-v) and check for any errors.
- How to: Deploy Azure Functions Using Github Actions - 4th October 2021
- Solved: Set SharePoint Person Field to Null in Power Automate - 16th September 2021
- Visual Studio: Generate Class From JSON String - 17th August 2021