Copied to clipboard

Flag this post as spam?

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


  • David Redmond 24 posts 76 karma points
    Mar 14, 2016 @ 12:44
    David Redmond
    0

    DI/IoC (Autofac) with a LeBlenderController

    Hi All,

    I've got a LeBenderController that I'm trying to setup with DI/IoC using Autofac. I've got Autofac configured and working for other controllers, Backoffice & LeBlender in the Backoffice but I can't manage to get it working in a LeBlenderController.

    Here is the error message received:

    Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
    
    {"Cannot bind source type Lecoati.LeBlender.Extension.Models.LeBlenderModel to model type Umbraco.Web.Controllers.ProjectListing."}
    

    If I create a parameterless constructor all works well. Here is Autofac configuration:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                var builder = new ContainerBuilder();
                builder.RegisterControllers(Assembly.GetExecutingAssembly());
                builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
                builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
                builder.RegisterApiControllers(typeof(Lecoati.LeBlender.Extension.Controllers.LeBlenderController).Assembly);
                builder.RegisterControllers(typeof(Lecoati.LeBlender.Extension.Controllers.LeBlenderController).Assembly);
    
                builder.RegisterType<AutofacHandlerResolver>().As<ICommandDispatcher>();
                builder.RegisterType<ContactService>().As<IContact>();
                builder.RegisterType<ProductsService>().As<IProduct>();
                builder.RegisterType<ProjectsService>().As<IProject>();
                builder.RegisterType<SuburbService>().As<ISuburb>();
    
                foreach (var assm in BuildManager.GetReferencedAssemblies().Cast<Assembly>())
                {
                    builder.RegisterAssemblyTypes(assm).AsClosedTypesOf(typeof(ICommandHandler<>));
                    builder.RegisterAssemblyTypes(assm).AsClosedTypesOf(typeof(ICommandHandler<,>));
                }
    
                var container = builder.Build();
                var resolver = new AutofacWebApiDependencyResolver(container);
                GlobalConfiguration.Configuration.DependencyResolver = resolver;
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
                base.ApplicationStarted(umbracoApplication, applicationContext);
            }
    

    The controller is pretty straight forward:

    public class ProjectListingController : LeBlenderController
        {
            private readonly LeBlenderHelper _leBlenderHelper;
            private readonly IProject _projectService;
    
            public ProjectListingController(IProject projectService)
            {
                _leBlenderHelper = new LeBlenderHelper();
                _projectService = projectService;
            }
    
            public ActionResult Index(LeBlenderModel model)
            {
                var projectModel = new ProjectListing
                {
                    Header = _leBlenderHelper.GetValue(model, "heading", "Current Projects"),
                    Projects = new List<ProjectListingSummary>()
                };
                return View(projectModel);
            }
        }
    }
    

    LeBlenderHelper is just a wrapper to get a string value from the LeBlenderModel;

    public class LeBlenderHelper
    {
        public string GetValue(LeBlenderModel model, string valueName, string defaultValue)
        {
            var value = model.Items.FirstOrDefault(a => a.HasProperty(valueName));
            return value == null
                ? defaultValue
                : value.GetValue(valueName);
        }
    }
    

    I'd be grateful for any advice and hints on where I can look next to getting this to work.

    Cheers

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 14, 2016 @ 12:55
    Dave Woestenborghs
    0

    Hi David,

    Can you post the code of your view ? We have leblender working with Autofac.

    Our leblender views have this as the first line :

    @inherits UmbracoViewPage<CustomModel>
    

    Dave

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 14, 2016 @ 12:56
    Dave Woestenborghs
    0

    I just had a look at our Autofac setup. We don't have this line ;

     builder.RegisterControllers(typeof(Lecoati.LeBlender.Extension.Controllers.LeBlenderController).Assembly);
    

    DAve

  • David Redmond 24 posts 76 karma points
    Mar 14, 2016 @ 13:03
    David Redmond
    0

    Hi Dave,

    The view also starts with the same line;

    @inherits UmbracoViewPage<Umbraco.Web.ModelsProjectListing>
    

    The rest is just your usual HTML markup. In case it's relevant, here is ProjectListing;

    public class ProjectListing : LeBlenderModel
    {
        public string Header { get; set; }
        public ICollection<ProjectListingSummary> Projects { get; set; }
    }
    

    I removed the line you mentioned in your next comment but still no luck :(

    Cheers

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 14, 2016 @ 13:07
    Dave Woestenborghs
    0

    Our model doesn't inherit form LeBlenderModel. Mayb that's where the issue is.

    Dave

  • David Redmond 24 posts 76 karma points
    Mar 14, 2016 @ 13:14
    David Redmond
    0

    Have removed that as well, unfortunately no luck either!

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 14, 2016 @ 13:15
    Dave Woestenborghs
    0

    Hi David,

    Than I'm out of options. Only have used leblender in one project

    Dave

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 16, 2017 @ 07:55
    Stefan Kip
    0

    The issue I ran into is that LeBlender uses reflection to initialize Controllers:

    var controllerInstance = (LeBlenderController)Activator.CreateInstance(controllerType);

    This will throw an exception when there's no parameterless constructor. So I added a parameterless constructor and added the dependency loading inside via the MVC DependencyResolver:

    _urlConfiguration = (IUrlConfiguration)DependencyResolver.Current.GetService(typeof(IUrlConfiguration));

  • 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