Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 4, 2025

This PR fixes a regression introduced in version 3.9.1 where TokenAcquirerFactory.GetDefaultInstance() throws ArgumentNullException when AppContext.BaseDirectory is a root directory.

Problem

In version 3.9.1, the DefineConfiguration method was changed from using Assembly.Location to AppContext.BaseDirectory:

Before (v3.9.0):

Assembly assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
return Path.GetDirectoryName(assembly.Location);

After (v3.9.1):

return Path.GetDirectoryName(AppContext.BaseDirectory)!;

The issue occurs because:

  • Assembly.Location returns the full path to the assembly file (e.g., "C:\MyApp\bin\MyApp.dll")
  • AppContext.BaseDirectory returns a directory path (e.g., "C:\" or "/")
  • When AppContext.BaseDirectory is a root directory, Path.GetDirectoryName() returns null
  • This null value causes SetBasePath(null) to throw ArgumentNullException

Solution

Modified the DefineConfiguration method to handle the null case by falling back to the original AppContext.BaseDirectory:

protected virtual string DefineConfiguration(IConfigurationBuilder builder)
{
    string? basePath = Path.GetDirectoryName(AppContext.BaseDirectory);
    return !string.IsNullOrEmpty(basePath) ? basePath : AppContext.BaseDirectory;
}

This ensures that:

  • Normal cases continue to work as expected (e.g., "/app/bin/""/app/bin")
  • Root directory cases are handled gracefully (e.g., "/""/" when Path.GetDirectoryName("/") returns null)
  • The method never returns null or empty string

Testing

Added comprehensive unit tests that verify:

  • The method handles null results from Path.GetDirectoryName()
  • Various edge cases with root paths work correctly
  • Normal directory paths continue to function as before

The fix is minimal and surgical, changing only the problematic logic while preserving all existing functionality.

Fixes #3425.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Id.Web - Inability to instantiate TokenAcquirerFactory Fix TokenAcquirerFactory null reference exception when AppContext.BaseDirectory is root path Aug 4, 2025
Copilot AI requested a review from pmaytak August 4, 2025 22:04
Copilot finished work on behalf of pmaytak August 4, 2025 22:04
@pmaytak pmaytak marked this pull request as ready for review August 6, 2025 22:25
@pmaytak pmaytak requested a review from a team as a code owner August 6, 2025 22:25
@pmaytak
Copy link
Contributor

pmaytak commented Aug 6, 2025

This was referenced Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Id.Web - Inability to instantiate TokenAcquirerFactory

4 participants