Windsor and MVC4

309 views
Skip to first unread message

Wayne Douglas

unread,
Oct 11, 2012, 4:43:50 AM10/11/12
to castle-pro...@googlegroups.com
Hi

Does anyone have any pointers to getting the container wired up in
MVC4? I also understand the process differs slightly for web and web
api?

Googling seems to come up with controller factories which to my
understanding are not necessary in mvc 4?

--
Cheers,

w://

Serdar B.

unread,
Oct 11, 2012, 4:49:17 AM10/11/12
to castle-pro...@googlegroups.com
It would be really nice if there is another "tobeseen" example for mvc4 :)

* including general exception interceptor 

Serdar
--
serdarb.com



--
Cheers,

w://

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


Krzysztof Kozmic

unread,
Oct 11, 2012, 4:50:40 AM10/11/12
to castle-pro...@googlegroups.com
I don't think it would be much different from MVC3...

-- 
Krzysztof Kozmic

Wayne Douglas

unread,
Oct 11, 2012, 4:52:06 AM10/11/12
to castle-pro...@googlegroups.com
so i'd still need a controller factory and all that?

I thought the whole point was that they had integrated it all better
so we won't need all that? With the whole DependencyResolver thing no?
Cheers,

w://

Wayne Douglas

unread,
Oct 11, 2012, 4:55:08 AM10/11/12
to castle-pro...@googlegroups.com
i guess dependancyresolver is not the way to go:

http://blog.ploeh.dk/2012/09/28/DependencyInjectionAndLifetimeManagementWithASPNETWebAPI.aspx

On Thu, Oct 11, 2012 at 9:52 AM, Wayne Douglas
--
Cheers,

w://

Merijn de Jonge

unread,
Oct 11, 2012, 4:57:23 AM10/11/12
to castle-pro...@googlegroups.com
This is what I did:

  • Create the following contorller factory:
 public class WindsorControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel _container;

        public WindsorControllerFactory(IKernel container)
        {
            _container = container;
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, System.Type controllerType)
        {
            if(controllerType == null)
            {
                throw new HttpException(404, "Controller not found.");
            }
            return _container.Resolve(controllerType) as IController;
        }
    }

  • Instruct MVC to make use of this controller factor by adding the following in the method Application_Start in the file Global.asax.cs I instructed:

    ControllerBuilder.Current.SetControllerFactory(Container.Resolve<IControllerFactory>());

I'm not sure if this is the best way to do it, but it works.


Kind regards,
Merijn

--

Dr. Merijn de Jonge
Chief Software Development


T +31 6 51442504 /
E merijn....@sorama.eu /
W www.sorama.eu /
Twitter logo @MerijnDeJonge /

Sorama op Twitter
Sorama Nieuwsbrief


Den Dolech 2, Gemini-Noord -1.65 /
5612 AZ / Eindhoven /
The Netherlands /
Chamber of Commerce Eindhoven nr. 50232053 /
Sorama logo

The information contained in this message is confidential and may be legally privileged. The message is intended solely for the addressee(s). If you are not the intended recipient, you are hereby notified that any use, dissemination, or reproduction is strictly prohibited and may be unlawful. If you are not the intended recipient, please contact the sender by return e-mail and destroy all copies of the original message.

Patrick Steele

unread,
Oct 11, 2012, 7:58:04 AM10/11/12
to castle-pro...@googlegroups.com
And don't forget Mark's follow-up post on doing that technique with Windsor:

http://blog.ploeh.dk/2012/10/03/DependencyInjectionInASPNETWebAPIWithCastleWindsor.aspx

---
Patrick Steele
http://weblogs.asp.net/psteele


On Thu, Oct 11, 2012 at 4:55 AM, Wayne Douglas
Reply all
Reply to author
Forward
0 new messages