Aspire Multilanguage with .NET Aspire Community Toolkit

tommasodotnet

Tommaso Stocchi

Posted on November 25, 2024

Aspire Multilanguage with .NET Aspire Community Toolkit

Not long ago, I posted about how to use .NET Aspire even when our distributed application involves different languages. In that article, I used the ability of .NET Aspire to support different languages natively or via containers to build a distributed calculator. In this article, I will show how this has improved thanks to the .NET Aspire Community Toolkit.

What is the .NET Aspire Community Toolkit?

It's an open-source repository where third parties can contribute their own integrations and extensions to the .NET Aspire ecosystem. Writing such integrations is easy, as demonstrated by Aaron Powell during dotnetconf.

The .NET Aspire Community Toolkit already offers all the integrations we need to move the distributed calculator away from local containers and run executables locally.

How to use the .NET Aspire Community Toolkit

To use the Golang integration, simply add the appropriate NuGet package to your project:

dotnet add package CommunityToolkit.Aspire.Hosting.Golang
Enter fullscreen mode Exit fullscreen mode

Next, use the following code to configure the Golang app:

var add = builder.AddGolangApp("addapp", "../../go-adder")
    .WithHttpEndpoint(env: "APP_PORT")
    .PublishAsDockerFile();
Enter fullscreen mode Exit fullscreen mode

This is a significant improvement over our previous setup:

var add = (builder.ExecutionContext.IsPublishMode
    ? builder.AddContainer("addapp", "acrt6xtihl2b3uxe.azurecr.io/addapp")
    : builder.AddContainer("addapp", "addapp"))
        .WithHttpEndpoint(targetPort: 6000, env: "APP_PORT", name: "http")
        .WithOtlpExporter()
        .WithEnvironment("OTEL_SERVICE_NAME", "addapp")
        .PublishAsContainer();
Enter fullscreen mode Exit fullscreen mode

Additionally, we can leverage the NodeJs Extensions to automatically install the necessary node packages for our NodeJs divider app and frontend:

var divide = builder.AddNodeApp(name: "divideapp", scriptPath: "app.js", workingDirectory: "../../node-divider")
    .WithNpmPackageInstallation()
    // other config

//...

builder.AddViteApp(name: "calculator-front-end", workingDirectory: "../../react-calculator", packageManager: "yarn")
    .WithYarnPackageInstallation()
    // other config
Enter fullscreen mode Exit fullscreen mode

The WithNpmPackageInstallation and WithYarnPackageInstallation methods will automatically install the necessary packages for the NodeJs and React apps, respectively.

More than just other languages

The .NET Aspire Community Toolkit is not just about languages. It also offers integrations with other services, such as Data API Builder. In this blog, I demonstrated how to use Data API Builder to create a chat-with-your-database application. Integrating DAB with .NET Aspire resulted in this:

var dabService = builder.AddContainer("dab", "mcr.microsoft.com/azure-databases/data-api-builder")
    .WithHttpEndpoint(targetPort: 5000, name: "http")
    .WithBindMount(@"D:\src\distributed-dab-sk\aw-data-api-builder\dab-config.json", "/App/dab-bm0/dab-config.json", true)
    .WithArgs("--ConfigFileName", "./dab-bm0/dab-config.json")
    .WithReference(sql)
    .WithOtlpExporter()
    .PublishAsContainer();
Enter fullscreen mode Exit fullscreen mode

Using the Data API Builder integration, we can simplify it to:

var dabService = builder.AddDataAPIBuilder("dab", @"..\aw-data-api-builder\dab-config.json");
Enter fullscreen mode Exit fullscreen mode

Useful Links

💖 💪 🙅 🚩
tommasodotnet
Tommaso Stocchi

Posted on November 25, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related