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();
}
}
}
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.
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));
}
}
}
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:
I have added a new API Controller class called CountryApiController.cs to folder Controllers:
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.
It seems you have not registered the api controller with autofac. You only registered api controllers in the Umbraco Assembly
If you add that it will "probably" work :-)
dave
Thank you so much, Dave. That fixed my issue.
The Dependency Injection configuration file 'DependencyInjectionConfig.cs' now looks as follows:
is working on a reply...
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.