How to Resolve component from Action?

Dec 17, 2009 at 4:41 PM

I`m trying to resolve some of my IUserService in Action using code like this

var s = MvcTurbine.ComponentModel.ServiceLocatorManager.Current.Resolve<IUserService>();

 

But I`m getting an Exception from Windsor: "No component for supporting the service Persistence.Services.IUserService was found". I`ve checked that UserService is Registered in Persistence library.

locator.Register<IUserService, UserService>();

 

Then I went to debug and noticed, that every time I call ServiceLocatorManager.Current, the version (or guid name) of IWindsorContainer is different. So there is no service registrations already. What I`m doing wrong?

Coordinator
Dec 17, 2009 at 10:45 PM

Yes, you are correct since the implementation of ServiceLocatorManager.Current is as follows:

public static IServiceLocator Current {
    get {
            if (currentProvider == null) {
                    throw new InvalidOperationException(Properties.Resources.ServiceLocatorProviderExceptionMessage);
            }

            return currentProvider();
    }
}

Which means it will call you registered ServiceLocatorProvider which typically is () => new MyServiceLocator(), so it's not initialized. The ServiceLocatorManager.Current was not meant to be used within your code. If you're planning on using a IUserService instance within an action in your controller, you should inject it via constructor or property injection.