Skip to content
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

CastRootScopeFrame expression should be casting scope.Root, instead o… #386

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading