Ninject 2 with MVC 2 - probably newbie problem

42 views
Skip to first unread message

Havremunken

unread,
Mar 25, 2010, 3:01:53 PM3/25/10
to ninject
Hi there,

I just did File -> New Project last night on a new project. Ah, the
smell of green fields.

I am using the just released ASP.NET MVC 2 (i.e. no preview or release
candidate, the real thing), and thought I'd get off to a good start
using Ninject 2 (also released version) with the MVC extensions. I
downloaded the MVC extensions project, opened it in VS2008Sp1, built
it in release mode, and then went into the mvc2\build\release folder
and copied Ninject.dll and Ninject.Web.Mvc.dll from there to the
Libraries folder on my project (so that I can lug them around in
source control and always have the right version everywhere). I didn't
include the corresponding .xml files - should I? Do they just provide
intellisense, or some other function? Not a big deal I believe.

Anyhoo, I followed the most up-to-date advice I could find; I
referenced the DLLs in my MVC2 project, then went to work on
Global.asax.cs. First I made it inherit from NinjectHttpApplication. I
removed the Application_Start() method, and overrode
OnApplicationStarted() instead. Here is that method:

protected override void OnApplicationStarted()
{
base.OnApplicationStarted();

AreaRegistration.RegisterAllAreas();

RegisterRoutes(RouteTable.Routes);

//
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}

And I also followed the advise of VS and implemented the CreateKernel
method:

protected override Ninject.IKernel CreateKernel()
{
//
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
return new StandardKernel();
}

That is all. No other modifications to the project.

You'll notice that the RegisterAllControllersIn() method is commented
out in two places above. I've figured I can run it in three different
combinations, all with their funky side effects;

1. Running it like above.

I am then presented with the standard "Welcome to ASP.NET MVC" page in
all its' glory. However, after this page is displayed correctly in the
browser, VS shows me an exception that was thrown. It throws in
NinjectControllerFactory.GetControllerInstance(), which was called
with a NULL value in the controllerType parameter. Notice that this
happens after the /Home page is rendered - I have no idea why it is
called again, and by using breakpoints I've already determined that
GetControllerInstance() has been successfully called for the
HomeController. Why this new call with controllerType as null? I
really have no idea. Pressing F5 at this time takes me back to the
browser, no complaints there.

2. Uncommenting the RegisterAllControllersIn() method in
CreateKernel()

This is where stuff is really starting to get funky. Now I get a 404
error. Some times I have also gotten an ArgumentNullException on the
RegisterAllControllersIn() line, but that is pretty rare, and I have
not been able to reproduce it.

3. Uncommenting the RegisterAllControllers() method in
OnApplicationStarted()

(And putting the comment back on the one in CreateKernel())

Results in behavior that seems exactly like that in point 1.

So to keep from going on forever - is there an exact step-by-step
guide on how to set up an MVC 2 project with Ninject 2 (both non-beta
release versions) to get the controllers provided by Ninject? Of
course I will then start providing some actual stuff for injection
(like ISession objects and repositories, loggers etc), but I thought
I'd get this working first.

Any help will be highly appreciated!


Ian Davis

unread,
Mar 25, 2010, 4:33:25 PM3/25/10
to nin...@googlegroups.com
Download the latest give it a try. I updated the MVC2 trunk with a bug fix.

-Ian



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


Havremunken

unread,
Mar 25, 2010, 5:51:04 PM3/25/10
to ninject
Hi Ian,

Thanks for the quick response! Certain commercial companies could
learn from this.. :)

This fixed my issue. However, another one came up that might not be
critical, but you should probably be aware of it; Inside the new null
check, I get an HttpException like this:

The controller for path '/favicon.ico' was not found or does not
implement IController.

I never found out what it failed on before, but it is likely that it
was the favicon that time as well, as the default page doesn't have
any graphics or the master page doesn't refer to any local javascript
files.

Does this perhaps mean that Ninject's MVC stuff tries to handle too
much, and also gets involved in requests that shouldn't really be
handled by MVC? I.e. things that are not picked up by MVCs handler and
should be routed through to the normal pipeline? I am not too into
this stuff, so please forgive my ignorance.

Best regards,

Rune Jacobsen

> > ninject+u...@googlegroups.com<ninject%2Bunsu...@googlegroups.com >

Ian Davis

unread,
Mar 25, 2010, 5:54:12 PM3/25/10
to nin...@googlegroups.com
Are you registering routes that don't have controllers?

routes.IgnoreRoute("{*favicon}"new { favicon = @"(.*/)?favicon.ico(/.*)?" });

-Ian

To unsubscribe from this group, send email to ninject+u...@googlegroups.com.

Havremunken

unread,
Mar 25, 2010, 6:03:32 PM3/25/10
to ninject
Man, you know everything!

Right you are, that fixed it!

This just leaves me with a question - this application is as I
mentioned a 100% "File -> New" application. I didn't put in any code
there myself yet, except for the modifications to "Ninjectify" it. So
the route setup was the default that comes with the project in a
newborn state. I am probably not the only newbie that will be hit by
this; Should it be mentioned somewhere that this is needed? Will this
affect

a) people who don't use Ninject
b) people who use other IoC containers
c) other files than favicon.ico

? Should Microsoft do something about this?

I am just asking because I spent some time on google trying to figure
this out before coming here, and there are probably people out there
nearly as new to this as I am, who might end up struggling with the
same issue.

Either way - thank you very much for the help!

Best regards,

Rune Jacobsen

> > <ninject%2Bunsu...@googlegroups.com<ninject%252Bunsubscribe@googlegroup s.com>>

Havremunken

unread,
Mar 25, 2010, 6:24:34 PM3/25/10
to ninject
Ok, I googled a bit on this last topic, and seems this is an issue
with Cassini and running in debug mode, so apparently isn't Ninjects'
fault at all. I didn't actually see it before adding Ninject to the
mix, but I don't know enough to make an educated guess. It is easily
solved with the way you proposed or of course by actually adding the
favicon.ico file.

Again, thanks for the quick and very good response!

Ian Davis

unread,
Mar 26, 2010, 2:41:19 PM3/26/10
to nin...@googlegroups.com
IIRC, the favicon is not just IoC container users.

I don't actually do web development, so I am not the right audience to answer with any authority.

-Ian
 
To unsubscribe from this group, send email to ninject+u...@googlegroups.com.

Jarrett Meyer

unread,
Mar 26, 2010, 2:52:30 PM3/26/10
to nin...@googlegroups.com
Ian is correct. ASP.NET MVC throws a framework when the browser requests favicon.ico does throw an exception. Check your w3se event logs in your event viewer. They'll be there. Or if you use ELMAH (http://code.google.com/p/elmah/), you'll get entries for controllers not found. You just notice these errors less because they don't produce a nasty error screen when a full page 404's. One of the benefits/burdens of Ninject is that these errors will be brought to the surface through the activation binding.

Phil Haack wrote about the exception quite a while back.

Jarrett Meyer

unread,
Mar 26, 2010, 2:59:29 PM3/26/10
to nin...@googlegroups.com
I apologize for that 2nd sentence. It should be something closer to this:

MVC throws an exception when the browser requests a favicon.ico, whether it exists or not, because the default routing doesn't match anything like "favicon.ico".


--
Jarrett Meyer
Email: jarret...@gmail.com
Web: JarrettMeyer.com


Reply all
Reply to author
Forward
0 new messages