.NET Aspire Rust hosting
Includes: Hosting integration not Client integration
Note
This integration is part of the .NET Aspire Community Toolkit and isn't officially supported by the .NET Aspire team.
Rust is a general-purpose programming language emphasizing performance, type safety, and concurrency. It enforces memory safety, meaning that all references point to valid memory. The .NET Aspire Rust hosting integration allows you to host Rust applications in your .NET Aspire app host project, and provide it to other resources in your application.
Hosting integration
The Rust hosting integration models a Rust application as the Aspire.Hosting.ApplicationModel.RustAppExecutableResource
type. To access this type and APIs that allow you to add it to your app host project, install the 📦 CommunityToolkit.Aspire.Hosting.Rust NuGet package in the app host project.
This integration expects that the Rust programming language has already been installed on the host machine and the Rust package manager cargo
is available in the system path.
dotnet add package CommunityToolkit.Aspire.Hosting.Rust
For more information, see dotnet add package or Manage package dependencies in .NET applications.
Add a Rust resource
In the Program.cs file of your app host project, call the Aspire.Hosting.RustAppHostingExtension.AddRustApp
on the builder
instance to add a Rust application resource as shown in the following example:
var builder = DistributedApplication.CreateBuilder(args);
var rust = builder.AddRustApp("rust-app", workingDirectory: "../rust-service")
.WithHttpEndpoint(env: "PORT");
var exampleProject = builder.AddProject<Projects.ExampleProject>()
.WithReference(rust);
// After adding all resources, run the app...
The working directory of the application should be the root of Rust application directory.
Also you can customize running behavior by passing args parameter to the AddRustApp
method.
var rust = builder.AddRustApp("rust-app", workingDirectory: "../rust-service", args: ["--locked"])
.WithHttpEndpoint(env: "PORT");
The Rust application can be added as a reference to other resources in the app host project.