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!
--
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.
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 >
To unsubscribe from this group, send email to ninject+u...@googlegroups.com.
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>>
Again, thanks for the quick and very good response!
To unsubscribe from this group, send email to ninject+u...@googlegroups.com.