Skip to content

feat(interceptors): added Interceptor page for .NET#4603

Open
flippedcoder wants to merge 3 commits into
mainfrom
mm/dotnet-interceptors
Open

feat(interceptors): added Interceptor page for .NET#4603
flippedcoder wants to merge 3 commits into
mainfrom
mm/dotnet-interceptors

Conversation

@flippedcoder
Copy link
Copy Markdown
Contributor

@flippedcoder flippedcoder commented May 21, 2026

What does this PR do?

Addresses #3875. .NET didn't have a page for interceptors and it's a request that's been brought up in the community a few times.

Notes to reviewers

┆Attachments: EDU-6406 feat(interceptors): added Interceptor page for .NET

@flippedcoder flippedcoder requested a review from a team as a code owner May 21, 2026 13:44
@vercel
Copy link
Copy Markdown

vercel Bot commented May 21, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
temporal-documentation Ready Ready Preview, Comment May 21, 2026 7:23pm

Request Review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 21, 2026

📖 Docs PR preview links

Copy link
Copy Markdown
Contributor

@brianmacdonald-temporal brianmacdonald-temporal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of small notes

Comment thread docs/develop/dotnet/workers/interceptors.mdx Outdated
Comment thread docs/develop/dotnet/workers/interceptors.mdx Outdated

Interceptors are SDK hooks that let you intercept inbound and outbound Temporal calls. You use them to apply shared
behavior across many calls, such as tracing and authorization, before calls reach the application code and after they return.
This is similar to middleware in other frameworks.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this line. I think it helps people immediately reify the idea.

Pass interceptors in the `Interceptors` argument of `TemporalClient.ConnectAsync`. Client interceptors modify outbound calls such
as starting and signaling Workflows.

```dotnet
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be csharp


Interceptors are SDK hooks that let you intercept inbound and outbound Temporal calls. You use them to apply shared
behavior across many calls, such as tracing and authorization, before calls reach the application code and after they return.
This is similar to middleware in other frameworks.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python calls out some specific middleware examples. Analogous example for .NET would be https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware

* Outbound interceptors wrap network calls, running before they reach the network and after they return.
* Inbound interceptors run after the network hop, wrapping application code and running before it starts and after it returns.

Those further break down into different interceptor types.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include a table or list of the interceptor types, what they are used for, where they run, and examples of things that call them?


### Register on the Client

Pass interceptors in the `Interceptors` argument of `TemporalClient.ConnectAsync`. Client interceptors modify outbound calls such
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically not correct. Interceptors is a property on TemporalClientConnectOptions (which is what the new() is creating in the following example).


### Register on the Worker only

If your interceptor doesn't affect the Client, you can pass interceptors in the `Interceptors` argument of `TemporalWorker()`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as https://github.com/temporalio/documentation/pull/4603/changes#r3284706388, but the options type is TemporalWorkerOptions. The sample below shows it correctly.


### Implementing Worker call Interceptors

To modify inbound Workflow and Activity calls, define a class implementing [`IWorkerInterceptor`](https://dotnet.temporal.io/api/Temporalio.Worker.Interceptors.IWorkerInterceptor.html). It provides `InterceptActivity()`, `InterceptWorkflow()`, and `InterceptNexusOperation()` methods for Activity, Workflow, and Nexus interception.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InterceptNexusOperation should be added into the sample below, even if it just returns the next interceptor, just like InterceptWorkflow. Using snipsync would reveal this issue because the code would actually be built in the temporalio/features repo.

var client = await TemporalClient.ConnectAsync(new()
{
TargetHost = "localhost:7233",
Interceptors = [interceptor],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python sample uses TracingInterceptor as the example here, and this is available in the .NET SDK as well. Consider doing the same and maybe refer to the observability doc.

client,
new TemporalWorkerOptions("my-task-queue")
{
Interceptors = new IWorkerInterceptor[]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use collection syntax (e.g. [interceptor]) instead of the explicit array syntax, just like the above example?

@@ -0,0 +1,204 @@
---
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a page in the encyclopedia that needs to be updated to link to this new page?


Workflow inbound and outbound interceptor methods also execute during [replay](/develop/dotnet/best-practices/testing-suite#replay). Use replay-safe APIs for logging, randomness, and time in these interceptors.
See [Develop Workflow logic](/develop/dotnet/workflows/basics#workflow-logic-requirements) for details.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python interceptors doc has some more information about writing generic code... The same can be written here and I think the check would be Workflow.Unsafe.IsReplaying

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.

4 participants