zcz527 / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

View injection doesn't work with Razor Layouts #349

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
1. Open solution in VS 2010
2. Run the app
3. It will blow up in _Layout.cshtml
4. Remove @Dependency.Now from _Layout.cshtml
5. Run the app again. Now it works and Dependency property in Index.cshtml is 
injected correctly.

It looks like Layout pages are created in a different way than regular pages 
and that's why dependencies are not injected. 

Original issue reported on code.google.com by pa...@pabich.eu on 24 Nov 2011 at 11:28

Attachments:

GoogleCodeExporter commented 8 years ago
I have same paroblem. ViewRegistrationSource.RegistrationsFor doesn't call for 
Layout type.

Original comment by achmedzh...@gmail.com on 30 Dec 2011 at 6:48

GoogleCodeExporter commented 8 years ago
Thanks for the report and repro Pawel (sorry about the extended delay in 
responding.) I'm not 100% certain that DI will be possible in this scenario, 
I'll take a look at it ASAP.

Original comment by nicholas...@gmail.com on 23 Jan 2012 at 5:21

GoogleCodeExporter commented 8 years ago
Any update on this?

Original comment by sebastia...@gmail.com on 6 Mar 2012 at 1:22

GoogleCodeExporter commented 8 years ago
Having the same issue. Any ideas?

Original comment by jaben.ca...@captiveaire.com on 27 Apr 2012 at 9:40

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 21 Sep 2012 at 4:34

GoogleCodeExporter commented 8 years ago
The only temporary solution I've found is to inject dependencies into a base 
controller and pass them into ViewData. Looking forward for the fix.

Original comment by alexey.s...@gmail.com on 27 Sep 2012 at 8:09

GoogleCodeExporter commented 8 years ago
It does not appear that this issue has been addressed in the new v3 autofac mvc 
integration.

Original comment by quen...@screenfeed.com on 13 Dec 2012 at 9:11

GoogleCodeExporter commented 8 years ago

Original comment by alex.meyergleaves on 14 Dec 2012 at 4:50

GoogleCodeExporter commented 8 years ago
There still seems to be no hook to achieve this. The layout view is not 
requested from the dependency resolver.

Original comment by alex.meyergleaves on 19 Dec 2012 at 12:33

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
In MVC5 there still doesn't appear to be a hook into the DependencyResolver to 
get Razor layouts.

From what I can tell, Razor goes like this:

* A System.Web.Mvc.RazorView gets created by the 
System.Web.Mvc.RazorViewEngine.CreateView().
* The view gets the Render method called on it, where the BuildManager reads 
the markup and compiles the type as necessary, then the ViewPageActivator 
passes the whole thing through the DependencyResolver. That all happens in the 
System.Web.Mvc.BuildManagerViewEngine class, from which all other view engines 
derive (ASPX and Razor).
* The RazorView has RenderView() called and gets a ViewContext, a TextWriter, 
and the actual instance of the view that was created by the ViewPageActivator.

Here's where it takes a turn.

* In RenderView, there's a mechanism that gets the start page 
("_ViewStart.cshtml") using the System.Web.WebPages.StartPage.GetStartPage 
method. To instantiate that start page, it uses an IVirtualPathFactory, which 
defaults to a wrapper around the BuildManager. This part of the page 
instantiation does not pass through DependencyResolver.
* The RazorView then calls ExecutePageHierarchy (from WebPageBase) and passes 
the start page in.
* Each start page then recursively executes in this fashion via the 
ExecutePageHierarchy method. Nowhere in here does it touch DependencyResolver.

Now, technically we could do some fanciness around that IVirtualPathFactory 
where we could try to insert ourselves in there, but there's no documentation 
around that except "This type/member supports the .NET Framework infrastructure 
and is not intended to be used directly from your code." It would be fairly 
complex and potentially risky if those aren't supposed to be used - we'd end up 
with stuff that could break on new releases and be hard to maintain.

Given this has been open for two years now and there's no real way to fix it, I 
vote we close the issue as WontFix and possibly update the wiki with this as a 
known issue.

Original comment by travis.illig on 3 Dec 2013 at 12:28

GoogleCodeExporter commented 8 years ago
Sounds good to me.

Original comment by pa...@pabich.eu on 3 Dec 2013 at 12:41

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 4 Dec 2013 at 11:31