-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UseFriendlyObjectDisposedException option (#87)
- Loading branch information
Showing
10 changed files
with
148 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
# https://docs.codecov.com/docs/codecov-yaml | ||
comment: | ||
behavior: new |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
95 changes: 95 additions & 0 deletions
95
src/SteroidsDI.Tests/Cases/UseFriendlyObjectDisposedExceptionTests.cs
This file contains 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,95 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using Shouldly; | ||
using SteroidsDI.Core; | ||
|
||
namespace SteroidsDI.Tests.Cases; | ||
|
||
internal class UseFriendlyObjectDisposedExceptionTests | ||
{ | ||
[Test] | ||
public void ObjectDisposedException_UseFriendlyObjectDisposedException_Enabled() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddDefer() | ||
.AddScoped<ScopedService>() | ||
.AddSingleton<SingletonService>() | ||
.AddGenericScope<UseFriendlyObjectDisposedExceptionTests>(); | ||
|
||
using (var rootProvider = services.BuildServiceProvider()) | ||
{ | ||
var service = rootProvider.GetRequiredService<SingletonService>(); | ||
var outerScope = new Scoped<UseFriendlyObjectDisposedExceptionTests>(rootProvider.GetRequiredService<IScopeFactory>()); | ||
|
||
var cts1 = new CancellationTokenSource(); | ||
var cts2 = new CancellationTokenSource(); | ||
|
||
var t = Task.Run(() => | ||
{ | ||
cts1.Cancel(); | ||
cts2.Token.WaitHandle.WaitOne(); | ||
var ex = Should.Throw<ObjectDisposedException>(() => service.Scoped.Value); | ||
ex.Message.ShouldBe(@"ObjectDisposedException occurred while resolving service 'ScopedService' by scoped service provider obtained from 'SteroidsDI.GenericScopeProvider`1[[SteroidsDI.Tests.Cases.UseFriendlyObjectDisposedExceptionTests, SteroidsDI.Tests, Version=1.0.3.0, Culture=neutral, PublicKeyToken=null]]'. | ||
Most likely this happened because the scope and scoped provider were disposed BEFORE the actual completion of the user code. | ||
Often, this is due to a forgotten 'await' operator somewhere in the user code. Make sure you await all the created tasks correctly. | ||
You see this message because 'ServiceProviderAdvancedOptions.UseFriendlyObjectDisposedException' option is enabled."); | ||
ex.InnerException.ShouldBeOfType<ObjectDisposedException>().Message.ShouldBe(@"Cannot access a disposed object. | ||
Object name: 'IServiceProvider'."); | ||
}); | ||
|
||
cts1.Token.WaitHandle.WaitOne(); | ||
outerScope.Dispose(); | ||
cts2.Cancel(); | ||
t.Wait(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void ObjectDisposedException_UseFriendlyObjectDisposedException_Disabled() | ||
{ | ||
var services = new ServiceCollection() | ||
.Configure<ServiceProviderAdvancedOptions>(opt => opt.UseFriendlyObjectDisposedException = false) | ||
.AddDefer() | ||
.AddScoped<ScopedService>() | ||
.AddSingleton<SingletonService>() | ||
.AddGenericScope<UseFriendlyObjectDisposedExceptionTests>(); | ||
|
||
using (var rootProvider = services.BuildServiceProvider()) | ||
{ | ||
var service = rootProvider.GetRequiredService<SingletonService>(); | ||
var outerScope = new Scoped<UseFriendlyObjectDisposedExceptionTests>(rootProvider.GetRequiredService<IScopeFactory>()); | ||
|
||
var cts1 = new CancellationTokenSource(); | ||
var cts2 = new CancellationTokenSource(); | ||
|
||
var t = Task.Run(() => | ||
{ | ||
cts1.Cancel(); | ||
cts2.Token.WaitHandle.WaitOne(); | ||
var ex = Should.Throw<ObjectDisposedException>(() => service.Scoped.Value); | ||
ex.Message.ShouldBe(@"Cannot access a disposed object. | ||
Object name: 'IServiceProvider'."); | ||
ex.InnerException.ShouldBeNull(); | ||
}); | ||
|
||
cts1.Token.WaitHandle.WaitOne(); | ||
outerScope.Dispose(); | ||
cts2.Cancel(); | ||
t.Wait(); | ||
} | ||
} | ||
|
||
private sealed class ScopedService { } | ||
|
||
private class SingletonService | ||
{ | ||
public SingletonService(IDefer<ScopedService> scoped) | ||
{ | ||
Scoped = scoped; | ||
} | ||
|
||
public IDefer<ScopedService> Scoped { get; } | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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