Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Dapr .NET SDK documentation #1409

Merged
merged 50 commits into from
Dec 12, 2024
Merged
Changes from 9 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
ed77b49
Updated documentation to reflect new DaprClient DI injection capabili…
WhitWaldo Nov 20, 2024
748b21c
Clarified relationship between DAPR_HTTP_ENDPOINT and DAPR_HTTP_PORT …
WhitWaldo Nov 20, 2024
c2f15fd
Clarified configuration prioritization order on DaprClientBuilder as …
WhitWaldo Nov 21, 2024
db626c4
Merge branch 'master' into daprclient-docs
WhitWaldo Nov 21, 2024
b6ee842
Merge branch 'master' into daprclient-docs
WhitWaldo Nov 24, 2024
74b7cf2
Merge branch 'master' into daprclient-docs
WhitWaldo Dec 3, 2024
c0aac20
Merge branch 'master' into daprclient-docs
WhitWaldo Dec 4, 2024
9e29773
Merge branch 'master' into daprclient-docs
WhitWaldo Dec 4, 2024
a5e1506
Merge branch 'master' into daprclient-docs
WhitWaldo Dec 5, 2024
6a30087
Fixed typo - great catch Philip!
WhitWaldo Dec 10, 2024
db59897
Merge remote-tracking branch 'origin/daprclient-docs' into daprclient…
WhitWaldo Dec 10, 2024
6186b0b
Merge branch 'master' into daprclient-docs
WhitWaldo Dec 10, 2024
392232e
Added information about using Dapr.Jobs favoring dependency injection
WhitWaldo Dec 10, 2024
8ca9b8e
Merge remote-tracking branch 'origin/daprclient-docs' into daprclient…
WhitWaldo Dec 10, 2024
7dce4d0
Building out .NET AI docs
WhitWaldo Dec 10, 2024
d34d8c2
Merge remote-tracking branch 'origin/daprclient-docs' into daprclient…
WhitWaldo Dec 10, 2024
ed64004
Building out .NET AI docs
WhitWaldo Dec 10, 2024
8463451
Merge branch 'master' into daprclient-docs
WhitWaldo Dec 11, 2024
10a4607
Added first-draft of .NET Aspire docs
WhitWaldo Dec 11, 2024
1577fc0
Added first-draft of .NET Aspire docs
WhitWaldo Dec 11, 2024
f63b390
Added first-draft of .NET Aspire docs
WhitWaldo Dec 11, 2024
5a885e4
Reordered the weighting of the development docs to reflect investment…
WhitWaldo Dec 11, 2024
2490222
Updated .NET SDK links to point to a better endpoint
WhitWaldo Dec 11, 2024
f52c284
Merge remote-tracking branch 'origin/daprclient-docs' into daprclient…
WhitWaldo Dec 11, 2024
dbf7b6c
Updated more of the .NET SDK links
WhitWaldo Dec 11, 2024
c0579dc
Reweighted document order
WhitWaldo Dec 11, 2024
26aa2b6
Building out pubsub docs
WhitWaldo Dec 11, 2024
6a6afd2
Tweak to clarify use of the Dapr SDK
WhitWaldo Dec 11, 2024
e02e112
Added missing whitespace for clarity
WhitWaldo Dec 11, 2024
c7a4b8c
Simplified alert about .NET versioning
WhitWaldo Dec 11, 2024
f4be508
Added Dapr.Jobs as a prereq
WhitWaldo Dec 11, 2024
d1ae5e3
Added some minor formatting tweaks
WhitWaldo Dec 11, 2024
8a3784f
Added body of the pubsub how to documentation
WhitWaldo Dec 11, 2024
48558d8
Updated table layout + reformatted
WhitWaldo Dec 11, 2024
76b322e
Added note about using DI functionality in best practices
WhitWaldo Dec 11, 2024
d2ec7c4
Fixed several typos
WhitWaldo Dec 11, 2024
8a24182
Corrected updated overload
WhitWaldo Dec 11, 2024
c58f7b9
Added best practices documentation for PubSub functionality
WhitWaldo Dec 11, 2024
12419a0
Updated contribution guide
WhitWaldo Dec 11, 2024
1b29528
Added current .NET version support to contributor guide
WhitWaldo Dec 11, 2024
34cdffd
Minor word addition
WhitWaldo Dec 11, 2024
89d0b95
Renamed for consistency
WhitWaldo Dec 11, 2024
49bfc9e
Tweaks to introduction text
WhitWaldo Dec 11, 2024
a494c08
Added Conversation usage documentation
WhitWaldo Dec 11, 2024
f2376e6
Updated to reflect updated extension method name following merge of #…
WhitWaldo Dec 11, 2024
0041807
Merge branch 'master' into daprclient-docs
WhitWaldo Dec 11, 2024
c21659b
Built out Jobs introduction
WhitWaldo Dec 11, 2024
73a3f28
Merge remote-tracking branch 'origin/daprclient-docs' into daprclient…
WhitWaldo Dec 11, 2024
2daa522
Updated support message for Dapr.Workflows
WhitWaldo Dec 11, 2024
535b264
Merge branch 'master' into daprclient-docs
WhitWaldo Dec 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,48 @@ description: Essential tips and advice for using DaprClient

A `DaprClient` holds access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar. `DaprClient` implements `IDisposable` to support eager cleanup of resources.

### Dependency Injection

The `AddDaprClient()` method will register the Dapr client with ASP.NET Core dependency injection. This method accepts an optional
options delegate for configuring the `DaprClient` and an `ServiceLifetime` argument, allowing you to specify a different lifetime
for the registered resources instead of the default `Singleton` value.

The following example assumes all default values are acceptable and is sufficient to register the `DaprClient`.

```csharp
services.AddDaprClient();
```

The optional configuration delegates are used to configure `DaprClient` by specifying options on the provided `DaprClientBuilder`
as in the following example:

```csharp
services.AddDaprClient(daprBuilder => {
daprBuilder.UseJsonSerializerOptions(new JsonSerializerOptions {
WriteIndented = true,
MaxDepth = 8
});
daprBuilder.UseTimeout(TimeSpan.FromSeconds(30));
});
```

The another optional configuration delegate overload provides access to both the `DaprClientBuilder` as well as an `IServiceProvider`
allowing for more advanced configurations that may require injecting services from the dependency injection container.

```csharp
services.AddSingleton<SampleService>();
services.AddDaprClient((serviceProvider, daprBuilder) => {
var sampleService = serviceProvider.GetRequiredService<SampleService>();
var timeoutValue = sampleService.TimeoutOptions;

daprBuilder.UseTimeout(timeoutValue);
});
```

### Manual Instantiation

Rather than using dependency injection, a `DaprClient` can also be built using the static client builder.

For best performance, create a single long-lived instance of `DaprClient` and provide access to that shared instance throughout your application. `DaprClient` instances are thread-safe and intended to be shared.

Avoid creating a `DaprClient` per-operation and disposing it when the operation is complete.
Expand All @@ -24,13 +66,41 @@ var daprClient = new DaprClientBuilder()
.Build();
```

The `DaprClientBuilder` contains settings for:
By default, the `DaprClientBuilder` will prioritize the following locations, in the following order, to source the configuration
values:

- The value provided to a method on the `DaprClientBuilder` (e.g. `UseTimeout(TimeSpan.FromSeconds(30))`)
- The value pulled from an optionally injected `IConfiguration` matching the name expected in the associated environment variable
- The value pulled from the associated environment variable
- Default values

### Configuring on `DaprClientBuilder`

The `DaprClientBuilder` contains the following methods to set configuration options:

- `UseHttpEndpoint(string)`: The HTTP endpoint of the Dapr sidecar
- `UseGrpcEndpoint(string)`: Sets the gRPC endpoint of the Dapr sidecar
- `UseGrpcChannelOptions(GrpcChannelOptions)`: Sets the gRPC channel options used to connect to the Dapr sidecar
- `UseHttpClientFactory(IHttpClientFactory)`: Configures the DaprClient to use a registered `IHttpClientFactory` when building `HttpClient` instances
- `UseJsonSerializationOptions(JsonSerializerOptions)`: Used to configure JSON serialization
- `UseDaprApiToken(string)`: Adds the provided token to every request to authenticate to the Dapr sidecar
- `UseTimeout(TimeSpan)`: Specifies a timeout value used by the `HttpClient` when communicating with the Dapr sidecar

- The HTTP endpoint of the Dapr sidecar
- The gRPC endpoint of the Dapr sidecar
- The `JsonSerializerOptions` object used to configure JSON serialization
- The `GrpcChannelOptions` object used to configure gRPC
- The API Token used to authenticate requests to the sidecar
### Configuring From `IConfiguration`
Rather than rely on sourcing configuration values directly from environment variables or because the values are sourced
from dependency injected services, another options is to make these values available on `IConfiguration`.

For example, you might be registering your application in a multi-tenant environment and need to prefix the environment
variables used. The following example shows how these values can be sourced from the environment variables to your
`IConfiguration` when their keys are prefixed with `test_`;

```csharp
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddEnvironmentVariables("test_"); //Retrieves all environment variables that start with "test_" and removes the prefix when sourced from IConfiguration
builder.Services.AddDaprClient();
```

### Configuring From Environment Variables

The SDK will read the following environment variables to configure the default values:

Expand All @@ -40,9 +110,14 @@ The SDK will read the following environment variables to configure the default v
- `DAPR_GRPC_PORT`: if `DAPR_GRPC_ENDPOINT` is not set, this is used to find the gRPC local endpoint of the Dapr sidecar
- `DAPR_API_TOKEN`: used to set the API Token

{{% alert title="Note" color="primary" %}}
If both `DAPR_HTTP_ENDPOINT` and `DAPR_HTTP_PORT` are specified, the port value from `DAPR_HTTP_PORT` will be ignored in favor of the port
implicitly or explicitly defined on `DAPR_HTTP_PORT`. The same is true of both `DAPR_GRPC_ENDPOINT` and `DAPR_GRPC_PORT`.
WhitWaldo marked this conversation as resolved.
Show resolved Hide resolved
{{% /alert %}}

### Configuring gRPC channel options

Dapr's use of `CancellationToken` for cancellation relies on the configuration of the gRPC channel options. If you need to configure these options yourself, make sure to enable the [ThrowOperationCanceledOnCancellation setting](https://grpc.github.io/grpc/csharp-dotnet/api/Grpc.Net.Client.GrpcChannelOptions.html#Grpc_Net_Client_GrpcChannelOptions_ThrowOperationCanceledOnCancellation).
Dapr's use of `CancellationToken` for cancellation relies on the configuration of the gRPC channel options and this is enabled by default. If you need to configure these options yourself, make sure to enable the [ThrowOperationCanceledOnCancellation setting](https://grpc.github.io/grpc/csharp-dotnet/api/Grpc.Net.Client.GrpcChannelOptions.html#Grpc_Net_Client_GrpcChannelOptions_ThrowOperationCanceledOnCancellation).

```C#
var daprClient = new DaprClientBuilder()
Expand Down
Loading