-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...njection.Specification.Tests/GHIssue435_hangfire_use_dryioc_report_ContainerIsDisposed.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,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.DependencyInjection.Specification; | ||
|
||
namespace DryIoc.Microsoft.DependencyInjection.Specification.Tests | ||
{ | ||
[TestFixture] | ||
public class GHIssue435_hangfire_use_dryioc_report_ContainerIsDisposed : DependencyInjectionSpecificationTests | ||
{ | ||
protected override IServiceProvider CreateServiceProvider(IServiceCollection services) => DryIocAdapter.Create(services); | ||
|
||
[Test, Ignore("failing")] | ||
public void SingletonFactory_Test() | ||
{ | ||
var collection = new ServiceCollection(); | ||
|
||
collection.AddSingleton<ISingletonFactory>(r => new SingletonFactory(r)); | ||
collection.AddTransient<IFakeService, FakeService>(); | ||
|
||
|
||
var serviceProvider = CreateServiceProvider(collection); | ||
var scopeFactory = serviceProvider.GetService<IServiceScopeFactory>(); | ||
ISingletonFactory singletonFactory; | ||
using (var scope = scopeFactory.CreateScope()) | ||
{ | ||
singletonFactory = (ISingletonFactory)scope.ServiceProvider.GetService(typeof(ISingletonFactory)); | ||
} | ||
|
||
var fakeService = singletonFactory.GetService<IFakeService>(); | ||
Assert.NotNull(fakeService); | ||
} | ||
|
||
public interface IFakeService { } | ||
public interface IFakeScopedService { } | ||
public interface IFakeSingletonService { } | ||
public class FakeService : IFakeService, IFakeScopedService, IFakeSingletonService, IDisposable | ||
{ | ||
public bool Disposed { get; private set; } | ||
|
||
public void Dispose() | ||
{ | ||
if (Disposed) | ||
throw new ObjectDisposedException("FakeService"); | ||
Disposed = true; | ||
} | ||
} | ||
|
||
public interface ISingletonFactory | ||
{ | ||
T GetService<T>(); | ||
} | ||
|
||
public class SingletonFactory : ISingletonFactory | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
public SingletonFactory(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public T GetService<T>() | ||
{ | ||
return (T)_serviceProvider.GetService(typeof(T)); | ||
} | ||
} | ||
} | ||
} |