Copied to clipboard

Flag this post as spam?

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


  • Alex 14 posts 95 karma points
    Dec 02, 2015 @ 17:49
    Alex
    0

    Umbraco 7.3.2 and Unity IOC error

    I am trying to implement Unity IOC in a Umbraco 7.3.2 project get YSOD at application startup.

    Sofar I have only the required dependencies added to the project and also register UmbracoContext as following:

           container.RegisterType<UmbracoContext>(
                new ContainerControlledLifetimeManager(),
                new InjectionFactory(c => UmbracoContext.Current));
    

    Then, Unity throws the following exception:

             The current type, Umbraco.Core.Models.IPublishedContent, is an interface and cannot be constructed. Are you missing a type mapping? 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
        Exception Details: System.InvalidOperationException: The current type, Umbraco.Core.Models.IPublishedContent, is an interface and cannot be constructed. Are you missing a type mapping?
    
        Source Error: ...... etc.
    

    What do I miss? How to find out which types I need to register to make Umbraco controllers work?

    Thanks!

  • Enrico 47 posts 247 karma points
    Dec 16, 2015 @ 10:07
    Enrico
    0

    I had similar problem with Unity and Umbraco 7.2.8. You can take a look at my implementation

    https://our.umbraco.org/forum/developers/api-questions/72485-umbraco-728-and-unity-dependency-resolver

    With this solution the back office breaks and does not work properly.

    I asked for some help but I didn't get any help for now

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

    Hi Alex,

    Did you have any luck with this? I'm getting the same error when accessing the Umbraco backoffice.

    Thanks,

    Razvan

  • Alex 14 posts 95 karma points
    Dec 30, 2015 @ 09:32
    Alex
    0

    I went for a lame solution choosing Autofac as IoC container, but I didn't have any preference about a particular IoC container and Autofac proved to work good.

  • Razvan Trifan 7 posts 78 karma points
    Dec 30, 2015 @ 12:40
    Razvan Trifan
    0

    Thank you for your suggestion, I'll give it a try.

  • Enrico 47 posts 247 karma points
    Dec 22, 2015 @ 11:55
    Enrico
    1

    Dependecy Injection update:

    I tried to follow the tutorial on Umbraco documentation at https://our.umbraco.org/documentation/reference/using-ioc

    I started creating with Visual Studio 2012 a new project

    With the Package Manager Console in Visual Studio 2012 I added Umbraco and Autofac

    PM > Install-Package UmbracoCms
    PM> Install-Package Autofac.Mvc5

    Following the tutorial I created the class MyAwesomeContext

    public class MyAwesomeContext
    {
        public MyAwesomeContext()
        {
            MyId = Guid.NewGuid();
        }
        public Guid MyId { get; private set; }
    }  
    

    And after that I modified the global.asax following the second method suggested in the tutorial, because the first one was not working:

     public class MyApplication : IApplicationEventHandler
        {
    
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                var builder = new ContainerBuilder();
    
                //register all controllers found in this assembly
                builder.RegisterControllers(typeof(MyApplication).Assembly);
    
                //add custom class to the container as Transient instance
                builder.RegisterType<MyAwesomeContext>();
    
                var container = builder.Build();
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            }
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
            }
        }
    

    After compiling I got two errors : one on the back end and one in the fron end

    Front End Error :

    System.MissingMethodException: No parameterless constructor defined for this object.

    Back End Error

    Received an error from the server Failed to retrieve data for application tree content

    Object reference not set to an instance of an object.

    EXCEPTION DETAILS:

    System.NullReferenceException: Object reference not set to an instance of an object.

    *I think that Umbraco 7 is not fully compatible with IOC Container ( I tried with Unity, Ninject and Autofac) *

    Obviosly, the tutorial on the documentation is not working

  • Dave Woestenborghs 2384 posts 6724 karma points mvp c-trib
    Dec 30, 2015 @ 14:00
    Dave Woestenborghs
    1

    Hi Alex,

    We have autofac working without any problem. But there are some Umbraco core things you need to register as well.

    This what we have :

            // Register the UmbracoContext as a factory, the controllers require this in their constructor
        builder.Register(c => UmbracoContext.Current).AsSelf();
    
    
        builder.RegisterControllers(typeof(MyApplication).Assembly);
        builder.RegisterApiControllers(typeof(MyApplication).Assembly);
        builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);                 
    
        builder.RegisterType<ApplicationContext>().AsSelf();
    
            // register your own services here
    
    
        // create container 
        var container = builder.Build();
    
        // setup the mvc dependency resolver to user autofac
        var resolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    

    Dave

  • Alexey Gilmanov 1 post 71 karma points
    Jan 28, 2016 @ 11:18
    Alexey Gilmanov
    0

    It seems that one more constructor have been added to RenderMvcController in latest releases:

    RenderMvcController(UmbracoContext umbracoContext, UmbracoHelper umbracoHelper)
    

    so, by default Unity use this constructor with the most parameters and next try to resolve UmbracoHelper using the constructor:

    public UmbracoHelper(UmbracoContext umbracoContext, IPublishedContent content, ITypedPublishedContentQuery typedQuery, IDynamicPublishedContentQuery dynamicQuery, ITagQuery tagQuery, IDataTypeService dataTypeService, UrlProvider urlProvider, ICultureDictionary cultureDictionary, IUmbracoComponentRenderer componentRenderer, MembershipHelper membershipHelper);
    

    where IPublishedContent does not have any registered types. Try to register the RenderMvcController constructor explicitly to avoid that:

    container.RegisterType<UmbracoContext>(
        new ContainerControlledLifetimeManager(),
        new InjectionFactory(c => UmbracoContext.Current))
    .RegisterType<RenderMvcController>(
        new InjectionConstructor(new ResolvedParameter<UmbracoContext>()))
    
  • Markus Johansson 1375 posts 3759 karma points
    Feb 23, 2016 @ 23:03
    Markus Johansson
    0

    Thanks Alexey for sharing your solution!

    I use StructureMap and had the same issue, this is the solution:

    x.ForConcreteType<RenderMvcController>()
                    .Configure.SelectConstructor(() => new RenderMvcController(UmbracoContext.Current));
    
  • azadeh khojandi 1 post 73 karma points
    Jun 26, 2016 @ 10:43
    azadeh khojandi
    2

    We had same issue with unity and umbraco and Merchello, we solved the issue by registering types

            container.RegisterTypes(AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()));
            container.RegisterTypes(AllClasses.FromLoadedAssemblies());
    
    
            container.RegisterInstance(typeof(UmbracoHelper), new UmbracoHelper(UmbracoContext.Current));
            container.RegisterInstance(typeof(UmbracoContext), UmbracoContext.Current);
            container.RegisterInstance(typeof(Merchello.Core.IMerchelloContext), MerchelloContext.Current);
            container.RegisterInstance(typeof(ApplicationContext), ApplicationContext.Current);
            container.RegisterInstance(typeof(ServiceContext), ApplicationContext.Current.Services);
            container.RegisterInstance(typeof(Umbraco.Core.Services.IMemberService), ApplicationContext.Current.Services.MemberService);
    
  • Simon Dingley 1219 posts 2777 karma points
    5 days ago
    Simon Dingley
    0

    Thanks for sharing - this resolved some issues for me.

  • WURMi 4 posts 74 karma points
    Feb 23, 2017 @ 19:04
    WURMi
    0

    Add an empty controller with name of the document type (ex. ContentPageController) and inherit from Umbraco.Web.Mvc.RenderMvcController

    public class ContentPageController : Umbraco.Web.Mvc.RenderMvcController
    {
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft