microsoft/autogen · error · DirectoryNotFoundException
Could not find directory for assembly '{asm}'.
Error message
Could not find directory for assembly '{asm}'. What it means
The seed-memory tool throws DirectoryNotFoundException when Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) returns null/empty. Assembly.Location is empty for assemblies loaded from a bundle — most notably single-file publishes and some trimming/AOT scenarios — so the tool cannot locate the PDF file it is supposed to import into semantic memory.
Source
Thrown at dotnet/samples/dev-team/seed-memory/Program.cs:39
.AddDebug();
});
var memoryBuilder = new MemoryBuilder();
var memory = memoryBuilder.WithLoggerFactory(loggerFactory)
.WithQdrantMemoryStore(kernelSettings.QdrantEndpoint, 1536)
.WithAzureOpenAITextEmbeddingGeneration(kernelSettings.EmbeddingDeploymentOrModelId, kernelSettings.Endpoint, kernelSettings.ApiKey)
.Build();
await ImportDocumentAsync(memory, WafFileName).ConfigureAwait(false);
}
public static async Task ImportDocumentAsync(ISemanticTextMemory memory, string filename)
{
var asm = Assembly.GetExecutingAssembly();
var currentDirectory = Path.GetDirectoryName(asm.Location);
if (currentDirectory is null)
{
throw new DirectoryNotFoundException($"Could not find directory for assembly '{asm}'.");
}
var filePath = Path.Combine(currentDirectory, filename);
using var pdfDocument = PdfDocument.Open(File.OpenRead(filePath));
var pages = pdfDocument.GetPages();
foreach (var page in pages)
{
try
{
var text = ContentOrderTextExtractor.GetText(page);
var descr = text.Take(100);
await memory.SaveInformationAsync(
collection: "waf",
text: text,
id: $"{Guid.NewGuid()}",
description: $"Document: {descr}").ConfigureAwait(false);
}
catch (Exception ex)View on GitHub (pinned to 027ecf0a37)
Solutions
- Prefer AppContext.BaseDirectory (the directory of the app binary, reliable in single-file mode) instead of Assembly.Location.
- Or run the tool via 'dotnet run' / normal (non-single-file) publish while seeding memory, then deploy.
- Ensure the WAF PDF file (WafFileName) is copied to the output directory (CopyToOutputDirectory in the csproj) so it sits next to the resolved base path.
- If embedded, read the resource stream instead of File.OpenRead on a disk path.
Example fix
// before
var asm = Assembly.GetExecutingAssembly();
var currentDirectory = Path.GetDirectoryName(asm.Location);
if (currentDirectory is null) { throw new DirectoryNotFoundException(...); }
// after
var currentDirectory = AppContext.BaseDirectory; // valid even for single-file publish
var filePath = Path.Combine(currentDirectory, filename); Defensive patterns
Strategy: validation
Validate before calling
var baseDir = AppContext.BaseDirectory; // always valid, even for single-file publish
if (!File.Exists(Path.Combine(baseDir, filename)))
{
Console.Error.WriteLine($"'{filename}' not found next to the executable in {baseDir}.");
return;
} Try / catch
try { await ImportDocumentAsync(memory, WafFileName); } catch (DirectoryNotFoundException) { Console.Error.WriteLine("Assembly location unavailable (single-file publish?). Seeding must run from a normal publish or use AppContext.BaseDirectory."); } Prevention
- Use AppContext.BaseDirectory instead of Assembly.GetExecutingAssembly().Location whenever the binary's folder is needed — Location is empty in single-file bundles.
- Mark data files CopyToOutputDirectory in the csproj so they land beside the binary in every publish mode.
- Smoke-test publish pipelines (including single-file) with the seeding step before shipping.
When it happens
Trigger: Running seed-memory after 'dotnet publish -p:PublishSingleFile=true'; running the tool from a single-file executable on Linux/macOS; any context where the assembly is loaded from memory rather than a file on disk.
Common situations: DevTeam sample deployed as a self-contained single-file binary; CI packaging step publishes single-file and runs the seed tool in the pipeline; .NET 5+ single-file behavior change vs old .NET Framework where Location always worked.
Related errors
- Please set OPENAI_API_KEY environment variable.
- Please set ANTHROPIC_API_KEY environment variable.
- Missing ANTHROPIC_API_KEY environment variable.
- Missing ANTHROPIC_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/bb8f74a8f7bdd5b4.
Report an issue: GitHub.