zcz527 / autofac

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

Using ViewRegistrationSource causes recursive views to break #356

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Register ViewRegistrationSource
2. Cause direct or indirect (finite!) recursion in your views (e.g. view A 
conditionally calling Html.RenderPartial("A"))
3. An error will occur during rendering, e.g. "Stack empty." in 
WebPageBase.WriteLiteral.

What is the expected output? What do you see instead?
Views should render properly even when called recursively.

What version of Autofac are you using? On what version of .NET/Silverlight?
Autofac 2.5.2.830, .NET 4, MVC 3

Please provide any additional information below.
This error is caused by the fact that ViewRegistrationSource registers views as 
InstancePerHttpRequest. When a view is called recursively Autofac will return 
the same instance each time the view type is requested. Without going into too 
much detail, this leaves the view in a bad state during back-tracking from 
deeper partial views.

The solution should be simple: make ViewRegistrationSource register views as 
InstancePerDependency.

I have attached a project that shows the bug. If you compile and run it, it 
works fine. Uncomment line 46 in global.asax.cs (which registers the 
ViewRegistrationSource) to cause the error to show up.

Original issue reported on code.google.com by m...@andreloker.de on 20 Feb 2012 at 5:47

Attachments:

GoogleCodeExporter commented 8 years ago
I have pushed the fix up to the repository ready for the next release. Thanks 
for attaching the sample project. It made is clear what the bug was and allowed 
me to quickly test the fix. The fix was exactly as you described.

Original comment by alex.meyergleaves on 26 Feb 2012 at 2:46