Assembly-related API behavior changes for single-file publishing format

Multiple APIs related to an assembly's file location have behavior changes when they're invoked in a single-file publishing format.

Change description

In single-file publishing for .NET 5 and later versions, bundled assemblies are loaded from memory instead of extracted to disk. For single-file published apps, this means that certain location-related APIs return different values on .NET 5 and later than on previous versions of .NET. The changes are as follows:

API Previous versions .NET 5 and later
Assembly.Location Returns extracted DLL file path Returns empty string for bundled assemblies
Assembly.CodeBase Returns extracted DLL file path Throws exception for bundled assemblies
Assembly.GetFile(String) Returns null for bundled assemblies Throws exception for bundled assemblies
Environment.GetCommandLineArgs()[0] Value is the name of the entry point DLL Value is the name of the host executable
AppContext.BaseDirectory Value is the temporary extraction directory Value is the containing directory of the host executable

Version introduced

5.0

Avoid dependencies on the file location of assemblies when publishing as a single file.

Affected APIs