Copied to clipboard

Flag this post as spam?

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


  • kris 13 posts 54 karma points
    Feb 24, 2017 @ 11:14
    kris
    0

    Umbraco 7.5.10 and Unity accessible constructor error

    Hi all,

    I'm having a bit of an issue setting up Unity to work with Umbraco 7.5.10.

    I have set up Unity with a bog standard MVC app and it works great so I'm guessing that it's a Umbraco step that I've missed out.

    After installing Unity and Unity.MVC packages via nuget I've restarted the application and and an getting the following error...

    The type UmbracoContext does not have an accessible constructor.
    

    I have tried to register the Umbraco Context in both the UnityConfig.cs and the Global.aspx files using the following line...

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

    Any help or advice on this issue would be a great help thanks in advance.

    Kris

    Here's the error stack trace...

    [InvalidOperationException: The type UmbracoContext does not have an accessible constructor.]
    

    Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForNullExistingObject(IBuilderContext context) +226 lambdamethod(Closure , IBuilderContext ) +39 Microsoft.Practices.ObjectBuilder2.<>cDisplayClass1.0(IBuilderContext context) +33 Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +337 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +396 Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +130 lambdamethod(Closure , IBuilderContext ) +208 Microsoft.Practices.ObjectBuilder2.<>cDisplayClass1.0(IBuilderContext context) +33 Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +337 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +396 Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +238

    [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.

    At the time of the exception, the container was:

    Resolving Umbraco.Web.Mvc.RenderMvcController,(none) Resolving parameter "umbracoContext" of constructor Umbraco.Web.Mvc.RenderMvcController(Umbraco.Web.UmbracoContext umbracoContext, Umbraco.Web.UmbracoHelper umbracoHelper) Resolving Umbraco.Web.UmbracoContext,(none) ] Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +500 Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) +20 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +59

    [InvalidOperationException: An error occurred when trying to create a controller of type 'Umbraco.Web.Mvc.RenderMvcController'. Make sure that the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +240 Umbraco.Web.Mvc.UmbracoControllerFactory.CreateController(RequestContext requestContext, String controllerName) in c:\projects\umbraco-cms-xtjqd\src\Umbraco.Web\Mvc\UmbracoControllerFactory.cs:38 Umbraco.Web.Mvc.RenderControllerFactory.CreateController(RequestContext requestContext, String controllerName) in c:\projects\umbraco-cms-xtjqd\src\Umbraco.Web\Mvc\RenderControllerFactory.cs:37 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +268 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +95 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1204 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +196

  • Laurent Lequenne 123 posts 248 karma points
    Feb 24, 2017 @ 13:09
    Laurent Lequenne
    0

    That basic registration should work for the Umbraco part...

       container = new UnityContainer();            
    
            container.RegisterType<UmbracoContext>(
                    new HierarchicalLifetimeManager(),
                    new InjectionFactory(c => UmbracoContext.Current));
    
            container.RegisterType<HealthCheckController>(new InjectionConstructor());
    
            // MVC Configuration
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    
            // Web API Configuration
            GlobalConfiguration.Configuration.DependencyResolver = new UnityWebApi.UnityDependencyResolver(container);
    
  • kris 13 posts 54 karma points
    Feb 24, 2017 @ 14:22
    kris
    0

    Thanks For your response Laurent.

    This code has definitely progressed the issue but I'm getting another error now.

    The current type, Umbraco.Core.Models.IPublishedContent, is an interface and cannot be constructed. Are you missing a type mapping?
    

    Just to check that the code you pasted above was in the UnityConfig.cs class?

    Just to add the UmbracoContext.Current is null.

    Kris

  • Laurent Lequenne 123 posts 248 karma points
    Feb 24, 2017 @ 14:39
    Laurent Lequenne
    0

    Kris,

    For every issue you need to know what do you need to inject. I didn't got that one but should be solved by registering how the object that gave the error is constructed. IPublishedContent should come from your current request, and is the current page requested.

    What is the scope of that injection process because your umbracocontext.current should not be null if you are in the scope of a webrequest handled by umbraco.

    You should also register your unity configuration in the OnApplicationStarting, and not OnApplicationStarted.

Please Sign in or register to post replies

Write your reply to:

Draft