Copied to clipboard

Flag this post as spam?

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


  • Matthew Hunt 13 posts 35 karma points
    Jul 09, 2013 @ 14:23
    Matthew Hunt
    1

    Umbraco 6, MVC and Unity

    I've just setup a new instance of Umbraco 6.1.2 and am using MVC. I'm also using Unity to inject a unit of work into my overriden RenderMvcController (HomeController.cs). While the Home controller works fine and even the web api controllers I've added work fine, when I try and use a page with no special setup at all I get the error

    The type UmbracoContext does not have an accessible constructor.

    that also contains

    [ResolutionFailedException: Resolution of the dependency failed, type = "Umbraco.Web.Mvc.RenderMvcController", name = "(none)".
    Exception occurred while: while resolving.
    Exception is: InvalidOperationException - The type UmbracoContext does not have an accessible constructor.

    so I'm guessing I need to setup something in my Unity container to inject the context or I've seen a couple of posts that talk about writting your own ControllerFactory. I've not been able to find any useful articles on the web that have working examples.

     

    This is my Unity setup

    public static class UnityConfig

        {

            public static void Initialise()

            {

                var container = BuildUnityContainer();

     

                GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

                DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            }

     

            private static IUnityContainer BuildUnityContainer()

            {

                var container = new UnityContainer();

     

                RegisterTypes(container);

     

                IControllerFactory factory = new UmbracoControllerFactory(container);

                ControllerBuilder.Current.SetControllerFactory(factory);

     

                return container;

            }

     

            public static void RegisterTypes(IUnityContainer container)

            {

                string connectionString = ConfigurationManager.ConnectionStrings["EntityContainer"].ConnectionString;

                container.RegisterType<IDatabaseFactory, DatabaseFactory>

                    (new HttpContextLifetimeManager<IDatabaseFactory>())

                    .RegisterType<DatabaseFactory>(new InjectionConstructor(connectionString));

                container.RegisterType<IUnitOfWork, UnitOfWork>();

                container.RegisterType<IDeviceManager, DeviceManager>();

            }

        }

    Any help on this would be great as I'm new to Unity and this is my first Umbraco project using Dependency Injection.

  • Matthew Hunt 13 posts 35 karma points
    Jul 09, 2013 @ 16:07
    Matthew Hunt
    1

    well this seems to have worked, not sure if its correct though

    container.RegisterInstance(typeof (UmbracoContext), UmbracoContext.Current);

  • Justin Moser 20 posts 92 karma points
    Aug 14, 2013 @ 22:43
    Justin Moser
    0

    Hi Matthew,

    I've just come across your post, and I am having the same problem. I am using the UmbracoApiController and injecting my IUnitOfWork into the controller using unity. I am also setting up unity in my own EventHalder class (inherited from IApplicationEventHandler) which deals with OnApplicationStarting/OnApplicationStarted. The code in my OnApplicationStarting method is:

     

                var unity = new UnityContainer();

                unity.RegisterType<PropertiesApiController>();

                unity.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());

                unity.RegisterInstance(typeof (UmbracoContext), UmbracoContext.Current);

                config.DependencyResolver = new IoCContainer(unity);

     

    however I get the same error as you "The type IUnitOfWork does not have an accessible constructor."

    Have you had any luck with this/can you post the code you have that fixed this please?

     

    Thanks,

    Justin

  • Lee 1130 posts 3088 karma points
    Jun 20, 2014 @ 10:36
Please Sign in or register to post replies

Write your reply to:

Draft