Open ldubrois opened 3 years ago
@ldubrois Are you looking to submit a PR? It seems like you did most of the work already? Assuming the existing tests passed.
There are a couple of things not ideal with the way we're using IServiceProvider. The big thing is the RazorLightEngineBuilder isn't composable via "services".
I'm not used to github to create PR but I can try 😀
The only think that concerns me is the possible side effect of removing the scopes. May be some people take advantage of it (but don't really see how because the scope is completely dissociated from the render of the view itself).
From what I've seen, in classic MVC website, the scope is for the entire request, so if we want to reproduce it, the scope should be created on the pre-render and disposed after the render.
I don't know well RazorLight, but is there something similar to ASP.NET pipelines (collection of middleware) in which we could create the scope and dispose it ?
@ldubrois The tutorial is here: https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request
There are two reasons IServiceProvider might cause problems:
@ldubrois Can you answer whether you were using RazorLightEngineBuilder or not? Thanks. It would help save time writing a regression test to cover this scenario.
@ldubrois Any update?
I believe i've run into the same/related problem.
I'm currently using 2.0.0-rc2 and I believe this code is problematic.
The linked code from the PropertyInjector creates a scope and sets the @inject
fields inside the template but then disposes the service scope which will dispose any IDisposable services it created.
In my case I'm attempting to use asp.net's UserManager which is an IDisposable. The UserManager is correctly injected but because it's disposed before the template is actually rendered it'll throw an error:
Object name: 'UserManager`1'.
Stack Trace:
at Microsoft.AspNetCore.Identity.UserManager`1.ThrowIfDisposed()
Describe the bug We are using RazorLight in a project, with PropertyInjector as a PreRenderCallback. We are injecting a service in a Razor view (using the form @inject) ; this service requires a IServiceProvider. When using the IServiceProvider in the class, we a have a Object Disposed exception.
To Reproduce Use injection with a class requiring an IServiceProvider and try to invoke it.
Expected behavior The exception should not occurs.
Information (please complete the following information):
Additional context It seems from the documentation provided by Microsoft, that a IServiceProvider lifetime is "scope", and this is why we are getting an exception.
Replacing the PropertyInjector by a very simple code solve the problem (by removing the scopes)
Thanks,