Question:
Good afternoon, I'm using ASP.NET MVC 5 with Windsor and when an Ajax request calls a method in the controller, it gives an error saying that the controller has unsatisfied dependencies. How do I make the request work since the dependencies are resolved in the constructor?
In the Controller I declare the variables:
private readonly IMaterial _material;
private readonly IFamilia _familia;
private readonly IUnidade _unidade;
private readonly IAlterarMaterial _alterarMaterial;
Then I have the Controller constructor:
public MaterialController(IMaterial material, IFamilia familia, IUnidade unidade, IAlterarMaterial alterarMaterial)
{
_material = material;
_unidade = unidade;
_familia = familia;
_alterarMaterial = alterarMaterial;
}
And then I have the method that will be called by Ajax.
public ActionResult ListarMaterial(String nome)
{
List<Material> lista = _material.Listar(nome).ToList();
return View(lista);
}
At Global.asax I have these two methods
private static void RegisterWindsor()
{
container = new WindsorContainer().Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
protected void Application_End()
{
container.Dispose();
}
E no applications_Start
RegisterWindsor();
ControllerFactory class
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel kernel;
public WindsorControllerFactory(IKernel kernel)
{
this.kernel = kernel;
}
public override void ReleaseController(IController controller)
{
kernel.ReleaseComponent(controller);
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
}
return (IController)kernel.Resolve(controllerType);
}
}
called Ajax
function Pesquisar() {
$('.corpoTbl').remove();
$.ajax({
url: "/Material/ListarMaterial",
type: "POST",
data: { nome: $('#pesquisa').val() },
success: function (data) {
if (!data.ok){
window.alert("erro");
}
else {
$('#tabela').html(data);
}
}
});
Answer:
For this tutorial here , I think some things were missing. It should be working, but apparently ASP.NET MVC gets lost somewhere when building Controllers for Ajax.
Global.asax.cs
protected void Application_Start()
{
// Troque isto
// RegisterWindsor();
// Por isto
CreateWindsorContainer();
RegisterRoutes(RouteTable.Routes);
RegisterControllers();
RegisterControllerFactory(); // Coloque este método
}
void RegisterControllerFactory() {
container.Register(
Component
.For<IControllerFactory>()
.ImplementedBy<WindsorControllerFactory>()
.LifeStyle.Singleton
);
var controllerFactory = container.Resolve<IControllerFactory>();
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
static void CreateWindsorContainer() {
container = new WindsorContainer();
// new: register the container with itself
// to be able to resolve the dependency in the ctor
// of WindsorControllerFactory
container.Register(
Component
.For<IWindsorContainer>()
.Instance(container)
);
}
static void RegisterControllers() {
container.Register(
AllTypes
.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<IController>()
);
}