Skip to content

Clarify SW103 migration with concrete examples and keyed-only registration guidance - #2

Merged
cliedeman merged 4 commits into
mainfrom
copilot/implement-analyzer-for-microsoft-extensions-azure
May 31, 2026
Merged

Clarify SW103 migration with concrete examples and keyed-only registration guidance#2
cliedeman merged 4 commits into
mainfrom
copilot/implement-analyzer-for-microsoft-extensions-azure

Conversation

Copilot AI commented May 31, 2026

Copy link
Copy Markdown
Contributor

This updates SW103 documentation to directly address migration ambiguity called out in review feedback. The rule guidance now shows exactly how to move from Microsoft.Extensions.Azure patterns to keyed DI and explicitly discourages fallback unkeyed registrations.

  • Migration examples added

    • Added concrete before/after registration examples:
      • AddAzureClients(...).WithName(...) usage
      • equivalent keyed DI registrations for Azure SDK clients
    • Added concrete before/after DI callsite examples:
      • IAzureClientFactory<T>.CreateClient("key")
      • [FromKeyedServices("key")] constructor injection
  • Keyed-only preference made explicit

    • Added a review-point note to prefer keyed singleton registrations for Azure SDK clients.
    • Explicitly calls out to avoid default unkeyed registration for the same client type.
// Preferred direction
builder.Services.AddKeyedSingleton<BlobServiceClient>("blob-storage", (sp, _) =>
    new BlobServiceClient(builder.Configuration.GetConnectionString("BlobStorage")));

public class BlobUploadService(
    [FromKeyedServices("blob-storage")] BlobServiceClient blobClient)
{
    private readonly BlobServiceClient _blobClient = blobClient;
}

```ini
[*.cs]
dotnet_diagnostic.SW103.severity = error
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some migration samples to the repo.

DI callsites and AddAzureClients config

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — added four concrete code samples to the Migration Notes section:

  1. Registration before — full AddAzureClients block with two named clients (blob-storage, order-queue) and configuration sections.
  2. Registration after — equivalent AddKeyedSingleton registrations using connection strings, no Microsoft.Extensions.Azure import.
  3. DI callsite before — constructor injection via IAzureClientFactory<BlobServiceClient> + CreateClient("blob-storage").
  4. DI callsite after — constructor injection using [FromKeyedServices("blob-storage")] attribute.

Copilot AI changed the title Add SW103 analyzer to ban Microsoft.Extensions.Azure and document keyed-service migration docs(SW103): add concrete AddAzureClients → keyed services migration samples May 31, 2026
Copilot AI requested a review from cliedeman May 31, 2026 11:09
Copilot AI changed the title docs(SW103): add concrete AddAzureClients → keyed services migration samples Clarify SW103 migration with concrete examples and keyed-only registration guidance May 31, 2026
@cliedeman
cliedeman marked this pull request as ready for review May 31, 2026 11:28
@cliedeman
cliedeman merged commit 58314f9 into main May 31, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants