Skip to content

StandardAssemblyLoader.LoadAssembly does not respect codeBasePath on .NET Core #439

@jaredpar

Description

@jaredpar

The implementation of StandardAssemblyLoader.LoadAssembly is supposed to consider the codeBasePath parameter when locating the assembly. It attempts to do this by setting the AssemblyName.CodeBase property before calling Assembly.Load(AssemblyName).

var assemblyName = new AssemblyName(assemblyFullName);
if (!string.IsNullOrEmpty(codeBasePath))
{
    assemblyName.CodeBase = codeBasePath;
}

// ends up calling Assembly.Load(AssemblyName)
return this.LoadAssembly(assemblyName);

.NET Core does not respect AssemblyName.CodeBase though hence this has no impact there.

The rough equivalent of this pattern for .NET Core is the following:

var alc = 
  AssemblyLoadContext.CurrentContextualReflectionContext 
  ?? AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly());
try 
{
  return alc.LoadFromAssemblyName(new AssemblyName(assemblyFullName);
} 
catch 
{
  return alc.LoadFromAssemblyPath(codeBase);
}

Hard to 100% tell what the use case is here though as I can't find callers of IAssemblyLoader.LoadAssembly(string, string) in the repo.

Note: this code will appropriately get SYSLIB0044 warnings when targeting net8.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions