Copied to clipboard

Flag this post as spam?

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


  • Walter Lockhart 2 posts 82 karma points
    Dec 27, 2016 @ 10:54
    Walter Lockhart
    0

    Umbraco 7 and Autofac -- Make sure that the controller has a parameterless public constructor.

    I am trying to use Autofac with my Umbraco 7 MVC Project (.NET Framework 4.5.2).

    I have installed Autofac.Mvc5 and Autofac.WebApi2.

    I have configured Dependency Injection as follows:

    using System.Reflection;
    using System.Web.Http;
    using System.Web.Mvc;
    using Autofac;
    using Autofac.Integration.Mvc;
    using Autofac.Integration.WebApi;
    using Umbraco.Web;
    using Data;
    using Data.Interface;
    using Service;
    using Service.Interface;
    
    namespace Website.App_Start
    {
        public class DependencyInjectionConfig
        {
            public static void RegisterDependencies()
            {
                var builder = new ContainerBuilder();
                builder.RegisterControllers(Assembly.GetExecutingAssembly());
                builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    
                // Register Mapper Service, Geography Service, etc.
    
                builder.Register(c => new MapperService()).As<IMapperService>().SingleInstance();
                builder.RegisterType<CustomDbContext>().As<IUnitOfWork>().InstancePerRequest();
                builder.RegisterType<GeographyService>().As<IGeographyService>().InstancePerLifetimeScope();
    
                var container = builder.Build();
                var resolver = new AutofacWebApiDependencyResolver(container);
                GlobalConfiguration.Configuration.DependencyResolver = resolver;
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            }
        }
    }
    

    I have added a new API Controller class called CountryApiController.cs to folder Controllers:

    using System.Collections.Generic;
    using System.Web.Mvc;
    using Data.Interface;
    using Domain.Model;
    using Service.Interface;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.WebApi;
    
    namespace Website.Controllers
    {
        // API Call: /umbraco/CountryList/CountryApi/GetCountries
        [PluginController("CountryList")]
        public class CountryApiController : UmbracoApiController
        {
            private IUnitOfWork DataContext { get; set; }
            private IGeographyService GeographyService { get; set; }
            private IMapperService MapperService { get; set; }
    
            public CountryApiController(IUnitOfWork dataContext, IGeographyService geographyService, IMapperService mapperService)
            {
                DataContext = dataContext;
                GeographyService = geographyService;
                MapperService = mapperService;
            }
    
            [HttpGet]
            public IEnumerable<Country> GetCountries()
            {
                return GeographyService.GetCountries();
            }
        }
    }
    

    When I Build and Run the solution and go to: http://localhost:59761/umbraco/CountryList/CountryApi/GetCountries

    I get the following exception message:

    An error occurred when trying to create a controller of type 'CountryApiController'. Make sure that the controller has a parameterless public constructor.

    Type 'Website.Controllers.CountryApiController' does not have a default constructor.

    Could someone please help me resolve this exception. Thanks.

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Dec 28, 2016 @ 11:58
    Dave Woestenborghs
    100

    It seems you have not registered the api controller with autofac. You only registered api controllers in the Umbraco Assembly

    builder.RegisterApiControllers(typeof(Website.Controllers.CountryApiController ).Assembly);
    

    If you add that it will "probably" work :-)

    dave

  • Walter Lockhart 2 posts 82 karma points
    Dec 28, 2016 @ 22:19
    Walter Lockhart
    0

    Thank you so much, Dave. That fixed my issue.

    The Dependency Injection configuration file 'DependencyInjectionConfig.cs' now looks as follows:

    using System.Reflection;
    using System.Web.Http;
    using System.Web.Mvc;
    using Autofac;
    using Autofac.Integration.Mvc;
    using Autofac.Integration.WebApi;
    using Umbraco.Web;
    using Data;
    using Data.Interface;
    using Service;
    using Service.Interface;
    
    namespace Website.App_Start
    {
        public class DependencyInjectionConfig
        {
            public static void RegisterDependencies()
            {
                var builder = new ContainerBuilder();
    
                // Register Umbraco Context, MVC Controllers and API Controllers.
                builder.Register(c => UmbracoContext.Current).AsSelf();
                builder.RegisterControllers(Assembly.GetExecutingAssembly());
                builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
                builder.RegisterApiControllers(typeof(Controllers.CountryApiController).Assembly);
    
                // Register Mapper Service, Geography Service, etc.
                builder.Register(c => new MapperService()).As<IMapperService>().SingleInstance();
                builder.RegisterType<CustomDbContext>().As<IUnitOfWork>().InstancePerRequest();
                builder.RegisterType<GeographyService>().As<IGeographyService>().InstancePerLifetimeScope();
    
                var container = builder.Build();
                var resolver = new AutofacWebApiDependencyResolver(container);
                GlobalConfiguration.Configuration.DependencyResolver = resolver;
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft