Controller dependency injection with Ajax

Asked

Viewed 148 times

3

Good afternoon, I am using ASP.NET MVC 5 with Windsor and when an Ajax request calls a method in the controller, the error informing that the controller has unsatisfied dependencies. How do I work the request since the dependencies are solved in the constructor?

In Controller I declare variables:

    private readonly IMaterial _material;
    private readonly IFamilia _familia;
    private readonly IUnidade _unidade;
    private readonly IAlterarMaterial _alterarMaterial;

Then I have the Controller’s Maker:

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();
        }

And in the applications_Start

RegisterWindsor();

Class Controllerfactory

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);
                }
            }
        });
  • 1

    You could post a piece of your code to understand how this dependency injection is done?

  • Missing command using for the Windsor act.

  • And what command would that be?

  • I don’t know. You had to set up Windsor. That’s what I want to know.

  • I edited the question and put the Windsor configuration files, when I use the system normally and the requests are made in the traditional way, IE, the form page, works normally, only when I make an Ajax request that does not work.

  • Apparently it’s all right. How is the call to Ajax?

Show 1 more comment

1 answer

2


For this tutorial here, I think there are some things missing. It should be working, but apparently ASP.NET MVC gets lost somewhere at build time Controllers to the 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>()
     );
 }
  • 1

    I redid all the settings of Windsor, and did as you said in the above answer to now ta working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.