|
| 1 | +--- |
| 2 | +title: Use Orleans with .NET Aspire |
| 3 | +description: Learn how to use Orleans with .NET Aspire |
| 4 | +ms.date: 05/3/2024 |
| 5 | +ms.topic: overview |
| 6 | +--- |
| 7 | + |
| 8 | +# Use Orleans with .NET Aspire |
| 9 | + |
| 10 | +Orleans and .NET Aspire are built for each other. .NET Aspire's application model lets you describe the services, databases, and other resources/infrastructure in your app and how they relate. Orleans' provides a straightforward way to build distributed applications which are elastically scalable and fault-tolerant. .NET Aspire is used to configure and orchestrate Orleans and its dependencies, such as, by providing Orleans with database cluster membership and storage. |
| 11 | + |
| 12 | +Orleans is represented as a resource in .NET Aspire. The Orleans resource includes configuration which your service needs to operate, such as cluster membership providers and storage providers. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- .NET 8.0 SDK or later |
| 17 | +- .NET Aspire workload |
| 18 | +- Orleans version 8.1.0 or later |
| 19 | + |
| 20 | +For more information, see [.NET Aspire setup and tooling](../fundamentals/setup-tooling.md). |
| 21 | + |
| 22 | +## Get started |
| 23 | + |
| 24 | +To get started you need to add the Orleans hosting package to your app host project by installing the [Aspire.Hosting.Orleans](https://www.nuget.org/packages/Aspire.Hosting.Orleans) NuGet package. |
| 25 | + |
| 26 | +### [.NET CLI](#tab/dotnet-cli) |
| 27 | + |
| 28 | +```dotnetcli |
| 29 | +dotnet add package Aspire.Hosting.Orleans --prerelease |
| 30 | +``` |
| 31 | + |
| 32 | +### [PackageReference](#tab/package-reference) |
| 33 | + |
| 34 | +```xml |
| 35 | +<PackageReference Include="Aspire.Hosting.Orleans" |
| 36 | + Version="[SelectVersion]" /> |
| 37 | +``` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +For more information, see [dotnet add package](/dotnet/core/tools/dotnet-add-package) or [Manage package dependencies in .NET applications](/dotnet/core/tools/dependencies). |
| 42 | + |
| 43 | +The Orleans resource is added to the .NET Aspire distributed application builder using the `AddOrleans(string name)` method, which returns an Orleans resource builder. |
| 44 | +The name provided to the Orleans resource is for diagnostic purposes. For most applications, a value of `"default"` will suffice. |
| 45 | + |
| 46 | +:::code language="csharp" source="snippets/Orleans/OrleansAppHost/Program.cs" range="12"::: |
| 47 | + |
| 48 | +The Orleans resource builder offers methods to configure your Orleans resource. In the following example, the Orleans resource is configured with clustering and grain storage using the `WithClustering` and `WithGrainStorage` methods respectively: |
| 49 | + |
| 50 | +:::code language="csharp" source="snippets/Orleans/OrleansAppHost/Program.cs" range="3-14" highlight="4-5,11-12"::: |
| 51 | + |
| 52 | +This tells Orleans that any service referencing it will also need to reference the `clusteringTable` resource. |
| 53 | + |
| 54 | +To participate in an Orleans cluster, reference the Orleans resource from your service project, either using `WithReference(orleans)` to participate as an Orleans server, or `WithReference(orleans.AsClient())` to participate as a client. When you reference the Orleans resource from your service, those resources will also be referenced: |
| 55 | + |
| 56 | +:::code language="csharp" source="snippets/Orleans/OrleansAppHost/Program.cs" range="16-22"::: |
| 57 | + |
| 58 | +Putting that all together, here is an example of an Aspire AppHost project which includes: |
| 59 | + |
| 60 | +- An Orleans resource with clustering and storage. |
| 61 | +- An Orleans server project, 'OrleansServer'. |
| 62 | +- An Orleans client project, 'OrleansClient'. |
| 63 | + |
| 64 | +:::code language="csharp" source="snippets/Orleans/OrleansAppHost/Program.cs" ::: |
| 65 | + |
| 66 | +To consume the .NET Aspire Orleans resource from an Orleans server project, there are a few steps: |
| 67 | + |
| 68 | +1. Add the relevant .NET Aspire components. In this example, you're using _Aspire.Azure.Data.Tables_ and _Aspire.Azure.Storage.Blobs_. |
| 69 | +2. Add the Orleans provider packages for those .NET Aspire components. In this example, you're using _Microsoft.Orleans.Persistence.AzureStorage_ and _Microsoft.Orleans.Clustering.AzureStorage_. |
| 70 | +3. Add Orleans to the host application builder. |
| 71 | + |
| 72 | +In the _Program.cs_ file of your Orleans server project, you must configure the .NET Aspire components you are using and add Orleans to the host builder. Note that the names provided must match the names used in the .NET Aspire app host project: "clustering" for the clustering provider, and "grain-state" for the grain state storage provider: |
| 73 | + |
| 74 | +:::code language="csharp" source="snippets/Orleans/OrleansServer/Program.cs" ::: |
| 75 | + |
| 76 | +You do similarly in the _OrleansClient_ project, adding the .NET Aspire resources which your project needs to join the Orleans cluster as a client, and configuring the host builder to add an Orleans client: |
| 77 | + |
| 78 | +:::code language="csharp" source="snippets/Orleans/OrleansClient/Program.cs" ::: |
| 79 | + |
| 80 | +## Enabling OpenTelemetry |
| 81 | + |
| 82 | +By convention, .NET Aspire application include a project for defining default configuration and behavior for your service. This project is called the _service default_ project and templates create it with a name ending in _ServiceDefaults_. |
| 83 | +To configure Orleans for OpenTelemetry in .NET Aspire, you will need to apply configuration to your service defaults project following the [Orleans observability](/dotnet/orleans/host/monitoring/) guide. |
| 84 | +In short, you will need to modify the `ConfigureOpenTelemetry` method to add the Orleans _meters_ and _tracing_ instruments. The following code snippet shows the _Extensions.cs_ file from a service defaults project which has been modified to include metrics and traces from Orleans. |
| 85 | + |
| 86 | +:::code language="csharp" source="snippets/Orleans/OrleansServiceDefaults/Extensions.cs" range="40-68" highlight="15,19-20"::: |
| 87 | + |
| 88 | +## Supported providers |
| 89 | + |
| 90 | +The Orleans Aspire integration supports a limited subset of Orleans providers today: |
| 91 | + |
| 92 | +- Clustering: |
| 93 | + - Redis |
| 94 | + - Azure Storage Tables |
| 95 | +- Persistence: |
| 96 | + - Redis |
| 97 | + - Azure Storage Tables |
| 98 | + - Azure Storage Blobs |
| 99 | +- Reminders: |
| 100 | + - Redis |
| 101 | + - Azure Storage Tables |
| 102 | +- Grain directory: |
| 103 | + - Redis |
| 104 | + - Azure Storage Tables |
| 105 | + |
| 106 | +Streaming providers are not supported as of Orleans version 8.1.0. |
0 commit comments