-
Hello @dadhi, Consider a singleton service that failed to initialize when it was first resolved.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
hi @yallie |
Beta Was this translation helpful? Give feedback.
-
I have a test with two services using a faulty singleton dependency, one with Lazy wrapper, and another is non-lazy. Both services have a dependency which throws an InvalidOperationException on its first instantiation: public class ServiceWithLazyImport
{
[Import]
private Lazy<IDependency> LazyDependency { get; set; }
public void DoWork() => LazyDependency.Value.DoWork();
}
public class ServiceWithNormalImport
{
[Import]
private IDependency Dependency { get; set; }
public void DoWork() => Dependency.DoWork();
} Test code is as follows: // dependency initialization failed (test passes)
var s1 = container.Resolve<ServiceWithLazyImport>();
Assert.That(() => s1.DoWork(), Throws.TypeOf<InvalidOperationException>());
// should it fail or should it work?
var s2 = container.Resolve<ServiceWithNormalImport>();
s2.DoWork();
// should it fail or should it work?
var s3 = container.Resolve<ServiceWithLazyImport>();
s3.DoWork(); The code above behaves like this. s2 throws an unexpected exception in RuntimeType.CheckValue: s3 throws an EntryPointNotFoundException: Also, I've seen s3 rethrowing (kind of expected) InvalidOperationException but with a weird very long call stack, but couldn't replicate it in my test yet. |
Beta Was this translation helpful? Give feedback.
-
@yallie Ok, I have added the test to the |
Beta Was this translation helpful? Give feedback.
hi @yallie
Good question, I think it was asked long ago but I forgot the conclusion.
Definitely its expression won't be cached, I remember the code for preventing it. So you may modify registrations later to resolve the problem. At least it was the intent.