Automatic startup registration isn't working

14 views
Skip to first unread message

shovavnik

unread,
Jul 23, 2010, 11:12:46 AM7/23/10
to MVC Turbine
I'm trying to add MvcTurbine to a new project I'm starting and am
having some trouble. Some of it has to do with inadequate
documentation, which I figured out by looking at the code. (For
example, override the Startup() method instead of implementing
Application_Start in the MvcApplication class).

The problem I'm having now is that the service registrations aren't
taking effect. If add a service dependency to my HomeController for
ISomeService, the controller is never resolved and turbine throws the
container's resolution error for ISomeService. Also, the service
locator is trying to resolve IRotorContext and a bunch of other
turbine services, and failing for those as well.

I tried registering using ServiceLocator.Register<ISomeService>(new
SomeService()); and also ServiceLocator.Register<ISomeService,
SomeService>(); but neither worked.

I tried using the Windsor, Unity and Ninject containers, and they all
fail in the same way.

I've also tried running ServiceLocator.Register and immediately after
that, ServiceLocator.Resolve, and the resolve fails.

Am I missing something or doing something wrong?

I'm using the recent 2.1 release downloaded from CodePlex.

Darren Cauthon

unread,
Jul 23, 2010, 11:22:12 AM7/23/10
to MVC Turbine

Just to double-check (since you shouldn't have to override Startup or
Application_Start or anything like that with Turbine), I'll list the
things I'd do if I wanted to do what you are describing:

1.) Follow the codeplex instructions on setting up (what little
documentation we have right now). Pretty much, this just requires you
to change your application to inherit from TurbineApplication instead
of MvcApplication.

2.) Pick a unity container and set it up like the example provided on
codexplain.

3.) Create a service locator class that will register your
ISomeService, like so:

public class SomeClassRegistration : IServiceRegistration{
public void Register(IServiceLocator locator)
{
locator.Register<ISomeService, SomeService>();
}
}


Are you doing your registration in the same way? You've probably gone
this far, but without seeing the code I just want to start from the
beginning.


Darren

Javier Lozano

unread,
Jul 23, 2010, 12:48:34 PM7/23/10
to MVC Turbine
This is weird, for some reason I get the emails from threads but I
can't see them online. So this is in response to Noam's last email to
this thread.
---
Yes, unfortunately that's the only way to do it right now in v2.1
since the bits don't know anything about the new MVC2 features. The
support for MVC2 pieces will be added on v2.2 which I'm currently
working on at http://github.com/jglozano/mvcturbine.

However, if you want to use the Area "Blade" for registration of MVC2
areas, you can do so (Will post code below). Essentially a "Blade" is
component that Turbine knows about and executes at App_Start. So what
we've done is to take the all the pieces that would typically go
inside the App_Start method and moved to a "Blade" in order to keep
all the extra clutter from App_Start from getting out of control.
Some of the clutter is the AreaRegistration pieces that are part of
the MVC2 bits.

Instead the same came accomplished with this:

public class AreaBlade : Blade, ISupportAutoRegistration {
public void AddRegistrations(AutoRegistrationList
registrationList) {

registrationList.Add(Registration.Simple<AreaRegistration>());
}

public override void Spin(IRotorContext context) {
var serviceLocator =
GetServiceLocatorFromContext(context);
var areaList = GetRegisteredAreas(serviceLocator);

if (areaList == null || areaList.Count == 0) return;

foreach (AreaRegistration registration in areaList) {
var registrationContext = new
AreaRegistrationContext(registration.AreaName, RouteTable.Routes);
var areaNamespace = registration.GetType().Namespace;

if (areaNamespace != null) {
registrationContext.Namespaces.Add(areaNamespace +
".*");
}

registration.RegisterArea(registrationContext);
}
}

public virtual IList<AreaRegistration>
GetRegisteredAreas(IServiceLocator locator) {
try {
return locator.ResolveServices<AreaRegistration>();
}
catch (Exception) {
return null;
}
}
}

All you need to do is add this class in an assembly that is referenced
by your application and Turbine automatically will register your
AreaRegistration types. Thus eliminating the need to call
AreaRegistration.RegisterAllAreas() from the App_Start.

If you feel uncomfortable with this piece, I completely understand and
like I previously mentioned, this will be added to v2.2 of MVC Turbine
so it will become a non-issue.

Thanks for checking out MVC Turbine!
Reply all
Reply to author
Forward
0 new messages