Skip to content

Commit

Permalink
CastRootScopeFrame expression should be casting scope.Root, instead o…
Browse files Browse the repository at this point in the history
…f scope to the target type
  • Loading branch information
cheng93 authored and jeremydmiller committed Sep 7, 2023
1 parent d5db3ac commit 4088321
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/Lamar.Testing/Bugs/Bug_385_ServiceScopeFactory_is_not_root.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Lamar.Testing.Bugs;

using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;

public class Bug_385_ServiceScopeFactory_is_not_root
{
[Fact]
public void Test()
{
var container = Container.For(x =>
{
x.Use<TestService>();
});

var serviceScopeFactory = container.GetInstance<IServiceScopeFactory>();
using var scope = serviceScopeFactory.CreateScope();
var testService = scope.ServiceProvider.GetRequiredService<TestService>();

testService.ServiceScopeFactory.ShouldBe(serviceScopeFactory);
}

public class TestService
{
public IServiceScopeFactory ServiceScopeFactory { get; }

public TestService(IServiceScopeFactory serviceScopeFactory)
{
ServiceScopeFactory = serviceScopeFactory;
}
}
}
8 changes: 6 additions & 2 deletions src/Lamar/IoC/Resolvers/ScopeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ public CastRootScopeFrame(Type interfaceType)
public void WriteExpressions(LambdaDefinition definition)
{
var variableExpr = definition.RegisterExpression(Variable);
definition.Body.Add(Expression.Assign(variableExpr,
Expression.Convert(definition.ExpressionFor(_scope), Variable.VariableType)));

var root = Expression.Property(definition.ExpressionFor(_scope), nameof(Scope.Root));
var convert = Expression.Convert(root, Variable.VariableType);
var assign = Expression.Assign(variableExpr, convert);

definition.Body.Add(assign);
}

public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
Expand Down

0 comments on commit 4088321

Please sign in to comment.