Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/orleans/grains/request-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,28 @@ Console.WriteLine(message);
```

In this example, the client sets the trace ID to "example-id-set-by-client" before calling the `SayHello` method on the `HelloGrain`. The grain retrieves the trace ID from the request context and logs it.

## Example placement access code

The <xref:Orleans.Runtime.RequestContext> data can be accessed during Placement or Placement Filtering through the <xref:Orleans.Runtime.Placement.PlacementTarget> `RequestContextData`. The static <xref:Orleans.Runtime.RequestContext> is not populated at this time as there is not yet an activation of the grain. The following placement filter code demonstrates how to get <xref:Orleans.Runtime.RequestContext> data while handling placement:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
The <xref:Orleans.Runtime.RequestContext> data can be accessed during Placement or Placement Filtering through the <xref:Orleans.Runtime.Placement.PlacementTarget> `RequestContextData`. The static <xref:Orleans.Runtime.RequestContext> is not populated at this time as there is not yet an activation of the grain. The following placement filter code demonstrates how to get <xref:Orleans.Runtime.RequestContext> data while handling placement:
The <xref:Orleans.Runtime.RequestContext> data can be accessed during placement or placement filtering through the <xref:Orleans.Runtime.Placement.PlacementTarget> `RequestContextData`. The static <xref:Orleans.Runtime.RequestContext> is not populated at this time as there is not yet an activation of the grain. The following placement filter code demonstrates how to get <xref:Orleans.Runtime.RequestContext> data while handling placement:


```csharp
internal sealed class ExamplePlacementFilterDirector(ILogger<ExamplePlacementFilterDirector> logger)
: IPlacementFilterDirector
{
public IEnumerable<SiloAddress> Filter(
PlacementFilterStrategy filterStrategy,
PlacementTarget target,
IEnumerable<SiloAddress> silos)
{
if (target.RequestContextData.TryGetValue("somekey", out var somevalue)
&& somevalue is string somestring)
{
logger.LogInformation("Read {Value} for {Key} from the RequestContext", somestring, "somekey");
// somestring is available for the filtering logic
}
return silos;
}
}
```
In this example, the "somekey" value is read from the <xref:Orleans.Runtime.Placement.PlacementTarget> `RequestContextData` during the placement filtering.
Loading