Documentation section: Apphosts
Go AppHosts
Use AspireC4 from an Aspire 17 Go AppHost (apphost.go): the Aspire CLI generates a Go binding for
AspireC4 from aspire.config.json, so a Go AppHost gets the same live LikeC4 architecture diagram,
resource details, relationships and optional Entity Framework Core ER diagram as C#. How the languages
compare is in AppHost languages.
aspire.config.json
Section titled “aspire.config.json”Declare AspireC4, and the integrations it draws, in packages:
{ "appHost": { "path": "apphost.go", "language": "go" }, "sdk": { "version": "17.0.0" }, "packages": { "Aspire.Hosting.JavaScript": "17.0.0", "AspireC4.Hosting": "<version>" }}Replace <version> with the current AspireC4.Hosting release on NuGet.
The repository sample points it at ../../src/src/AspireC4/AspireC4.csproj instead, so it exercises the
local source. aspire restore writes the binding to .aspire/modules/, which the sample’s go.mod
maps to the apphost/modules/aspire package with a replace directive.
apphost.go
Section titled “apphost.go”builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
builder. AddAspireC4(aspire.AddAspireC4Configure(func(opts aspire.AspireC4DiagramOptions) { opts.WithTitle("My distributed application").WithViewTitle("Architecture") })). WithServerLikeC4Details(&aspire.WithServerLikeC4DetailsOptions{ Configure: func(opts aspire.LikeC4NodeDetailsAnnotation) { opts.WithLabel("Architecture diagram") }, })
cache := builder.AddRedis("cache")
builder.AddNodeApp("api", "../api", "index.ts"). WithLikeC4Details(&aspire.WithLikeC4DetailsOptions{ Configure: func(opts aspire.LikeC4NodeDetailsAnnotation) { opts.WithLabel("API").WithTechnology("Node.js").WithTag("backend") }, }). // Draws the relationship and passes the connection string through Aspire's WithReference. WithLikeC4ConnectionReference(cache, &aspire.WithLikeC4ConnectionReferenceOptions{ Configure: func(opts aspire.LikeC4RelationshipDetailsAnnotation) { opts.WithLabel("Caches sessions").WithKind("RESP") }, })
app, err := builder.Build()if err != nil { log.Fatal(aspire.FormatError(err))}if err := app.Run(); err != nil { log.Fatal(aspire.FormatError(err))}Options are passed either as a single-field value, such as aspire.AddAspireC4Configure(...), or as a
pointer to the method’s options struct, such as &aspire.WithLikeC4DetailsOptions{...}; optional
fields take pointers (Title: &title). Errors are carried along the builder chain and reported by
Build().
Entity Framework Core diagrams
Section titled “Entity Framework Core diagrams”Declare AspireC4.Hosting.EntityFrameworkCore, and Aspire.Hosting.Dotnet to add the .NET project
that owns the DbContext, then run aspire restore:
title, label := "Order schema", "Orders"
c4 := builder.AddAspireC4()c4.WithEntityFrameworkDiagram(&aspire.WithEntityFrameworkDiagramOptions{ Configure: func(opts aspire.AspireC4EfDiagramOptions) { opts.SetTitle(&title) },})
builder. AddDotnetProject("orders", "../Orders.Api/Orders.Api.csproj"). WithEntityFrameworkModel(&aspire.WithEntityFrameworkModelOptions{Label: &label})The ER diagram is built purely from .NET Entity Framework Core code, so it only ever draws .NET projects, never a Go service. See Entity Framework Core ER Diagrams.
Run the Go sample
Section titled “Run the Go sample”The sample at samples/go-app-host builds the same graph as every other sample AppHost (see
The samples share one contract). Prerequisites are Docker,
the Aspire CLI, Go, and npm for the sample Node.js service.
just go-restore # aspire restore: generates .aspire/modulesjust go-run # aspire runjust go-lint # gofmt and go vetGenerated output lands in .aspirec4/ beside apphost.go, as beside a C# AppHost project; see
AppHost languages.