Closed AdamBowler closed 3 years ago
Did you configure dynamic assembly compilation in Simple Injector using container.Options.EnableDynamicAssemblyCompilation()
? If so, you might want to turn it off and try again.
Are you using Auto-Registration by calling AppDomain.CurrentDomain.GetAssemblies()
in your application startup? That might be the problem. Try using BuildManager.GetReferencedAssemblies().Cast<Assembly>()
instead, because assemblies are only loaded on demand when IIS recycles. Also see this.
We aren't using container.Options.EnableDynamicAssemblyCompilation()
and neither are we using AppDomain.CurrentDomain.GetAssemblies()
We found no correlation between IIS recycles causing the ActivationException in our investigation, we also checked to see if the error occurred after a period of no traffic but it was occurring randomly during high traffic periods as well, so I believe that title change may be misleading
Can you show the code that registers IReceiptStorage
?
container.Register<IReceiptStorageTypeFactory, ReceiptStorageTypeFactory>(Lifestyle.Singleton);
container.Register<IReceiptStorageFactory, SelenityReceiptStorageFactory>(Lifestyle.Singleton);
container.Register(
() => container.GetInstance<IReceiptStorageFactory>()
.Create(),
Lifestyle.Singleton
);
These bottom registration is for IReceiptStorage
The exception message presented in your initial post, is that the complete exception message, or did you cut some details from that message? If so, can you post those details?
That's the full exception message just with some folder structure removed and a couple of classes renamed
The registrations you showed are not dynamic, and considering the given exception message, this means that the Container
instance that IReceiptStorage
is resolved from is:
Container
instance throws a message containing the text "Please note that the container instance you are resolving from contains no registrations.")IReceiptStorage
.Considering the fact that a Simple Injector Container gets locked once in use and can't be changed nor cleared, my assumption is that somewhere in your code base, an accidental new Container instance is created that doesn't get initialized with IReceiptStorage
.
You can try the following in order to get more information when the exception happens:
Register
on a locked container, the container throws an exception containing the stack trace of the location where that instance was locked. You might be able to use that information to diagnose the problem.Container
instances, each instance will have its own hash code, which you can retrieve using GetHashCode()
. Might be useful to log the Container
's hashcode at startup and log the hash code of the Container instance throwing the exception.Container.GetRegistrations
to get a list of all registrations that are in the container. You can log that information upon failure in order to get sense of what's the difference between the 'real' container and the 'phantom' container instance.But there might be another reason for this failure. Although Simple Injector is designed for concurrent use, it is still possible that you stumbled upon an unfortunate concurrency bug within Simple Injector code base, but this is impossible for me to analyze without more information from your part. If the currency bug is the cause, you will see the problem to likely go away by calling container.Verify()
upon startup. Calling Verify()
pre-compiles all object graphs in a single thread. This might cause the application to startup more slowly, but if there is an bug caused by some parallel operation, the call to Verify()
will likely solve this. Downside of you calling Verify()
to solve the problem is that in that case we won't find the cause of the concurrency bug, and we won't be able to solve it.
Hi @AdamBowler, any luck in finding the source of the problem?
Describe the bug
We're experiencing an issue in a WebAPI project where our DI will work for long periods of time on live but at random intervals, and it could be weeks between occurrences, we start getting activation exceptions. If we recycle the affected app pool then it goes away and the DI continues as normal.
Expected behavior
The DI should work constantly.
Actual behavior
The DI works for a period of time and then the following errors are thrown repeatedly until we recycle the affected app pool,
To Reproduce
We have been unable to find what causes the issue to occur but it only occurs in our WebAPI project and nowhere else across our stack and product range.
Additional context
Add any other context about the problem here. e.g.: