Choosing infrastructure for a cloud application usually involves more than selecting a VM with enough CPU and memory.
A .NET application may need a particular amount of memory, network capacity, storage performance, processor architecture, or accelerator support. When several workloads are involved, maintaining these decisions manually can become difficult.
An AI infrastructure optimization workflow can help a .NET application evaluate workload requirements and identify suitable Azure infrastructure options.
The important part is to treat optimization as a recommendation layer rather than allowing an AI result to directly create production infrastructure.
This article shows how to structure a .NET application around an Azure AI infrastructure optimization workflow, how to model workload requirements, how to keep SDK integration isolated, and how to validate recommendations before deployment.
Where Infrastructure Optimization Fits in a .NET Application
A typical application deployment pipeline may look like this:
Developer Request
|
v
Workload Requirements
|
v
.NET Optimization Service
|
v
Infrastructure Recommendation
|
v
Policy Validation
|
v
Deployment Automation
|
v
Azure ResourcesThe .NET application does not need to contain hard-coded rules for every VM family.
Instead, it can collect requirements and pass them to the infrastructure optimization layer.
For example:
Application:
.NET API
CPU:
8 cores
Memory:
32 GB
Storage:
Premium SSD
Region:
East US
GPU:
Not requiredThe optimization layer evaluates those requirements and returns suitable infrastructure candidates.
Why Use a .NET Service Layer
A common mistake is placing SDK-specific calls directly inside controllers, background jobs, or deployment handlers.
For example, this creates tight coupling:
public async Task<IActionResult> Recommend()
{
// SDK calls
// Authentication
// Requirement mapping
// Recommendation processing
// Policy validation
return Ok();
}A better design separates responsibilities:
Controller
|
v
IVmOptimizationService
|
v
Azure Optimization Client
|
v
Azure InfrastructureThis gives the application a clear boundary between business logic and Azure SDK integration.
Define Workload Requirements
Start by defining a model that describes the workload rather than a specific VM.
public sealed class WorkloadRequirements
{
public int MinimumCpuCores { get; init; }
public int MinimumMemoryGb { get; init; }
public string Region { get; init; } = string.Empty;
public string Architecture { get; init; } = "x64";
public bool RequiresGpu { get; init; }
public int ExpectedNetworkMbps { get; init; }
public string StorageType { get; init; } = string.Empty;
}This model can be extended as the application grows.
The important principle is that the input describes what the workload needs.
It should not contain fields such as:
public string VmSize { get; init; }That would mix requirements with the recommendation.
Create a Recommendation Service
Create an abstraction around the optimization workflow:
public interface IInfrastructureOptimizationService
{
Task<VmRecommendation> RecommendAsync(
WorkloadRequirements requirements,
CancellationToken cancellationToken);
}The rest of the application can depend on this interface instead of the underlying SDK.
A recommendation model can contain the result and explanation:
public sealed class VmRecommendation
{
public string VmSize { get; init; } = string.Empty;
public string Region { get; init; } = string.Empty;
public string Reason { get; init; } = string.Empty;
public IReadOnlyList<string> Constraints { get; init; }
= Array.Empty<string>();
}This separation makes the recommendation process easier to test.
Register the Service With Dependency Injection
A standard ASP.NET Core application can register the service through dependency injection.
builder.Services.AddScoped<IInfrastructureOptimizationService,
InfrastructureOptimizationService>();The implementation can then contain the Azure-specific integration.
public sealed class InfrastructureOptimizationService
: IInfrastructureOptimizationService
{
public async Task<VmRecommendation> RecommendAsync(
WorkloadRequirements requirements,
CancellationToken cancellationToken)
{
// Map requirements to the Azure optimization workflow.
// Call the configured Azure SDK/client.
// Convert the result into the application's model.
await Task.CompletedTask;
return new VmRecommendation
{
VmSize = "candidate-vm-size",
Region = requirements.Region,
Reason = "Candidate satisfies the supplied workload requirements."
};
}
}The example intentionally keeps the SDK-specific operation behind the service boundary.
The exact SDK client, package name, namespaces, and API methods should match the Azure SDK version used by the project.
Authentication Should Stay Outside Business Logic
Azure SDK integrations should normally use Microsoft Entra-based authentication rather than embedding credentials in application code.
A common .NET pattern is:
using Azure.Identity;
var credential = new DefaultAzureCredential();The application can then use the credential with the appropriate Azure client.
The benefit of this approach is that the same application can use different supported authentication sources depending on its environment.
For local development, developers can authenticate through their Azure development identity.
In Azure-hosted environments, managed identity can be used where supported.
Do not put client secrets directly into:
appsettings.jsonor source code.
Add an API for VM Recommendations
Once the service is registered, an API endpoint can expose the recommendation workflow.
For example:
app.MapPost("/infrastructure/recommend",
async (
WorkloadRequirements requirements,
IInfrastructureOptimizationService service,
CancellationToken cancellationToken) =>
{
var recommendation =
await service.RecommendAsync(
requirements,
cancellationToken);
return Results.Ok(recommendation);
});A client could submit:
{
"minimumCpuCores": 8,
"minimumMemoryGb": 32,
"region": "East US",
"architecture": "x64",
"requiresGpu": false,
"expectedNetworkMbps": 1000,
"storageType": "Premium SSD"
}The API should return a recommendation that the deployment workflow can evaluate.
Do Not Deploy Immediately After a Recommendation
The most important production rule is to introduce a validation stage.
Requirements
|
v
Optimization
|
v
Recommendation
|
v
Policy Validation
|
v
Deployment Approval
|
v
Azure DeploymentFor example, an organization may allow only:
Regions:
East US
West Europe
Architectures:
x64
Maximum VM cost:
Defined by platform policy
GPU:
Allowed only for approved workloadsThe optimization system may recommend a technically valid VM that violates one of these policies.
The policy layer should reject it before deployment.
Validate the Region
Region should be part of the original workload requirements.
var requirements = new WorkloadRequirements
{
MinimumCpuCores = 8,
MinimumMemoryGb = 32,
Region = "East US",
Architecture = "x64"
};A recommendation that ignores region can be unusable in production.
Region can affect:
Join the conversation! Your thoughts help the community grow.