Skip to content

Address Dapr feedback. #1038

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

Merged
merged 7 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
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
40 changes: 26 additions & 14 deletions docs/frameworks/dapr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Use Dapr with .NET Aspire
description: Learn how to use Dapr with .NET Aspire
ms.date: 05/20/2024
ms.date: 06/04/2024
ms.topic: overview
---

Expand All @@ -19,6 +19,13 @@ In addition to the prerequisites for .NET Aspire, you will need:

To install Dapr, see [Install the Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/). After installing the Dapr CLI, run the `dapr init` described in [Initialize Dapr in your local environment](https://docs.dapr.io/getting-started/install-dapr-selfhost/).

> [!IMPORTANT]
> If you attempt to run the app without the Dapr CLI, you'll receive the following error:
>
> ```plaintext
> Unable to locate the Dapr CLI.
> ```

## Get started

To get started you need to add the Dapr hosting package to your app host project by installing the [Aspire.Hosting.Dapr](https://www.nuget.org/packages/Aspire.Hosting.Dapr) NuGet package.
Expand All @@ -40,25 +47,22 @@ dotnet add package Aspire.Hosting.Dapr

For more information, see [dotnet add package](/dotnet/core/tools/dotnet-add-package) or [Manage package dependencies in .NET applications](/dotnet/core/tools/dependencies).

The Dapr resource is added to the .NET Aspire distributed application builder using the `AddDapr()` method.
An overload of the `AddDapr()` method that accepts Dapr options is available. For most applications, the default options will suffice.

:::code language="csharp" source="snippets/Dapr/Dapr.AppHost/Program.cs" range="4":::
## Add a Dapr sidecar

Dapr uses the [sidecar pattern](https://docs.dapr.io/concepts/dapr-services/sidecar/) to run alongside your application. The Dapr sidecar runs alongside your application as a lightweight, portable, and stateless HTTP server that listens for incoming HTTP requests from your application.
Dapr uses the [sidecar pattern](https://docs.dapr.io/concepts/dapr-services/sidecar/) to run alongside your application. The Dapr sidecar runs alongside your app as a lightweight, portable, and stateless HTTP server that listens for incoming HTTP requests from your app.

Add a sidecar to a .NET Aspire resource by using the `WithDaprSidecar(string appId)` method. The `appId` parameter is the unique identifier for the Dapr application.
To add a sidecar to a .NET Aspire resource, call the <xref:Aspire.Hosting.IDistributedApplicationResourceBuilderExtensions.WithDaprSidecar%2A> method on the desired resource. The `appId` parameter is the unique identifier for the Dapr application, but it's optional. If you don't provide an `appId`, the parent resource name is used instead.

:::code language="csharp" source="snippets/Dapr/Dapr.AppHost/Program.cs" range="18-21" highlight="21":::
:::code language="csharp" source="snippets/Dapr/Dapr.AppHost/Program.cs" range="1-7" highlight="7":::

The `WithDaprSidecar` method offers overloads to configure your Dapr sidecar options like app ID and ports. In the following example, the Dapr sidecar is configured with specific ports for GRPC, HTTP, metrics, and a specific App ID.
The `WithDaprSidecar` method offers overloads to configure your Dapr sidecar options like app ID and ports. In the following example, the Dapr sidecar is configured with specific ports for GRPC, HTTP, metrics, and a specific app ID.

:::code language="csharp" source="snippets/Dapr/Dapr.AppHost/Program.cs" range="6-16" highlight="1-7,11":::
:::code language="csharp" source="snippets/Dapr/Dapr.AppHost/Program.cs" range="9-19" highlight="1-6,11":::

Putting everything together, here's an example of a .NET Aspire app host project which includes:
Putting everything together, consider the following example of a .NET Aspire app host project that includes:

- A backend that declares a Dapr sidecar with specific ports and app ID.
- A frontend that declares a Dapr sidecar with a specific app ID and default ports.
- A backend API that declares a Dapr sidecar with defaults.
- A frontend that declares a Dapr sidecar with specific options, such as explict ports.

:::code language="csharp" source="snippets/Dapr/Dapr.AppHost/Program.cs":::

Expand Down Expand Up @@ -94,7 +98,7 @@ Once installed into an ASP.NET Core project, the SDK can be added to the service

An instance of `DaprClient` can now be injected into your services to interact with the Dapr sidecar through the Dapr SDK.

:::code language="csharp" source="snippets/Dapr/Dapr.Web/WeatherApiClient.cs" highlight="9-10":::
:::code language="csharp" source="snippets/Dapr/Dapr.Web/WeatherApiClient.cs" highlight="11-15":::

`InvokeMethodAsync` is a method that sends an HTTP request to the Dapr sidecar. It is a generic method that takes:

Expand Down Expand Up @@ -124,6 +128,14 @@ At first sight Dapr and .NET Aspire may look like they have overlapping function

.NET Aspire makes setting up and debugging Dapr applications easier by providing a straightforward API to configure Dapr sidecars, and by exposing the sidecars as resources in the dashboard.

### Explore Dapr components with .NET Aspire

Dapr provides many [built-in components](https://docs.dapr.io/concepts/components-concept), and when you use Dapr with .NET Aspire you can easily explore and configure these components. Don't confuse these components with .NET Aspire components. For example, consider the following:

- [Dapr—State stores](https://docs.dapr.io/concepts/components-concept/#state-stores): Call <xref:Aspire.Hosting.IDistributedApplicationBuilderExtensions.AddDaprStateStore%2A> to add a configured state store to your .NET Aspire app.
- [Dapr—Pub Sub](https://docs.dapr.io/concepts/components-concept/#pubsub-brokers): Call <xref:Aspire.Hosting.IDistributedApplicationBuilderExtensions.AddDaprPubSub%2A> to add a configured pub sub to your .NET Aspire app.
- Dapr—Components: Call <xref:Aspire.Hosting.IDistributedApplicationBuilderExtensions.AddDaprComponent%2A> to add a configured component to your .NET Aspire app.

## Next steps

> [!div class="nextstepaction"]
Expand Down
14 changes: 6 additions & 8 deletions docs/frameworks/snippets/Dapr/Dapr.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
using Aspire.Hosting.Dapr;

var builder = DistributedApplication.CreateBuilder(args);
builder.AddDapr();

DaprSidecarOptions sidecarOptions = new ()
var apiService = builder
.AddProject<Projects.Dapr_ApiService>("apiservice")
.WithDaprSidecar();

DaprSidecarOptions sidecarOptions = new()
{
AppId = "apiservice-dapr",
DaprGrpcPort = 50001,
DaprHttpPort = 3500,
MetricsPort = 9090
};

var apiService = builder
.AddProject<Projects.Dapr_ApiService>("apiservice")
.WithDaprSidecar(sidecarOptions);

builder.AddProject<Projects.Dapr_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(apiService)
.WithDaprSidecar("webfrontend-dapr");
.WithDaprSidecar(sidecarOptions);

builder.Build().Run();
1 change: 0 additions & 1 deletion docs/frameworks/snippets/Dapr/Dapr.Web/Dapr.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<PackageReference Include="Dapr.AspNetCore" Version="1.13.1" />
<PackageReference Include="Dapr.Client" Version="1.13.1" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion docs/frameworks/snippets/Dapr/Dapr.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

Expand Down
9 changes: 7 additions & 2 deletions docs/frameworks/snippets/Dapr/Dapr.Web/WeatherApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ namespace Dapr.Web;

public class WeatherApiClient(DaprClient client)
{
public async Task<WeatherForecast[]> GetWeatherAsync(int maxItems = 10, CancellationToken cancellationToken = default)
public async Task<WeatherForecast[]> GetWeatherAsync(
int maxItems = 10, CancellationToken cancellationToken = default)
{
List<WeatherForecast>? forecasts =
await client.InvokeMethodAsync<List<WeatherForecast>>(HttpMethod.Get, "apiservice-dapr", "/weatherforecast", cancellationToken); ;
await client.InvokeMethodAsync<List<WeatherForecast>>(
HttpMethod.Get,
"apiservice",
"/weatherforecast",
cancellationToken);

return forecasts?.ToArray() ?? [];
}
Expand Down
Loading