Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • leandro koiti 4 posts 24 karma points
    Mar 17, 2014 @ 12:47
    leandro koiti
    0

    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

    Any ideas? Thanks a lot!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 22, 2014 @ 18:00
    Jan Skovgaard
    0

    Hi Leandro

    Perhaps you're missing some references to the Umbraco .dll files?

    /Jan

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 23, 2014 @ 19:51
    Andy Butland
    1

    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

  • leandro koiti 4 posts 24 karma points
    Mar 23, 2014 @ 20:48
    leandro koiti
    0

    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

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies