The type UmbracoContext does not have an accessible constructor.
Hello, I'm trying add an IoC container into Umbraco 7.0.4, I already tried Unity and Ninject, on Unity, when I try to run the website I get this error "The type UmbracoContext does not have an accessible constructor.", is there anything I might be doing incorrectly? Here's my code:
The controller I'm trying to execute:
[PluginController("MyProject")]
public class MyProjectController : MyProjectBaseController //the base controller inherits from Umbraco.Web.Mvc.SurfaceController
{
public IServiceA ServiceA { get; private set; }
public IServiceB ServiceB { get; private set; }
public MyProjectController (IServiceA serviceA, IServiceB serviceB)
{
this.ServiceA = serviceA;
this.ServiceB= serviceB;
}
}
Inside MyProject/App_Start/MvcStartup.cs:
public class MvcStartup : IApplicationEventHandler
{
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
AreaRegistration.RegisterAllAreas();
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityWebActivator.Start();
}
}
Unityconfig.cs:
public class UnityConfig
{
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
IControllerFactory factory = new Umbraco.Web.Mvc.RenderControllerFactory(); //I've tried both SurfaceControllerFactory and RenderControllerFactory, when I use SurfaceControllerFactory I get a blank page, when I use RenderControllerFactory I get the exception above
ControllerBuilder.Current.SetControllerFactory(factory);
container.RegisterInstance(typeof(UmbracoContext), "UmbracoContext", UmbracoContext.Current, new PerRequestLifetimeManager());
return container;
});
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IServiceA, ServiceA>();
container.RegisterType<IServiceB, ServiceB>();
}
}
UnityMvcActivator.cs:
public static class UnityWebActivator
{
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
}
I've tried to setup the container in the Global.asax with no luck, debugging this project I can see the container being set up however, this exception is always thrown, when I try to use Ninject I get the "no parameterless constructor defined for this object" exception
I've not used Unity Leando, but have Ninject and found it wasn't problematic to set up. I found instructions here and as it seemed (and turned out to be) nicely straightforward I've used that from now on for DI with Umbraco..
Thank you Jan and Andy, I got Ninject working after a few hours switching back and forth from Ninject to Unity, unfortunately I couldn't figure out what was wrong with my Unity setup so I decided to stick with Ninject
The type UmbracoContext does not have an accessible constructor.
Hello, I'm trying add an IoC container into Umbraco 7.0.4, I already tried Unity and Ninject, on Unity, when I try to run the website I get this error "The type UmbracoContext does not have an accessible constructor.", is there anything I might be doing incorrectly? Here's my code:
The controller I'm trying to execute:
Inside MyProject/App_Start/MvcStartup.cs:
Unityconfig.cs:
UnityMvcActivator.cs:
I've tried to setup the container in the Global.asax with no luck, debugging this project I can see the container being set up however, this exception is always thrown, when I try to use Ninject I get the "no parameterless constructor defined for this object" exception
Any ideas? Thanks a lot!
Hi Leandro
Perhaps you're missing some references to the Umbraco .dll files?
/Jan
I've not used Unity Leando, but have Ninject and found it wasn't problematic to set up. I found instructions here and as it seemed (and turned out to be) nicely straightforward I've used that from now on for DI with Umbraco..
Hope that helps.
Andy
Thank you Jan and Andy, I got Ninject working after a few hours switching back and forth from Ninject to Unity, unfortunately I couldn't figure out what was wrong with my Unity setup so I decided to stick with Ninject
is working on a reply...