Skip to content

[API Proposal]: Support non-locking assembly loading with preserved Assembly.Location #127097

Closed

Description

@cdmazom

Background and motivation

We’re running into an issue with assembly loading in the Unity Editor, and we ended up customizing our fork of the runtime to solve it. Since we've seen other users hit similar roadblocks, we wanted to see if an upstream solution makes sense.

When we load an assembly from a path via AssemblyLoadContext.LoadFromAssemblyPath, the runtime maps the file into memory. On Windows, particularly, this creates an OS-level lock. This interferes with standard Unity Editor workflows as it prevents us (or our users) from performing common operations on those files while the editor is running, like recompiling the assembly, renaming the file, moving it, or deleting it.

The obvious workaround is to read the file into a stream and load it from memory instead. However, this breaks third-party libraries that rely on Assembly.Location (e.g., to find adjacent assets or native dependencies), because loading from a stream leaves the location empty.

Previous Discussions and Related Issues

This is a long-standing challenge within the ecosystem, with several issues highlighting the same friction around file locks and metadata preservation:

API Proposal

We propose adding a static callback to AssemblyLoadContext that allows an application to override the value returned by Assembly.Location. To ensure predictability, this callback should only be settable once for the whole application.

namespace System.Runtime.Loader
{
    public partial class AssemblyLoadContext
    {
        /// <summary>
        /// Sets a global callback to override the value returned by Assembly.Location.
        /// This method can only be called once per application.
        /// </summary>
        /// <param name="locationOverride">
        /// A function that takes the Assembly and its original location (if any), 
        /// and returns the overriding location string.
        /// </param>
        public static void SetAssemblyLocationOverride(Func<Assembly, string, string> locationOverride);
    }
}

API Usage

// 1. Set the callback once during application startup
AssemblyLoadContext.SetAssemblyLocationOverride((assembly, originalLocation) =>
{
    // Consult a custom mapping dictionary (or ALC context) to find the fake location
    if (CustomLoader.TryGetMappedPath(assembly, out string mappedPath))
    {
        return mappedPath;
    }
    
    return originalLocation;
});

// 2. Load the assembly from memory to avoid file locks
var alc = new AssemblyLoadContext("ExampleContext");
var assembly = alc.LoadFromStream(exampleLibStream);

// 3. Register the mapping so the callback returns the expected physical path
CustomLoader.RegisterMapping(assembly, "third-party-lib/example_lib.dll");

// Third-party code can now successfully resolve assembly location
var location = Path.GetDirectoryName(assembly.Location);

Alternative Designs

  • Virtual method on ALC: We also considered allowing AssemblyLoadContext to override it internally via a protected virtual method, e.g., protected internal virtual string ResolveAssemblyLocation(Assembly assembly, string originalLocation). This would scope the override to specific contexts rather than relying on a global static hook and wouldn't work for the default ALC.
  • Dedicated In-Memory Load Method: LoadFromAssemblyPathWithoutLocking(string assemblyPath). This was our original proposal. We discarded it as discussed in this thread to keep the runtime out of the business of managing file-locking trade-offs.

Risks

  • Global State Conflicts: Because the callback is a static, set-once delegate, there is a risk of conflict if multiple independent frameworks or complex hosting scenarios within the same process attempt to claim it.

Changelog

  • Updated: Replaced the LoadFromAssemblyPathWithoutLocking API proposal with a static callback (SetAssemblyLocationOverride) to override assembly resolution, based on feedback regarding runtime responsibilities.

Activity

  1. added
    api-suggestion
    on Apr 17, 2026
  2. added
    untriaged
    on Apr 17, 2026
  3. jkotas commented on Apr 18, 2026

    @jkotas
    Member

    Reading the content of the file into memory is just one of the possible strategies used to deal with the file locking problem. Alternative includes copying or linking the file to some other path and loading it from there to allow original location to be mutated. (This technique was called "shadow copy" in .NET Framework - https://learn.microsoft.com/en-us/dotnet/framework/app-domains/shadow-copy-assemblies.) Each of these options come with different tradeoffs. It would be best for the runtime to stay away from choosing the tradeoff.

    The runtime does not need to be in the business of enforcing that the value returned by the Location property makes sense. I think the simplest and most flexible way to address your issue is to allow location property to be set to an arbitrary path after the assembly is loaded - introduce an API like static void AssemblyLoadContext.SetAssemblyLocation(Assembly assembly, string location).

    We allow similar arbitrary overriding of AppDomain.BaseDirectory and Assembly.GetEntryAssembly with similar reasoning.

  4. cdmazom commented on Apr 18, 2026

    @cdmazom
    ContributorAuthor

    I think the simplest and most flexible way to address your issue is to allow location property to be set to an arbitrary path after the assembly is loaded - introduce an API like static void AssemblyLoadContext.SetAssemblyLocation(Assembly assembly, string location).

    I agree on the simplicity and flexibility, I'm very open to this direction. But would you expect any restrictions or guards on that API? (e.g. making it "set-once" or potentially a protected method?)

    Part of the motivation of my initial proposal was to limit the scope of the assignment to the load phase to ensure immutability at the API level. Is that window between the load and the setter call something we should be concerned about?

  5. jkotas commented on Apr 19, 2026

    @jkotas
    Member

    How about a static callback that cannot be set more than once for the whole app: static void AssemblyLoadContext.SetAssemblyLocationOverride(Func<Assembly, string, string> locationOverride);? Assembly.Location would call this callback to allow overriding the returned value.

    Assembly.Location is not compatible with single file and native AOT (it returns empty string there). Modern .NET libraries should not be depending on Assembly.Location in the first place. I think about the proposed API as a compat quirk to allow the existing libraries that depends on Assembly.Location to work with custom assembly loaders. It is best effort that is not trying to be 100% reliable. For example, the 3rd party library can lock other the files in the original Assembly.Location path and you are back to square one.

  6. dotnet-policy-service commented on Apr 20, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @elinor-fung
    See info in area-owners.md if you want to be subscribed.

  7. cdmazom commented on Apr 20, 2026

    @cdmazom
    ContributorAuthor

    Wondering if it'd make more sense to allow the AssemblyLoadContext to override it...

    public partial class AssemblyLoadContext
    {
        protected internal virtual string ResolveAssemblyLocation(Assembly, string originalLocation)
        {
            return originalLocation;
        }
    }
    
  8. jkotas commented on Apr 21, 2026

    @jkotas
    Member

    It would not work for the default assembly load context.

  9. cdmazom commented on May 6, 2026

    @cdmazom
    ContributorAuthor

    The static callback seems to be working properly for us, I've updated the proposal.

  10. added
    api-ready-for-review
    and removed
    api-suggestion
    on May 6, 2026
  11. removed
    untriaged
    on May 12, 2026
  12. added this to the 11.0.0 milestone on May 12, 2026
  13. bartonjs commented on Jun 9, 2026

    @bartonjs
    Member

    Video

    Looks good as proposed

    namespace System.Runtime.Loader
    {
        public partial class AssemblyLoadContext
        {
            public static void SetAssemblyLocationOverride(Func<Assembly, string, string> locationOverride);
        }
    }
  14. added
    api-approved
    and removed
    api-ready-for-review
    on Jun 9, 2026
  15. jkotas commented on Jun 9, 2026

    @jkotas
    Member

    @cdmazom The API was approved. Would you like to propose a PR with the implementation?

  16. cdmazom commented on Jun 9, 2026

    @cdmazom
    ContributorAuthor

    Will do. Thank you!

  17. added a commit that references this issue on Jul 7, 2026
    8988437
  18. added a commit that references this issue on Jul 15, 2026
    4935c4f
  19. locked and limited conversation to collaborators on Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-AssemblyLoader

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions