Copied to clipboard

Flag this post as spam?

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


  • Enrico 47 posts 252 karma points
    Oct 28, 2015 @ 10:06
    Enrico
    0

    Umbraco 7.2.8 and Unity Dependency Resolver Issue

    Hello everyone,

    I used unity as dependency resolver for several projects with Umbraco 7.2.6 and it was working good. I started using Umbraco 7.2.8 and the same implementation doesn't work anymore. Umbraco back end does not update or remove any content.

    Here is my implementation

    With the Package Manager Console in Visual Studio 2012 I added Unity.MVC4 and Unity Web Api

    PM > Install-Package Unity.Mvc4
    PM> Install-Package Unity.WebAPI -Version 0.10.0

    After installing Unity a new class is added to my project : bootstrapper.cs

    public static class Bootstrapper
      {
        public static IUnityContainer Initialise()
        {
          var container = BuildUnityContainer();
    
          DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    
          return container;
        }
    
        private static IUnityContainer BuildUnityContainer()
        {
          var container = new UnityContainer();
          container.RegisterInstance(typeof(UmbracoContext), UmbracoContext.Current);
          container.RegisterType<IApplicationConfiguration, ApplicationConfiguration>(); 
    
          RegisterTypes(container);
    
          return container;
        }
    
        public static void RegisterTypes(IUnityContainer container)
        {
    
        }
      }
    

    My global.asax

    public class MyApplication : IApplicationEventHandler
        {
    
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                Bootstrapper.Initialise();
            }
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
            }
        }
    

    My HomePage controller

    public class HomePageController : RenderMvcController
        {
            private readonly IApplicationConfiguration _config;
    
    
            public HomePageController(IApplicationConfiguration _config)
            {
                this._config = _config;
            }
            public ActionResult HomePage(RenderModel model)
            {
    
                ViewBag.Msg = "test of viewbag" + _config.test();
    
                return base.Index(model);
    
            }
        }
    

    My Interface

    public interface IApplicationConfiguration
        {
            string test();
        }
    

    My service

    public class ApplicationConfiguration : IApplicationConfiguration
        {
            public string test()
            {
                return "test is successfull";
            }
        }
    

    Basic DI stuff. But it breaks Umbraco 7.2.8 Back end

    I tried to follow several tutorial on the internet, but I can not find the solution.

    I also tried to follow the tutorial Umbraco documentation - with Autofac but I was not able to make it work

    Does anyone have a any clue?

  • Enrico 47 posts 252 karma points
    Dec 16, 2015 @ 10:04
    Enrico
    0

    I tried also with Ninject and the result is the same: the back office breaks. Does anyone have a working example of Dependecy Injection with any dependency resolver?

    Thanks in adavance

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Dec 16, 2015 @ 16:40
    David Brendel
    0

    Don't know a solution for this but I'm also interested. Want to wire up ninject to make a package Plugin ready.

    Maybe one way would be to create a custom dependency resolver?

    Regards David

  • Enrico 47 posts 252 karma points
    Dec 19, 2015 @ 11:33
    Enrico
    0

    Hi David,

    it's a pity not to be able to use Dependency Injection. I will give it a try with the new version of Umbraco and let you know. 'Who dares wins...'

  • Razvan Trifan 7 posts 78 karma points
    Dec 20, 2015 @ 23:43
    Razvan Trifan
    0

    Hi,

    I was wondering if you had any luck with this. I'm getting the same error when accessing the Umbraco backoffice.

    Thanks, Razvan

  • Mark 11 posts 82 karma points
    May 17, 2016 @ 22:28
    Mark
    0

    Hi There,

    Same issue here too. Any resolution yet for anyone?

    Thanks in advance

  • Razvan Trifan 7 posts 78 karma points
    May 17, 2016 @ 22:41
    Razvan Trifan
    1

    Hi Mark,

    Meanwhile I've upgraded to 7.3.7 and I switched to Autofac, which works nicely. Here's an extract from my Autofac configuration, hope it will point you in the right direction:

        using System.Globalization;
    using System.Reflection;
    using System.Web.Http;
    using System.Web.Mvc;
    using Autofac;
    using Autofac.Core;
    using Autofac.Integration.Mvc;
    using Autofac.Integration.WebApi;
    using Umbraco.Core;
    using Umbraco.Web;
    
    public static class AutofacConfiguration
    {
        private static CultureInfo currentCulture = new CultureInfo("en-GB");
    
        public static void ConfigureContainer()
        {
            var builder = new ContainerBuilder();
    
            // Register the UmbracoContext as a factory, the controllers require this in their constructor
            builder.Register(c => UmbracoContext.Current).AsSelf();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterApiControllers(typeof(UmbracoContext).Assembly);
    
            builder.RegisterType<ApplicationContext>().AsSelf();
    
            // register your own services here
            RegisterContextObjects(builder);
    
            // create container and setup the mvc dependency resolver to user autofac
            var container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    
        private static void RegisterContextObjects(ContainerBuilder builder)
        {
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().WithParameters(
                new[]
                {
                     new ResolvedParameter((p, c) => p.Name == "currentCulture", (p, c) => currentCulture)
                });
        }
    }
    
  • Enrico 47 posts 252 karma points
    May 18, 2016 @ 08:16
    Enrico
    0

    Hi Mark,

    I will try with your method. Thanks for sharing it.

    Enrico

  • Enrico 47 posts 252 karma points
    May 18, 2016 @ 08:27
    Enrico
    0

    Hi guys,

    if you need sometimes to avoid using Dependency Resolvers you can injct dependency in following way

    This an example of DI in an Umbraco Surface controller. I have an interface called IContactRepository and a repository called ContactRepository.

     public class ContactController : Umbraco.Web.Mvc.SurfaceController
        {
            private readonly IContactRepository _contact;
    
            public ContactController() : this(new ContactRepository()) { 
    
            }
            public ContactController(IContactRepository contact)   
            {
                this._contact = contact;
            }
    
    
          }
    

    I hope it will be helpful.

  • Mark 11 posts 82 karma points
    May 19, 2016 @ 09:32
    Mark
    0

    Thanks Razvan,

    I've given up on Unity now and will try with Autofac...which version of the MVC and WebApi package are you using?

    Ta Mark

    Update: All working now. Thanks Razvan! Much appreciated....for anyone else coming across this post add Autofac MVC5 and WebApi2 packages also. My Umbraco version is 7.4.3

  • Razvan Trifan 7 posts 78 karma points
    May 20, 2016 @ 18:15
    Razvan Trifan
    0

    Hi Mark,

    I'm using Autofac.Mvc5 v 3.3.4, Autofac.WebApi2 v 3.4.0, Autofac v 3.5.0, Microsoft.AspNet.Mvc v 5.2.3 and WebApi v 5.2.0.

    Hope that helps.

    Cheers, Razvan

Please Sign in or register to post replies

Write your reply to:

Draft