Using Zend Framework 2 I can access the ViewHelperManager doing this:
public function onBootstrap(MvcEvent $e)
$viewHelperManager = $e->getApplication()->getServiceManager()->get('viewHelperManager');
}
This does not seem to work anymore in my Modules since starting a new project using Zend Framework 3.
Not quite sure what to do here.
The Services are uppercased in Zend Framework 3 apparently.
$viewHelperManager = $e->getApplication()->getServiceManager()->get('ViewHelperManager');
var_dump(get_class($viewHelperManager));
returns string(29) "Zend\View\HelperPluginManager"
Related
I started a new ASP.NET MVC project and I'm using Ninject as my IOC controller.
As far as regular MVC Controllers goes - everything work fine and binding is done to the controller constructor as expected.
I've added a WEB API to my project and did pretty much the same thing. here is the API constructor:
public class DetailsController : ApiController
{
private IClientInfoRetriever _clientInfoRetriever;
public DetailsController(IClientInfoRetriever clientInfoRetriever)
{
_clientInfoRetriever = clientInfoRetriever;
}
.
.
.
// The rest of my methods
}
here is the Ninject binding:
Bind<IClientInfoRetriever>().To<ClientInfoRetriever>();
but when I try to access my API (just putting the URL in the browser for a get action) I get the following error:
An error occurred when trying to create a controller of type 'DetailsController'. Make sure that the controller has a parameterless public constructor.
It's expecting to get an empty constructor, but if I give it an empty constructor to use - it won't be initializing the object I need.
What am I doing wrong? does Ninject support web api?
Thanks
Install the proper Ninject Nuget package for WebAPI:
PM> Install-Package Ninject.Web.WebApi -Version 3.0.0.2
I have 2 different module in project
1)Album
2)User
I want use some classes of User module in 'Album' Module.
Please help me how I can configure it.
Thanks and Regards,
Ashish
Service Classes should be accessible via the ServiceManager. In case Classes are made available there, either as factories or invokables, you should be able to call them from your Controller like this:
$this->getServiceLocator()->get('name-of-your-service');
Otherwise if it's only classes you need to use, you can simply import and use them like
namespace Othermodule;
use My\Module\Class\Name;
class OtherModuleClass {
public function someAction()
{
new Name();
// Or fully qualified inline
new \Third\Module\Class\Name();
}
}
I'm trying to use MVC Contrib Localization in my Asp.net MVC application, for now I have it working with resource files, but I want to use it with Sql Server,
I'm checking this tutorial: http://www.codeproject.com/Articles/352583/Localization-in-ASP-NET-MVC-with-Griffin-MvcContri
but it uses Autofac as the IoC container which I don't understand, does anyone have used it with Ninject? or anyone knows how this Autofac code can be transated to Ninject:
// Loads strings from repositories.
builder.RegisterType<RepositoryStringProvider>().AsImplementedInterfaces().InstancePerLifetimeScope();
builder.RegisterType<ViewLocalizer>().AsImplementedInterfaces().InstancePerLifetimeScope();
// Connection factory used by the SQL providers.
builder.RegisterInstance(new AdoNetConnectionFactory("DemoDb")).AsSelf();
builder.RegisterType<LocalizationDbContext>().AsImplementedInterfaces().InstancePerLifetimeScope();
// and the repositories
builder.RegisterType<SqlLocalizedTypesRepository>().AsImplementedInterfaces().InstancePerLifetimeScope();
builder.RegisterType<SqlLocalizedViewsRepository>().AsImplementedInterfaces().InstancePerLifetimeScope();
Thanks in advance.
I am trying to do the same thing at the moment with the Ninject.Web.MVC NuGet package.
I am not sure if Ninject has anything similar to .AsImplementedInterfaces() however you can still bind the interfaces yourself if not, its just more manual work looking at Griffin.MvcContrib's classes and what interfaces they implement.
One example to put in the NinjectWebCommon RegisterServices method is:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ILocalizedStringProvider>()
.To<RepositoryStringProvider>().InRequestScope();
...
}
The InRequestScope extension (https://github.com/ninject/Ninject.Web.Common/wiki/Inrequestscope), from what I have read so far, is the closest I can see to the AutoFac .InstancePerLifetimeScope() http://code.google.com/p/autofac/wiki/InstanceScope
As for .RegisterInstance(new AdoNetConnectionFactory("DemoDb")).AsSelf();
There is a .ToSelf() method for Ninject but I am not entirely sure what this line is doing yet myself.
I've got 2 projects, one is a MVC site, the other a class library. The MVC site references the class library.
I have an implementation of IWindsorInstaller in the MVC site and the class library. There is no code in the MVC site that directly references any classes defined in the class library, they are all implementations of interfaces defined elsewhere.
In the MVC site, in app start I'm doing the usual
var container = new Castle.Windsor.WindsorContainer();
container.Install(FromAssembly.InThisApplication());
This does not call the installer in the class library. However if I do this
container.Install(FromAssembly.Containing<ClassFromTheClassLibrary>());
The installer gets called twice. It seems that Castle requires an actual in-code reference to the other assembly for the InThisApplication to pick it up. I can fix this by just doing this:
container.Install(FromAssembly.This());
container.Install(FromAssembly.Containing<ClassFromTheClassLibrary>());
but I was hoping to not have to reference the other assembly directly.
Update The namespaces are:
MVC app is MyApp.OnlineProducts.Service
class library is MyApp.Individuals.Service
This should work if you name your assemblies as per required naming convention. If your main application assembly name is MyApp.exe, you should name other class libraries like MyApp.*.dll (e.g. FirstClassLibrary.Whatever.dll and MyApp.SecondClassLibrary.dll) and Windsor will pick up all of the related libraries abiding the naming convention. See this page on Windsor documentation that explains this behavior.
FromAssembly.InThisApplication() matches assemblies using the calling assembly as a prefix. Calling from MyApp.dll will match MyApp.Core.dll & MyApp.Stuff.dll.
So, renaming your class library may be an option?
Otherwise, you might want to use:
FromAssembly.InDirectory(new AssemblyFilter("c:\dir","*.dll"))
to locate your "component assemblies".
== UPDATE ==
My comments are misleading.
FromAssembly.InThisApplication() only walks assemblies referenced from the calling assembly.
To get Current executing Assembly, follow the other answer.
public class ServiceIoC
{
private static IWindsorContainer _container;
private static int initstatus = 0;
public static IWindsorContainer Container
{
get
{
if (_container == null && initstatus == 0)
{
initstatus = 1;
_container = new WindsorContainer();
_container.Install(FromAssembly.InThisApplication(Assembly.GetExecutingAssembly()));
}
return _container;
}
}
}
Following the other answers here, when building a web app / wcf service, make sure the class calling
var container = new Castle.Windsor.WindsorContainer();
container.Install(FromAssembly.InThisApplication());
does not live in the App_Code folder. This is an easy mistake to make, since the Windsor bootstrap process relies on the AppInitialize() method to be somewhere in App_Code, but having the actual bootstrap code in the App_Code folder makes it live in a separate App_Code assembly, breaking the assembly prefix conversion mentioned here.
I'm stuck. I was using the method outlined here for wcf web api p6 Ninject working with WCF Web API Preview 5, however things are quite a bit different with the mvc implementation in the beta. There is a good article here http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver that talks about building your own custom dependency resolver, however i would like to use the same implementation i'm using for my mvc view controllers...e.g. Ninject. I've tried a few things based on the IoC Unity example in the article too, but nothing has panned out yet. Any help pointing me in the right direction would be much appreciated. I'm going to keep digging on my own as well. Thanks in advance!
Here's where I'm at. I was using WebActivator to bootstrap the code but I've since dropped it to the Application_Start() just to take one more thing out of the equation.
protected void Application_Start()
{
var kernel = new StandardKernel(new MyNinjectModule());
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
And am receiving the following error:
The type Ninject.Web.Mvc.NinjectDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.Parameter name: commonServiceLocator
Found the solution
Perhaps there is/will be a more elegant way but this is now working for me. I'm also adding my custom message handler here as well.
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.AppStart.ApiBootstrapper), "Start")]
namespace MyApp.AppStart
{
public class ApiBootstrapper
{
public static void Start()
{
var kernel = new StandardKernel(new MyNinjectModule());
var resolver = new NinjectDependencyResolver(kernel);
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(resolver.GetService, resolver.GetServices);
GlobalConfiguration.Configuration.MessageHandlers.Add(new ApiAuthHandler());
}
}
}
I never used the WebAPI but since the semantic of the IDependencyResolver is exactly the same as the one from MVC3 you should be able to use the same implementation: https://github.com/ninject/ninject.web.mvc/blob/master/mvc3/src/Ninject.Web.Mvc/NinjectDependencyResolver.cs
Update:
The Ninject.Web.WebAPi extension adds support for ApiControllers
I found the answer and updated my question above with the solution. The solution itself was more or less present in the Using the Web API Dependency Resolver article, i just had to keep tweaking for ninject. Both answers helped me quickly narrow this down so thanks to #Remo and #James.
May sound silly but have you made sure you have a reference to the CommonServiceLocator / CommonServiceLocator.NinjectAdapter nuget package or associated assemblies. Your NinjectDependencyResolver may not be able to resolve the reference to IServiceLocator.
The same code will work for both MVC and the WebApi, however because the WebApi was inspired by MVC and the MVC assemblies do not have to be referenced in order to use WebApi (or vise versa), there is a fair amount of duplication of code between the two frameworks. If you want to use MVC you're dependencies will come from System.Web.Mvc, if you want to use WebApi you'll use System.Web.Http. If you want to use both you'll have to differentiate in your code which to use when, using a more explicit namespace resolution.
In your case your problem is that MVC came first and the NinjectDependancyResolver class inherits from System.Web.Mvc.IDependencyResolver. You'll want to create an exact duplicate of the NinjectDependancyResolver class and instead inherit from System.Web.Http.IDependencyResolver instead. Use THIS class to setup your IoC and you'll be good to go.
I found a nice solution here.
It's pretty easy if you're already using the Ninject CommonServiceLocator / Bootstrapper:
private static IKernel CreateKernel() {
var kernel = new StandardKernel();
RegisterServices(kernel);
GlobalConfiguration.Configuration.ServiceResolver
.SetResolver(new NinjectServiceLocator(kernel));
return kernel;
}