Welcome to Manicprogrammer Sign in | Join | Help

ASP.Net MVC, Windsor and some Singleton trouble

What?

I just managed to create Stormwind.Accuracy demo app using ASP.Net MVC (more on that later).

The first thing I setup in any project I do is an IoC container. I won't do code without it unless it's a REALLY simple spike. Period.

So, there I go setting my MVC app in a way that it uses Windsor to get the controllers and everything under them. Fine, works like a charm. In theory.

I have an url that looks like: http://localhost:9999/Posts/View/1, where 1 is the id of the post (yeah blog engine, not very original, I know!).

It works fine. Then I tried http://localhost:9999/Posts/View/2. I got the same post as before. Wait, there's something wrong! Let's debug that!

I have this action in PostController:

public ActionResult View(int id){
//Retrieves post and returns View();
}

To my complete and utter surprise, id was being passed as 1 in both requests. "Probably some IIS issue. Let's restart IIS." Nothing.

Ok, probably the routes are wrong. Let's check the Route Values Dictionary:

? this.ControllerContext.RouteData.Values.Keys
Count = 3
    [0]: "id"
    [1]: "controller"
    [2]: "action"
? this.ControllerContext.RouteData.Values.Values
Count = 3
    [0]: "2"
    [1]: "Post"
    [2]: "View"

What??? So it is getting the right value for Id from the route! At this point my colleagues started noticing a big "?" in my forehead.

The Light

That's the point where you keep scratching your head and then it hits you.

How does the MVC framework invoke actions. My guess was that it stores in a table somewhere the controller, action and parameters to invoke with. The issue here is that it stores this data in the actual controller instance.

Usually that's not a problem, since ASP.Net MVC will create a new instance every time it needs to call an action.

Remember that I'm using Windsor to resolve it, though. By default, the lifestyle of my controllers are singleton. I won't discuss here whether they should or shouldn't be singletons, even though I think there is no good reason why they can't be.

Conclusion

Changing the lifestyle to Transient or PerWebRequest did the trick.

Bottomline: If using Windsor to resolve your controllers, STAY AWAY FROM SINGLETONS!

Published Tuesday, November 25, 2008 11:50 AM by heynemann

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: ASP.Net MVC, Windsor and some Singleton trouble

Tuesday, November 25, 2008 12:02 PM by Mike

From MonoRail documentation for WindsorIntegration:

Container set up

You must register a facility named RailsFacility. It ensures that the lifestyle of each controller is set to Transient - as using the default, singleton, would be a terrible mistake - and register each component/controller on the ControllerTree.

# re: ASP.Net MVC, Windsor and some Singleton trouble

Tuesday, November 25, 2008 1:20 PM by heynemann

Thanks a lot Mike! I knew it was something silly.

Anyway, just registering the controllers as transient works. What's the need for registering them in the controller tree?

When I click the Windsor Integration in the MVC contrib website I get a blank page... :( Any ideas on why the page on codeplex wiki is not done?

Thanks again,

Bernardo Heynemann

# ASP.NET MVC Archived Blog Posts, Page 1

Wednesday, November 26, 2008 9:54 AM by ASP.NET MVC Archived Blog Posts, Page 1

# re: ASP.Net MVC, Windsor and some Singleton trouble

Monday, December 01, 2008 4:27 AM by Alex Scordellis

Hey Bernardo, it isn't just you!

http://stackoverflow.com/questions/238460

# re: ASP.Net MVC, Windsor and some Singleton trouble

Monday, December 01, 2008 9:57 AM by heynemann

True! :)

It's weird that controllers can't be singletons. I actually think it's a good idea to have them as transient as to not encourage people to put state in them, but not being able to use them as singleton and getting weird unexpected behavior is a little too much.

Thanks Alex,

Bernardo

# re: ASP.Net MVC, Windsor and some Singleton trouble

Thursday, December 11, 2008 3:50 PM by Jeff Jin

I had similar issue with my Login page, every time I log out and try to login again, it actually passes previous viewdata to Login action. I registered my controllers as Singleton too. Thanks, your suggestion was a good hint to me.  

# re: ASP.Net MVC, Windsor and some Singleton trouble

Wednesday, March 18, 2009 4:09 PM by Kelly

Controllers can't be singletons because their context changes per request.


Enter the text you see in the image:

Leave a Comment

(required) 
required 
(required)