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.
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:
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:
If I create a parameterless constructor all works well. Here is Autofac configuration:
The controller is pretty straight forward:
LeBlenderHelper is just a wrapper to get a string value from the LeBlenderModel;
I'd be grateful for any advice and hints on where I can look next to getting this to work.
Cheers
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 :
Dave
I just had a look at our Autofac setup. We don't have this line ;
DAve
Hi Dave,
The view also starts with the same line;
The rest is just your usual HTML markup. In case it's relevant, here is ProjectListing;
I removed the line you mentioned in your next comment but still no luck :(
Cheers
Our model doesn't inherit form LeBlenderModel. Mayb that's where the issue is.
Dave
Have removed that as well, unfortunately no luck either!
Hi David,
Than I'm out of options. Only have used leblender in one project
Dave
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));
is working on a reply...