Copied to clipboard

Flag this post as spam?

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


  • stephen 84 posts 125 karma points
    Sep 20, 2018 @ 13:53
    stephen
    0

    Dependency Injection - IOC

    I was following the instructions at https://our.umbraco.com/documentation/reference/using-ioc but without success.

    As per the instructions I have created:

    1. public class MyAwesomeContext
    2. public class MyApplication : Umbraco.Web.UmbracoApplication
    3. the Application tag inside the global.asax file in the web root
    4. public class MyTestSurfaceController : SurfaceController

    but I get internal server error

    In the global file I register the controllers: var builder = new ContainerBuilder(); // register all controllers found in your assembly builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterApiControllers(typeof(MvcApplication).Assembly);

    builder.RegisterControllers(typeof(Umbraco.Web.UmbracoApplication).Assembly); builder.RegisterApiControllers(typeof(Umbraco.Web.UmbracoApplication).Assembly);

    // add custom class to the container as Transient instance builder.RegisterType

    var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    but as soon as a call is added in the surfacecontroller to "public depinjController" it fails.

    public class depinjController : SurfaceController { private readonly namespace.Utils.MyAwesomeContext _myAwesome;

        public depinjController(namespace.Utils.MyAwesomeContext myAwesome)
        {
            _myAwesome = myAwesome;
        }
    
        [ChildActionOnly]
        public ActionResult Index()
        {
            return Content("Hello World! Here is my id " + _myAwesome.MyId);
        }
    }
    
  • Yakov Lebski 594 posts 2350 karma points
    Sep 25, 2018 @ 20:58
    Yakov Lebski
    0

    I think you should register MyAwesomeContext in container too

  • 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