-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add more details and source code examples of DI anti-patterns #21273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
44c47fd
wip bits...mid-change, context switch
IEvangelist 1fe7bdb
Updates to additional/contextual samples of anti-patterns
IEvangelist 362533b
Added access-by-line entries, and sorted 'em
IEvangelist 7fdb251
Minor updates
IEvangelist b41eacb
Added a sentence describing each disposable instance's intended lifetime
IEvangelist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,24 @@ | ||
| snippets/configuration/dependency-injection/Program.cs: ~/docs/core/extensions/dependency-injection.md | ||
| snippets/configuration/console-di-ienumerable/Program.cs: ~/docs/core/extensions/dependency-injection.md | ||
| snippets/configuration/app-lifetime/ExampleHostedService.cs: ~/docs/core/extensions/generic-host.md | ||
| snippets/configuration/app-lifetime/Program.cs: ~/docs/core/extensions/generic-host.md | ||
| snippets/configuration/console-custom-logging/ColorConsoleLogger.cs: ~/docs/core/extensions/custom-logging-provider.md | ||
| snippets/configuration/console-di-disposable/Program.cs: ~/docs/core/extensions/dependency-injection-guidelines.md | ||
| snippets/configuration/console-di-ienumerable/ExampleService.cs: ~/docs/core/extensions/dependency-injection.md | ||
| snippets/configuration/console-di-ienumerable/Program.cs: ~/docs/core/extensions/dependency-injection.md | ||
| snippets/configuration/console-di/Program.cs: ~/docs/core/extensions/dependency-injection-usage.md | ||
| snippets/configuration/console-host/Program.cs: ~/docs/core/extensions/generic-host.md | ||
| snippets/configuration/console-ini/Program.cs: ~/docs/core/extensions/configuration-providers.md | ||
| snippets/configuration/console-json/appsettings.json: ~/docs/core/extensions/options.md | ||
| snippets/configuration/console-json/Program.cs: ~/docs/core/extensions/configuration-providers.md | ||
| snippets/configuration/console-json/Program.cs: ~/docs/core/extensions/options.md | ||
| snippets/configuration/console-json/TransientFaultHandlingOptions.cs: ~/docs/core/extensions/options.md | ||
| snippets/configuration/console-xml/Program.cs: ~/docs/core/extensions/configuration-providers.md | ||
| snippets/configuration/console-ini/Program.cs: ~/docs/core/extensions/configuration-providers.md | ||
| snippets/configuration/console/Program.cs: ~/docs/core/extensions/configuration.md | ||
| snippets/logging/console-formatter-simple/Program.cs: ~/docs/core/extensions/custom-log-formatter.md | ||
| snippets/configuration/console/Program.cs: ~/docs/core/extensions/logging-providers.md | ||
| snippets/configuration/custom-provider/Program.cs: ~/docs/core/extensions/custom-configuration-provider.md | ||
| snippets/configuration/console-custom-logging/ColorConsoleLogger.cs: ~/docs/core/extensions/custom-logging-provider.md | ||
| snippets/configuration/console-di-disposable/Program.cs: ~/docs/core/extensions/dependency-injection-guidelines.md | ||
| snippets/configuration/console-di/Program.cs: ~/docs/core/extensions/dependency-injection-usage.md | ||
| snippets/configuration/console-host/Program.cs: ~/docs/core/extensions/generic-host.md | ||
| snippets/configuration/app-lifetime/Program.cs: ~/docs/core/extensions/generic-host.md | ||
| snippets/configuration/app-lifetime/ExampleHostedService.cs: ~/docs/core/extensions/generic-host.md | ||
| snippets/configuration/dependency-injection/Program.cs: ~/docs/core/extensions/dependency-injection.md | ||
| snippets/configuration/di-anti-patterns/Foo.cs: ~/docs/core/extensions/dependency-injection-guidelines.md | ||
| snippets/configuration/di-anti-patterns/Program.cs: ~/docs/core/extensions/dependency-injection-guidelines.md | ||
| snippets/configuration/worker-service-options/Worker.cs: ~/docs/core/extensions/high-performance-logging.md | ||
| snippets/configuration/console/Program.cs: ~/docs/core/extensions/logging-providers.md | ||
| snippets/configuration/worker-service/appsettings.IncludeScopes.json: ~/docs/core/extensions/logging.md | ||
| snippets/configuration/worker-service/Worker.cs: ~/docs/core/extensions/logging.md | ||
| snippets/configuration/console-json/appsettings.json: ~/docs/core/extensions/options.md | ||
| snippets/configuration/console-json/TransientFaultHandlingOptions.cs: ~/docs/core/extensions/options.md | ||
| snippets/configuration/console-json/Program.cs: ~/docs/core/extensions/options.md | ||
| snippets/logging/console-formatter-simple/Program.cs: ~/docs/core/extensions/custom-log-formatter.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
docs/core/extensions/snippets/configuration/di-anti-patterns/Bar.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace DependencyInjection.AntiPatterns | ||
| { | ||
| public class Bar | ||
| { | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
docs/core/extensions/snippets/configuration/di-anti-patterns/ExampleDisposable.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using System; | ||
|
|
||
| namespace DependencyInjection.AntiPatterns | ||
| { | ||
| public class ExampleDisposable : IDisposable | ||
| { | ||
| public void Dispose() => | ||
| Console.WriteLine($"Disposed: {GetHashCode(),12}"); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
docs/core/extensions/snippets/configuration/di-anti-patterns/Foo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace DependencyInjection.AntiPatterns | ||
| { | ||
| public class Foo | ||
| { | ||
| public Foo(Bar bar) | ||
| { | ||
| } | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
docs/core/extensions/snippets/configuration/di-anti-patterns/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace DependencyInjection.AntiPatterns | ||
| { | ||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| // Uncomment individual methods below to see anti-patterns in action: | ||
| // TransientDisposablesWithoutDispose(); | ||
| // DeadLockWithFactories(); | ||
| // CaptiveDependency(); | ||
| // ScopedServiceBecomesSingleton(); | ||
| } | ||
|
|
||
| static void TransientDisposablesWithoutDispose() | ||
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddTransient<ExampleDisposable>(); | ||
| ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
|
|
||
| for (int i = 0; i < 1000; ++ i) | ||
| { | ||
| _ = serviceProvider.GetRequiredService<ExampleDisposable>(); | ||
| } | ||
|
|
||
| // serviceProvider.Dispose(); | ||
| } | ||
|
|
||
| static void DeadLockWithAsyncFactory() | ||
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<Foo>(implementationFactory: provider => | ||
| { | ||
| Bar bar = GetBarAsync(provider).Result; | ||
| return new Foo(bar); | ||
| }); | ||
|
|
||
| services.AddSingleton<Bar>(); | ||
|
|
||
| using ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
| _ = serviceProvider.GetRequiredService<Foo>(); | ||
| } | ||
|
|
||
| static async Task<Bar> GetBarAsync(IServiceProvider serviceProvider) | ||
| { | ||
| // Emulate asynchronous work operation | ||
| await Task.Delay(1000); | ||
|
|
||
| return serviceProvider.GetRequiredService<Bar>(); | ||
| } | ||
|
|
||
| static void CaptiveDependency() | ||
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<Foo>(); | ||
| services.AddScoped<Bar>(); | ||
|
|
||
| using ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
| // Enable scope validation | ||
| // using ServiceProvider serviceProvider = services.BuildServiceProvider(validateScopes: true); | ||
|
|
||
| _ = serviceProvider.GetRequiredService<Foo>(); | ||
| } | ||
|
|
||
| static void ScopedServiceBecomesSingleton() | ||
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddScoped<Bar>(); | ||
|
|
||
| using ServiceProvider serviceProvider = services.BuildServiceProvider(validateScopes: true); | ||
| using (IServiceScope scope = serviceProvider.CreateScope()) | ||
| { | ||
| // Correctly scoped resolution | ||
| Bar correct = scope.ServiceProvider.GetRequiredService<Bar>(); | ||
| } | ||
|
|
||
| // Not within a scope, becomes a singleton | ||
| Bar avoid = serviceProvider.GetRequiredService<Bar>(); | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
docs/core/extensions/snippets/configuration/di-anti-patterns/di-anti-patterns.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| <RootNamespace>DependencyInjection.AntiPatterns</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0-rc.2.20475.5" /> | ||
| </ItemGroup> | ||
| </Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.