dotnet/orleans · error · FileNotFoundException

Cannot find file {filename}

Error message

Cannot find file {filename}

What it means

Thrown by Secrets.LoadFromFile after it walks from the current working directory up through every parent directory looking for the secrets file (default Secrets.json) and cannot find it anywhere in that chain. The Streaming samples use this to load Event Hub / service credentials at runtime.

Source

Thrown at samples/Streaming/Common/Secrets.cs:38

        EventHubConnectionString = eventHubConnectionString
            ?? throw new ArgumentException(
                "Must provide an eventHubConnectionString", nameof(eventHubConnectionString));
    }

    public static Secrets? LoadFromFile(string filename = "Secrets.json")
    {
        var currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
        while (currentDir != null && currentDir.Exists)
        {
            var filePath = Path.Combine(currentDir.FullName, filename);
            if (File.Exists(filePath))
            {
                return JsonSerializer.Deserialize<Secrets>(File.ReadAllText(filePath));
            }

            currentDir = currentDir.Parent;
        }
        throw new FileNotFoundException($"Cannot find file {filename}");
    }

    public static Secrets? TryLoadFromFile(string filename = "Secrets.json")
    {
        var currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
        while (currentDir != null && currentDir.Exists)
        {
            var filePath = Path.Combine(currentDir.FullName, filename);
            if (File.Exists(filePath))
            {
                var secrets = JsonSerializer.Deserialize<Secrets>(File.ReadAllText(filePath));
                // Return null if secrets file exists but has empty/missing values
                if (secrets is null ||
                    string.IsNullOrWhiteSpace(secrets.DataConnectionString) ||
                    string.IsNullOrWhiteSpace(secrets.EventHubConnectionString))
                {
                    return null;
                }

View on GitHub (pinned to fca799fa70)

Solutions

  1. Create samples/Streaming/Common/Secrets.json from the Secrets.template.json (or sample README) and fill in your Event Hub / storage connection strings.
  2. Run the sample from the sample's project directory so the upward directory walk starts near the file.
  3. Prefer Secrets.TryLoadFromFile (returns null instead of throwing) if missing secrets should degrade gracefully rather than crash.

Example fix

// before
var secrets = Secrets.LoadFromFile("Secrets.json"); // throws if absent

// after: create the file from the template, or tolerate absence
var secrets = Secrets.TryLoadFromFile("Secrets.json");
if (secrets is null) { Console.WriteLine("Configure Secrets.json first."); return; }
Defensive patterns

Strategy: validation

Validate before calling

// Prefer the non-throwing loader and check for null
var secrets = Secrets.TryLoadFromFile("Secrets.json");
if (secrets is null || string.IsNullOrEmpty(secrets.EventHubConnectionString))
{
    Console.WriteLine("Configure Secrets.json (see Secrets.template.json) before running.");
    return;
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling Secrets.LoadFromFile("Secrets.json") (or TryLoadFromFile used as a non-throwing variant) when Secrets.json is absent from the bin output directory and all of its ancestors up to the filesystem root. Common because the file is git-ignored and must be created per-developer.

Common situations: First run of a Streaming sample without copying the provided Secrets.template.json to Secrets.json; running from a working directory that is not under the sample folder; the file was named with a different case on a case-sensitive filesystem.

Related errors


AI-assisted analysis of dotnet/orleans@fca799fa70 (2026-08-13). Data as JSON: /api/errors/4b00894cec4a80f6. Report an issue: GitHub.