microsoft/semantic-kernel · error · InvalidOperationException
Could not determine the assembly path.
Error message
Could not determine the assembly path.
What it means
Thrown in the relative-path branch when Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) returns null. This indicates the assembly has no file location (e.g. loaded from a single-file publish bundle, in-memory, or a dynamic assembly), so the renderer cannot derive a base output folder.
Source
Thrown at dotnet/samples/GettingStartedWithProcesses/Utilities/MermaidRenderer.cs:50
{
// Use the provided absolute path
outputFilePath = filenameOrPath;
// Ensure the directory exists
string directoryPath = Path.GetDirectoryName(outputFilePath)
?? throw new InvalidOperationException("Could not determine the directory path.");
if (!Directory.Exists(directoryPath))
{
throw new DirectoryNotFoundException($"The directory '{directoryPath}' does not exist.");
}
}
else
{
// Use the assembly's directory for relative paths
string? assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (assemblyPath == null)
{
throw new InvalidOperationException("Could not determine the assembly path.");
}
string outputPath = Path.Combine(assemblyPath, "output");
Directory.CreateDirectory(outputPath); // Ensure output directory exists
outputFilePath = Path.Combine(outputPath, filenameOrPath);
}
// Download Chromium if it hasn't been installed yet
BrowserFetcher browserFetcher = new();
browserFetcher.Browser = SupportedBrowser.Chrome;
await browserFetcher.DownloadAsync();
// Define the HTML template with Mermaid.js CDN
string htmlContent = $@"
<html>
<head>
<style>
body {{View on GitHub (pinned to c028a0c7dc)
Solutions
- Pass an absolute path so the relative branch is skipped entirely.
- Use AppContext.BaseDirectory instead of Assembly.Location in caller setup, and pass an absolute path built from it.
Example fix
// before await MermaidRenderer.GenerateMermaidImageAsync(code, "diagram.png"); // fails under single-file publish // after string abs = Path.Combine(AppContext.BaseDirectory, "output", "diagram.png"); await MermaidRenderer.GenerateMermaidImageAsync(code, abs);
Defensive patterns
Strategy: validation
Validate before calling
string baseDir = !string.IsNullOrEmpty(Assembly.GetExecutingAssembly().Location)
? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!
: AppContext.BaseDirectory;
string abs = Path.Combine(baseDir, "output", filename);
await MermaidRenderer.GenerateMermaidImageAsync(code, abs); Type guard
bool HasAssemblyLocation() => !string.IsNullOrEmpty(Assembly.GetExecutingAssembly().Location);
Try / catch
try { await MermaidRenderer.GenerateMermaidImageAsync(code, filename); }
catch (InvalidOperationException ex) when (ex.Message.Contains("assembly path")) { /* pass an absolute path built from AppContext.BaseDirectory */ } Prevention
- Under single-file publish, prefer AppContext.BaseDirectory over Assembly.Location.
- Pass absolute output paths to avoid the assembly-derived relative branch.
- Test rendering under single-file deployment in CI.
When it happens
Trigger: Calling GenerateMermaidImageAsync with a relative filename when the executing assembly's Location is empty (single-file app, in-memory load, AppContext.BaseDirectory-only host).
Common situations: Publishing as a single-file executable; running tests where the assembly is loaded in-memory; container/scenarios where Location is not set.
Related errors
- The filename must have a .png extension.
- Could not determine the directory path.
- The directory '{directoryPath}' does not exist.
- An error occurred while accessing the file.
- File '{fileName}' not found.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/1184b18c8444e0ee.
Report an issue: GitHub.