Ninject MVC with Sitefinity
I have created a Sitefinity MVC4 project, however when I try to use Ninject MVC4 to use IOC, I get the following error. The same example works fine in a regular MVC project
Error activating ICommonFacadeWrapper
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency ICommonFacadeWrapper into parameter wrapper of constructor of type HomeController
1) Request for HomeController
Suggestions:
1) Ensure that you have defined a binding for ICommonFacadeWrapper.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.
Hi
You can check how to use Ninject with Sitefinity here.
I have created an MVC widget controller using a service loaded with Ninject.
I have the following service and controller:
Service:
public
class
NewsItemModelsService : INewsItemModelsService
...
[Inject]
public
NewsWidgetCustomController(INewsItemModelsService modelsService)
this
.ModelsService = modelsService;
using
Ninject;
using
Ninject.Modules;
using
SitefinityWebApp.Mvc.Models;
using
System;
using
System.Web.Mvc;
using
Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers;
namespace
SitefinityWebApp.Mvc
public
class
NinjectControllerFactory : FrontendControllerFactory
private
IKernel ninjectKernel;
/// <summary>
/// Initializes a new instance of the <see cref="NinjectControllerFactory"/> class.
/// </summary>
public
NinjectControllerFactory()
this
.ninjectKernel =
new
StandardKernel(
new
InterfaceMappings());
protected
override
IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
if
(controllerType ==
null
)
return
null
;
var resolvedController =
this
.ninjectKernel.Get(controllerType);
IController controller = resolvedController
as
IController;
return
controller;
public
class
InterfaceMappings : NinjectModule
/// <summary>
/// Loads the module into the kernel.
/// </summary>
public
override
void
Load()
Bind<INewsItemModelsService>().To<NewsItemModelsService>();
protected
void
Application_Start(
object
sender, EventArgs e)
Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
protected
void
Bootstrapper_Bootstrapped(
object
sender, EventArgs e)
ObjectFactory.Container.RegisterType<ISitefinityControllerFactory, NinjectControllerFactory>(
new
ContainerControlledLifetimeManager());
var factory = ObjectFactory.Resolve<ISitefinityControllerFactory>();
ControllerBuilder.Current.SetControllerFactory(factory);
[Inject]
Thanks for the details, I tried this today and it does not work for me. I compiled the code and I end up getting an error on compile which is as follows
Error33Unable to load assembly 'Cola.UI, Version=8.0.5710.0, Culture=neutral, PublicKeyToken=null' : Could not load file or assembly 'Telerik.Sitefinity, Version=8.0.5700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500)
ExceptionString:
System.SystemException: Unable to load assembly 'Cola.UI, Version=8.0.5710.0, Culture=neutral, PublicKeyToken=null' : Could not load file or assembly 'Telerik.Sitefinity, Version=8.0.5700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500) ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at Telerik.OpenAccess.Metadata.MetadataAttributesReader.PrepareMetaModelTypes(Assembly assembly)
--- End of inner exception stack trace ---
at Telerik.OpenAccess.Metadata.MetadataAttributesReader.PrepareMetaModelTypes(Assembly assembly)
at Telerik.OpenAccess.Metadata.MetadataAttributesReader.ReadMetaModel(Assembly assembly)
at Telerik.OpenAccess.Metadata.AttributesMetadataSource.<>c__DisplayClass1.<FromAssembly>b__0()
at Telerik.OpenAccess.Metadata.AttributesMetadataSource.CreateModel()
at Telerik.OpenAccess.Metadata.MetadataSource.GetModelCore(MetadataContainer old)
at Telerik.OpenAccess.Sdk.Enhancer.Enhancer.CrossDomainRunImpl(AssemblyLoader assemblyLoader)
at Telerik.OpenAccess.Sdk.Enhancer.EnhancerBase.CrossDomainRun()C:\TFSLocal\COLA\UI\Cola.UI\obj\Debug\Cola.UI.dllCola.UI
Hi
I doubt the error you are receiving is connected with the Ninject sample. It is an assembly references error. Please, check the referenced Telerik.Sitefinity version, as well as, from where it is read - the project bin folder or from nuget packages.
Regards,
Nikola Zagorchev
Telerik
I have the same problem on my project, after changing the Telerik references from /bin folder to Nuget packages.
The oddest thing is that if I comment the NinjectControllerFactory that inherits from FrontendController Factory it builds successfully the solution.
The error seams to be thrown by OpenAccess enhacer.exe
I'm using Sitefinity Version=8.0.5710.0. Have no idea why it needs Version=8.0.5700.0. There is no reference to it throughout the project, neither in web.config
This is my error:
[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: Unable to load assembly 'SitefinityWebApp, Version=8.0.5710.0, Culture=neutral, PublicKeyToken=null' : Could not load file or assembly 'Telerik.Sitefinity, Version=8.0.5700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500)
[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: ExceptionString:
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: System.SystemException: Unable to load assembly 'SitefinityWebApp, Version=8.0.5710.0, Culture=neutral, PublicKeyToken=null' : Could not load file or assembly 'Telerik.Sitefinity, Version=8.0.5700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500) ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
4>C:\Development\TerminalManagement\mytemporaryalphawebsite\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at System.Reflection.RuntimeModule.GetTypes()
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at System.Reflection.Assembly.GetTypes()
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at Telerik.OpenAccess.Metadata.MetadataAttributesReader.PrepareMetaModelTypes(Assembly assembly)
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: --- End of inner exception stack trace ---
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at Telerik.OpenAccess.Metadata.MetadataAttributesReader.PrepareMetaModelTypes(Assembly assembly)
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at Telerik.OpenAccess.Metadata.MetadataAttributesReader.ReadMetaModel(Assembly assembly)
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at Telerik.OpenAccess.Metadata.AttributesMetadataSource.<>c__DisplayClass1.<FromAssembly>b__0()
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at Telerik.OpenAccess.Metadata.AttributesMetadataSource.CreateModel()
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at Telerik.OpenAccess.Metadata.MetadataSource.GetModelCore(MetadataContainer old)
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at Telerik.OpenAccess.Sdk.Enhancer.Enhancer.CrossDomainRunImpl(AssemblyLoader assemblyLoader)
4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: at Telerik.OpenAccess.Sdk.Enhancer.EnhancerBase.CrossDomainRun()
Could you please help me? Many thanks!!
Hello
Please, try resolving the issue using a binding redirect:
Regards,
Nikola Zagorchev
Telerik
Actually, sitefinity never calls GetControllerInstance method. I still did not find how to make any container to work correctly within sitefinity.
Hello
You still have to add the ninject module registration:
protected
void
Application_Start(
object
sender, EventArgs e)
Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
protected
void
Bootstrapper_Bootstrapped(
object
sender, EventArgs e)
// DI binding
ObjectFactory.Container.RegisterType<ISitefinityControllerFactory, NinjectControllerFactory>(
new
ContainerControlledLifetimeManager());
var factory = ObjectFactory.Resolve<ISitefinityControllerFactory>();
ControllerBuilder.Current.SetControllerFactory(factory);
...
public
class
NinjectControllerFactory: FrontendControllerFactory
private
IKernel ninjectKernel;
public
NinjectControllerFactory()
ninjectKernel = Telerik.Sitefinity.Frontend.FrontendModule.Current.DependencyResolver;
ninjectKernel.Bind<IAuthorService>().To<AuthorService>();
protected
override
IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
if
(controllerType ==
null
)
return
null
;
var resolvedController =
this
.ninjectKernel.Get(controllerType);
IController controller = resolvedController
as
IController;
return
controller;
Hi Thank you for the code.
I was able to make this work by switching the registration from
Bootstrapper.Initialized += (obj, ev) =>
if (ev.CommandName != "Bootstrapped") return;
-- do the registration
to
Bootstrapper.Bootstrapped += (obj, ev) =>
-- do the registration
Thanks a lot.
Cheers.
J
Hi
I am glad you have managed to make it work.
Regards,
Nikola Zagorchev
Telerik
Hi everyone,
I was developing one custom widget and had the same issue so
I have followed this forum but still I am facing the same issue.
Can someone help me how to solve this issue?
FYI.
drive.google.com/open
Please find the file here.
Thanks,
Balu
Hi Balu,
What is the exception and stack trace that you get? The code in the Global application class seems correct.
Regards,
Nikola Zagorchev
Telerik by Progress
Hi Nikola,
I was getting below exception message.
An exception of type 'Ninject.ActivationException' occurred
in Ninject.dll but was not handled in user code
Additional information: Error activating ICorticonFormModel
No matching bindings are available, and the type is not
self-bindable.
Activation path:
1) Request for
ICorticonFormModel
Suggestions:
1) Ensure that you
have defined a binding for ICorticonFormModel.
2) If the binding
was defined in a module, ensure that the module has been loaded into the
kernel.
3) Ensure you have
not accidentally created more than one kernel.
4) If you are using
constructor arguments, ensure that the parameter name matches the constructors
parameter name.
5) If you are using
automatic module loading, ensure the search path and filters are correct.
Thanks,
Balu
Hello,
You do not have the Bootstrapped event attached to:
protected
void
Application_Start(
object
sender, EventArgs e)
Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
Hi Nikola,
Thank you. I have attached the Bootstrap event in my Global.ascx.cs file.
protected void
Application_Start(object sender, EventArgs e)
Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized +=
Bootstrapper_Initialized;
Thanks,
Balu
Hello Balu,
In your case, you need to attach to both events, since you execute code in both:
Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
and
Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
In the current implementation the Bootstrapper_Bootstrapped
handler where the Ninject factory is registered is not executed at all.
Regards,
Nikola Zagorchev
Telerik by Progress
Hi Nikola,
Thank you so much. My issue got resolved and widget is
working as expected.
Thanks,
Balu
Hi Balu,
I am glad the issue is resolved.
Regards,
Nikola Zagorchev
Telerik by Progress
Hi ,
i am getting error with ninject when sitefinity upgarded from 6.0 to 9.2
did you happen to resolve this? I'm trying to use DI in SF 10 to pass a service dependency into my Feather Widget MVC Controllers, but I'm getting a similar error of
No matching bindings are available, and the type is not self-bindable.
the documentation I've been able to find on this is sparse and inconsistent, anyone else been able to get DI to work with SF 10? Either with Ninject, Autofac, or Unity (all of which are apparently present!)
thanks
Hi,
I am new to Sitefinity. I am trying to deploy an old sitefinity site on another system and am getting the following runtime error:
Resolution of the dependency failed, type = "Telerik.Sitefinity.Mvc.ISitefinityControllerFactory", name = "(none)". Exception occurred while: Calling constructor LendNationSitefinity.NinjectControllerFactory(). Exception is: NullReferenceException - Object reference not set to an instance of an object. ----------------------------------------------- At the time of the exception, the container was: Resolving LendNationSitefinity.NinjectControllerFactory,(none) (mapped from Telerik.Sitefinity.Mvc.ISitefinityControllerFactory, (none)) Calling constructor LendNationSitefinity.NinjectControllerFactory()
How do I resolve the above error? Is this because of a missing library?