Removing Secret Strings from Your .NET Aspire Project
If you’re building modern .NET Aspire apps, you’re probably familiar with how service names and resource identifiers are often passed around as string literals, things like "apiservice"
or "storage"
. But these magic strings can lead to headaches: typos, duplication, poor discoverability, and painful refactoring.
Thankfully, there’s a clean and safe way to centralize and manage these identifiers using a shared constants class, removing “secret strings” from your Aspire project. 💥
✨ Credit Where Credit’s Due
First, big thanks to Jeff Fritz for showcasing this pattern in his excellent video: “Organize your .NET Aspire Projects like a Pro!”. I also saw Jeff share this approach during his excellent talk at StirTrek earlier this year. This post builds directly on that idea and shows how you can apply it to both project references and cloud resources like Azure Storage.
🎯 The Problem: Magic Strings in AppHost.cs
Here’s what the typical Aspire AppHost.cs might look like:
|
|
Or when registering Azure Storage resources:
|
|
These string literals ("apiservice"
, "storage"
, "blobs"
, etc.) are fragile and hard to reuse safely across your codebase.
One minor typo and things don’t work and you’re left scratching your head for a while, only to later realize you can’t spell. Been there. 😒
🔧 Step 1: Create a New Shared Project
Create a new class library in your solution:
|
|
This project will house your service and resource names.
💻 Step 2: Define Constants in Shared/Services.cs
Create a static class in the Shared project:
|
|
Now you have a single source of truth for all your Aspire identifiers. 🎯
🔗 Step 3: Reference Shared from AppHost
Edit the AppHost.csproj
to include a reference to Shared
, but be sure to opt out of Aspire’s resource detection using the IsAspireProjectResource="false"
attribute:
|
|
This ensures Aspire doesn’t treat Shared
as a resource provider.
🔨 Step 4: Update AppHost.cs to Use Constants
Now refactor your code to use those constants:
⏳ Old
|
|
✅ New
|
|
And for Azure Storage:
⏳ Old
|
|
✅ New
|
|
Cleaner. Safer. Easier to manage. 💡
🎈 Step 5: Reference Shared in Client Projects (Optional)
If you have other Aspire projects (e.g., frontend apps or background workers) that also use these service names, add a project reference to Shared
there as well.
And the code then becomes:
|
|
Now your whole solution can rely on centralized, strongly-typed service identifiers.
🔔 Bonus: Shorten Project Names
In addition to removing magic strings, you can simplify long project names in Aspire by explicitly specifying the AspireProjectMetadataTypeName
metadata type name in your project reference.
⏳ Old
|
|
This results in awkward long names like:
|
|
✅ New
|
|
Now you can shorten the reference in AppHost.cs to:
|
|
This makes your code cleaner and more readable, especially in solutions with deeply nested or namespaced projects.
💞 Benefits Recap
✅ Eliminate fragile magic strings
✅ Enable compiler support and refactoring
✅ Improve discoverability of services and resources
✅ Keep your solution organized and maintainable
📺 Learn More
👉 Watch Jeff Fritz’s video for a great walkthrough of this and other .NET Aspire tips.
🙌 Final Thoughts
Removing magic strings is a small refactor that pays big dividends in maintainability and clarity. Give it a try in your .NET Aspire project—and say goodbye to those “secret strings” once and for all. 🔓