Skip to content

Commit

Permalink
Fixed #29
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Dec 9, 2018
1 parent f3ae8e2 commit d950c53
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ internal static void Register(this IUnityContainer container,
serviceDescriptor.GetLifetime(lifetime),
new InjectionFactory(scope =>
{
var serviceProvider = serviceDescriptor.Lifetime == ServiceLifetime.Scoped
? scope.Resolve<IServiceProvider>()
: container.Resolve<IServiceProvider>();
var instance = serviceDescriptor.ImplementationFactory(serviceProvider);
var instance = serviceDescriptor.ImplementationFactory(scope.Resolve<IServiceProvider>());
return instance;
}));
}
Expand Down
121 changes: 121 additions & 0 deletions tests/GitHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using System.Reflection;
using Xunit;

namespace Unity.Microsoft.DependencyInjection.Tests
{
public class GitHubIssues
{
public interface IScopedService
{
IServiceProvider ServiceProvider { get; }
}

public class ScopedService : IScopedService
{
public ScopedService(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public IServiceProvider ServiceProvider { get; }
}

private interface ITransientService
{
IScopedService ScopedService { get; }
}

public class TransientService : ITransientService
{
public TransientService(IScopedService scopedService)
{
ScopedService = scopedService;
}
public IScopedService ScopedService { get; }
}

[Fact]
public void Issue_29_Type()
{
var sc = new ServiceCollection();

sc.AddTransient<ITransientService, TransientService>();
sc.AddScoped<IScopedService, ScopedService>();
var sp = sc.BuildServiceProvider(new UnityContainer());

Assert.Same(sp, sp.GetRequiredService<IScopedService>().ServiceProvider);
Assert.Same(sp, sp.GetRequiredService<ITransientService>().ScopedService.ServiceProvider);
var ssf = sp.GetRequiredService<IServiceScopeFactory>();
using (var scope = ssf.CreateScope())
{
var scopedSp = scope.ServiceProvider;
var scoped = scopedSp.GetRequiredService<IScopedService>();
var transient = scopedSp.GetRequiredService<ITransientService>();

Assert.Same(scopedSp, scoped.ServiceProvider);
Assert.Same(scopedSp, transient.ScopedService.ServiceProvider);
}
}


[Fact]
public void Issue_29_Factory()
{
var sc = new ServiceCollection();

sc.AddTransient<ITransientService>(o => new TransientService(o.GetRequiredService<IScopedService>()));
sc.AddScoped<IScopedService, ScopedService>();
var sp = sc.BuildServiceProvider(new UnityContainer());

Assert.Same(sp, sp.GetRequiredService<IScopedService>().ServiceProvider);
Assert.Same(sp, sp.GetRequiredService<ITransientService>().ScopedService.ServiceProvider);
var ssf = sp.GetRequiredService<IServiceScopeFactory>();
using (var scope = ssf.CreateScope())
{
var scopedSp = scope.ServiceProvider;
var scoped = scopedSp.GetRequiredService<IScopedService>();
var transient = scopedSp.GetRequiredService<ITransientService>();

Assert.Same(scopedSp, scoped.ServiceProvider);
Assert.Same(scopedSp, transient.ScopedService.ServiceProvider);
}
}


[Fact]
public void Test2_failing()
{

var serviceCollection = new ServiceCollection();


serviceCollection.AddHttpClient();

IUnityContainer container = new UnityContainer().CreateChildContainer();

var factory = new ServiceProviderFactory(c => c.UnityContainer = container);

var sp = factory.CreateServiceProvider(serviceCollection);
var scopeFactory = sp.GetRequiredService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var httpFactory = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>();

var sp1 = httpFactory.GetType().GetField("_services", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(httpFactory) as IServiceProvider;
var c1 = sp1.GetType().GetField("_container", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sp1);
Assert.NotNull(c1); // "In scope"

}

{
var httpFactory = sp.GetRequiredService<IHttpClientFactory>();
var sp1 = httpFactory.GetType().GetField("_services", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(httpFactory) as IServiceProvider;
var c1 = sp1.GetType().GetField("_container", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sp1);
Assert.NotNull(c1); // "after scope"
}

}
}
}
10 changes: 10 additions & 0 deletions tests/Microsoft.DependencyInjection.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
Expand All @@ -27,4 +28,13 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.DependencyInjection">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.dependencyinjection\2.1.1\lib\netcoreapp2.0\Microsoft.Extensions.DependencyInjection.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Http">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.http\2.1.1\lib\netstandard2.0\Microsoft.Extensions.Http.dll</HintPath>
</Reference>
</ItemGroup>

</Project>

0 comments on commit d950c53

Please sign in to comment.