Skip to content

fix(sdk/csharp): client session start, request timeout and error handling - #14

Merged
pjmagee merged 4 commits into
pjmagee:csharp/experimentalfrom
dominik-niebuhr:csharp/client-session-and-fixes
Nov 16, 2026
Merged

pjmagee merged 4 commits into
pjmagee:csharp/experimentalfrom
dominik-niebuhr:csharp/client-session-and-fixes

Conversation

@dominik-niebuhr

@dominik-niebuhr dominik-niebuhr commented Nov 16, 2026 •

Copy link
Copy Markdown

Follow-up to my comment on dagger#15187: fixes for the client-library side, as four commits you can take together or cherry-pick one by one. All of it is under sdk/csharp.

1. fix: remove the 100 s request timeout (GraphQLClient.cs)

HttpClient cancels a request after 100 seconds by default, so a single query that waits for a long exec or a service start failed with TaskCanceledException while the engine was still working. The client now has no request timeout; a caller bounds a call with its CancellationToken.

2. fix: query values and error responses (Types.cs, GraphQLResponse.cs, QueryExecutor.cs)

Found while reading the hand-written client code:

  • Control characters in a string argument went into the query unescaped (now \uXXXX).
  • Integer arguments were formatted with the current culture.
  • An error whose path contains a list index could not be deserialized (List<string>), which hid the real error behind a JsonException.
  • A failed HTTP request with a non-JSON body threw a JsonException; it now throws a DaggerException with the status code and body.

Note: the type of the public property GraphQLError.Path changes from List<string> to List<JsonElement>, which is source-breaking for anyone who reads it.

3. feat: start a session when the environment provides none (CliSession.cs, CliDownloader.cs, CliVersion.cs, GraphQLClient.cs, READMEs)

Without DAGGER_SESSION_PORT / DAGGER_SESSION_TOKEN the client threw, so dotnet run, dotnet test and IDE test runners needed a dagger run wrapper. The READMEs already promised that a compatible CLI is downloaded and run automatically (the sentence is the one from the Python README), but there was no code for it. This adds it, following sdk/go/engineconn:

  1. A provided session is used as before. A port without a token is now an error, as in Go, instead of a reason to start a second session.
  2. _EXPERIMENTAL_DAGGER_CLI_BIN names the CLI to run.
  3. Otherwise the CLI release the SDK is built for is downloaded from dl.dagger.io into the user's cache directory (XDG_CACHE_HOME, else the platform default, as dagger-<version>), checked against checksums.txt, and CLIs of other versions are removed from the cache.
  4. When that release is not published, as for a development build, dagger from the PATH is used with a warning. The PATH is searched explicitly, because starting a process by name in .NET also looks next to the running program and in the current directory.

The CLI is started as dagger session with the SDK labels and --version. Like the Go SDK, the client waits up to five minutes for the session to be reported and, on dispose or process exit, up to five minutes for the CLI to end after its input is closed. The client connects to 127.0.0.1, where the session listens.

Client.Dag no longer caches a failed connection attempt (it was a Lazy<Query>, which keeps the exception for the life of the process). The next access tries again, which matters now that connecting can involve a download and an engine start.

Two things for you to decide:

  • The pinned version. CliVersion.Value is 1.0.0-beta.28, the value of internal/version/VERSION and of the other SDKs' version files on your base commit. It is a constant in a .cs file rather than a csproj property because the SDK sources are also compiled into modules without the csproj. It has to be bumped with releases. The other SDKs get this from a generate function per SDK in .dagger/modules/release/generate.go; there is none for C# yet, and I did not add one because it is outside sdk/csharp. Until 1.0.0-beta.28 is published, every self-started session takes the PATH fallback.
  • No new dependencies. Archives are read with System.Formats.Tar and System.IO.Compression.

4. docs: integration test example (examples/integration-test-example)

An xUnit v3 project that starts PostgreSQL as a Dagger service from a test fixture and reaches it through Host.tunnel. It runs in VSTest mode, so dotnet test works from any directory and from the examples solution without a global.json. It needs commit 3 and therefore uses a ProjectReference.

How it was checked (engine and CLI v1.0.0-beta.27, macOS arm64)

Check Result
dotnet test Dagger.SDK.Tests, no dagger run wrapper (pinned release not published, so the CLI from the PATH is used) 73 passed
dotnet csharpier check . in src clean
dagger functions in examples/constructor-example against the working-tree SDK, which compiles the SDK sources the way a module does loads (an earlier revision of this branch failed here: the module build removes the System.IO using)
Example with no dagger on the PATH, an empty cache and the pin set to 1.0.0-beta.27 CLI downloaded and verified, test passed; second run uses the cached CLI
A 130 s WithExec(["sleep", "130"]) with a self-started session (on an earlier revision) ok, no dagger session process left afterwards
Example with dotnet test, from its own directory and from examples/ passed
Example under dagger run dotnet test (on an earlier revision) passed
Example with dotnet test inside a Dagger container (mcr.microsoft.com/dotnet/sdk:10.0) passed; the calls of the nested client show up under the withExec in the trace

Not checked: Windows and Linux hosts. The download tests build a zip or a tar.gz depending on the platform and need no network, so they should run on Windows; the CliSessionTest cases that use a shell script as a stand-in CLI report inconclusive there. The Windows cache path (%LOCALAPPDATA%\cache\dagger, as in the Go SDK) and the zip extraction have not run on a real Windows machine. Your csharp-client-dev checks against a dev engine have not run either.


🤖 Posted by Claude on behalf of Dominik · Claude Code 2.1.325 · claude-fable-5-2

HttpClient cancels a request after 100 seconds by default. One GraphQL
query can wait much longer than that, for a long exec or for a service
to start, and then failed with a TaskCanceledException although the
engine was still working. The client now has no request timeout; a
caller bounds a call with its CancellationToken.

Signed-off-by: Dominik Niebuhr <[email protected]>
- Control characters in a string argument were written into the query
  unescaped, which the engine rejects. They are now escaped as \uXXXX.
- Integer arguments were formatted with the current culture.
- An error whose path contains a list index could not be deserialized,
  because the path was typed as a list of strings. The original error
  was lost behind a JsonException.
- A failed HTTP request with a body that is not JSON threw a
  JsonException. It now throws a DaggerException with the status code
  and the body.

Signed-off-by: Dominik Niebuhr <[email protected]>
The client only worked under `dagger run` or inside a Dagger container,
where DAGGER_SESSION_PORT and DAGGER_SESSION_TOKEN are set. Anywhere
else it threw, so `dotnet run`, `dotnet test` and the test runner of
an IDE needed a wrapper. The READMEs already promised that a compatible
CLI is downloaded and run automatically; this adds the code for it,
following sdk/go/engineconn:

1. A provided session is used as before. A port without a token is an
   error instead of a reason to start a second session.
2. _EXPERIMENTAL_DAGGER_CLI_BIN names the CLI to run.
3. Otherwise the CLI release the SDK is built for (CliVersion) is
   downloaded from dl.dagger.io into the user's cache directory,
   checked against the published checksum, and CLIs of other versions
   are removed from the cache.
4. When that release is not published, as for a development build,
   `dagger` from the PATH is used with a warning.

The CLI is started as `dagger session` with the SDK labels and the
announced version. Like the Go SDK, the client waits up to five minutes
for the session to be reported and, on dispose or process exit, up to
five minutes for the CLI to end after its input is closed.

Client.Dag no longer caches a failed connection attempt: the next
access tries again, which matters now that connecting can involve a
download and an engine start.

CliVersion is pinned to the version in internal/version/VERSION and has
to be bumped with it, like the version files of the other SDKs.

Signed-off-by: Dominik Niebuhr <[email protected]>
An xUnit test project that starts PostgreSQL as a Dagger service from a
test fixture and reaches it through Host.tunnel. The same `dotnet test`
works on a developer machine, under `dagger run` and inside a Dagger
container.

Signed-off-by: Dominik Niebuhr <[email protected]>
@dominik-niebuhr
dominik-niebuhr force-pushed the csharp/client-session-and-fixes branch from 1d7937d to 114e03e Compare November 16, 2026 12:30
@pjmagee
pjmagee merged commit 4d1a484 into pjmagee:csharp/experimental Nov 16, 2026
@pjmagee

pjmagee commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Merged @dominik-niebuhr, thank you! will test on windows later.

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.

2 participants