Перейти к содержимому

Rust integration

Это содержимое пока не доступно на вашем языке.

⭐ Community Toolkit Rust logo

The Aspire Rust hosting integration enables you to run Rust applications alongside your Aspire projects in the Aspire app host.

To get started with the Aspire Rust hosting integration, install the CommunityToolkit.Aspire.Hosting.Rust NuGet package in the app host project.

Aspire CLI — Добавить пакет CommunityToolkit.Aspire.Hosting.Rust
aspire add communitytoolkit-rust

Aspire CLI интерактивен; выберите подходящий результат поиска при запросе:

Aspire CLI — Пример вывода
Select an integration to add:
> communitytoolkit-rust (CommunityToolkit.Aspire.Hosting.Rust)
> Other results listed as selectable options...

To add a Rust application to your app host, use the AddRustApp extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var rustApp = builder.AddRustApp(
name: "rust-api",
workingDirectory: "../rust-app")
.WithHttpEndpoint(port: 8080, env: "PORT");
builder.AddProject<Projects.ExampleProject>()
.WithReference(rustApp);
// After adding all resources, run the app...

The AddRustApp method requires:

  • name: The name of the resource in the Aspire dashboard
  • workingDirectory: The path to the directory containing your Rust application

Rust applications typically use environment variables to configure the port they listen on. Use WithHttpEndpoint to configure the port and set the PORT environment variable:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var rustApp = builder.AddRustApp("rust-api", "../rust-app")
.WithHttpEndpoint(port: 8080, env: "PORT");
// After adding all resources, run the app...

Your Rust application can read the PORT environment variable to determine which port to listen on:

use std::env;
use actix_web::{web, App, HttpServer, Responder};
async fn greet() -> impl Responder {
"Hello from Rust!"
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string());
let addr = format!("0.0.0.0:{}", port);
println!("Server listening on {}", addr);
HttpServer::new(|| {
App::new()
.route("/", web::get().to(greet))
})
.bind(addr)?
.run()
.await
}

You can pass custom arguments to cargo run using the WithArgs method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var rustApp = builder.AddRustApp("rust-api", "../rust-app")
.WithArgs("--release")
.WithHttpEndpoint(port: 8080, env: "PORT");
// After adding all resources, run the app...

The workingDirectory parameter specifies where the Rust application is located. The Aspire app host will run cargo run in this directory to start your Rust application.

Вопросы & ответыСотрудничатьСообществоОбсуждатьСмотреть