Skip to content

Commit

Permalink
fixing the #536 for the singleton dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Oct 29, 2022
1 parent e1cb5ba commit 517b8be
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/DryIoc/Container.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ partial class Container
{
partial void GetLastGeneratedFactoryID(ref int lastFactoryID)
{
lastFactoryID = 583; // generated: equals to the last used Factory.FactoryID
lastFactoryID = 597; // generated: equals to the last used Factory.FactoryID
}

partial void ResolveGenerated(ref object service, Type serviceType)
Expand All @@ -62,7 +62,7 @@ partial void ResolveGenerated(ref object service,
requiredServiceType == null &&
Equals(preRequestParent, Request.Empty.Push(
typeof(IService),
578,
592,
typeof(MyService),
Reuse.Transient,
RequestFlags.IsResolutionCall)))
Expand Down Expand Up @@ -92,7 +92,7 @@ internal static object Get_IService_0(IResolverContext r) =>
null,
Request.Empty.Push(
typeof(IService),
578,
592,
typeof(MyService),
Reuse.Transient,
RequestFlags.IsResolutionCall|RequestFlags.StopRecursiveDependencyCheck),
Expand All @@ -104,7 +104,7 @@ internal static object Get_IService_0(IResolverContext r) =>
null,
Request.Empty.Push(
typeof(IService),
578,
592,
typeof(MyService),
Reuse.Transient,
RequestFlags.IsResolutionCall|RequestFlags.StopRecursiveDependencyCheck),
Expand Down
8 changes: 6 additions & 2 deletions src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ private object ResolveAndCache(int serviceTypeHash, Type serviceType, IfUnresolv
{
var value = constExpr.Value;
if (value is ScopedItemException it)
it.TryReThrow();
it.ReThrow();
if (factory.CanCache)
TryCacheDefaultFactory<FactoryDelegate>(serviceTypeHash, serviceType, value.ToFactoryDelegate);
return value;
Expand Down Expand Up @@ -3079,7 +3079,11 @@ public static bool TryInterpret(IResolverContext r, Expression expr,
{
var argExpr = newExpr.GetArgument(i);
if (argExpr is ConstantExpression ac)
{
if (ac.Value is ScopedItemException it)
it.ReThrow();
args[i] = ac.Value;
}
else if (!TryInterpret(r, argExpr, paramExprs, paramValues, parentArgs, out args[i]))
return false;
}
Expand Down Expand Up @@ -12713,7 +12717,7 @@ internal sealed class ScopedItemException
{
public readonly Exception Ex;
public ScopedItemException(Exception ex) => Ex = ex;
internal void TryReThrow() => throw Ex.TryRethrowWithPreservedStackTrace();
internal void ReThrow() => throw Ex.TryRethrowWithPreservedStackTrace();
}

/// <summary>Lazy object storage that will create object with provided factory on first access,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class GHIssue536_DryIoc_Exception_in_a_Constructor_of_a_Dependency_does_t
{
public int Run()
{
Test_root_singleton_should_rethrow_the_exception();
// Test_root_singleton_should_rethrow_the_exception();
Test_dep_singleton_should_rethrow_the_exception();
return 2;
}

Expand All @@ -26,6 +27,27 @@ public void Test_root_singleton_should_rethrow_the_exception()
container.Resolve<IInterfaceA>());
}

[Test]
public void Test_dep_singleton_should_rethrow_the_exception()
{
var container = new Container();

container.Register<B>();
container.Register<IInterfaceA, ClassA>(Reuse.Singleton);

Assert.Throws<ArgumentException>(() =>
container.Resolve<B>());

Assert.Throws<ArgumentException>(() =>
container.Resolve<B>());
}

public class B
{
public readonly IInterfaceA IntA;
public B(IInterfaceA a) => IntA = a;
}

public interface IInterfaceA { }

public class ClassA : IInterfaceA
Expand Down
4 changes: 2 additions & 2 deletions test/DryIoc.TestRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class Program
{
public static void Main()
{
RunAllTests();
// RunAllTests();

// new GHIssue536_DryIoc_Exception_in_a_Constructor_of_a_Dependency_does_tunnel_through_Resolve_call().Run();
new GHIssue536_DryIoc_Exception_in_a_Constructor_of_a_Dependency_does_tunnel_through_Resolve_call().Run();

// new GHIssue535_Property_injection_does_not_work_when_appending_implementation_for_multiple_registration().Run();
// new GHIssue532_WithUseInterpretation_still_use_DynamicMethod_and_ILEmit().Run();
Expand Down

0 comments on commit 517b8be

Please sign in to comment.