Yannick Hammens
Yannick Hammens

Reputation: 73

How to use dependency injection in ashx (Autofac, MVC4)

public class AutoCompleteCity : IHttpHandler
{
    public IAutoCompleteRepository AutoCompleteRepository { get; set; }

    public void ProcessRequest(HttpContext context)
    {
    }
}

public static void RegisterDependencies()
{
    var builder = new ContainerBuilder();

    builder.RegisterType<AutoCompleteRepository>().As<IAutoCompleteRepository>).SingleInstance();
    IContainer container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

AutoCompleteRepository is registered with SingleInstance, but is always null when calling ProcessRequest.

How can I fix this?

Upvotes: 0

Views: 3569

Answers (3)

Peter Lillevold
Peter Lillevold

Reputation: 33910

Your code looks good. What I suspect is that you are missing in your PropertyInjectionModule httpModules setup in web.config. The following module setup is required for Autofac to work well with ASP.Net:

  <add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"/>
  <add name="PropertyInjection" type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web"/>

Note: the setup for ASP.Net WebForms integration is described here.

Upvotes: 2

MikeSW
MikeSW

Reputation: 16348

You can try this

public class AutoCompleteCity : IHttpHandler
{

 private IAutoCompleteRepository _repo;


public IAutoCompleteRepository Repository
{
   get
   {
       if (_repo==null)
       {
            _repo=AutofacDependencyResolver.Current.RequestLifetimeScope.Resolve<IAutoCompleteRepository>();           
        }
       return _repo;
   }
   set
   {
     if (value!=null) _repo=value;
   }
 }
 public void ProcessRequest(HttpContext context)
 {
 }
 }

Upvotes: 0

0lukasz0
0lukasz0

Reputation: 3267

Try injecting your dependencies in constructor, if you want to do properties injection you should enable it with: PropertiesAutowired(), in general using autofac if you want to use property injection you need to enable it with PropertiesAutowired() configuration for class in which you want get the injected property, not in class which you want to inject.

prefered way:

public class AutoCompleteCity : IHttpHandler
{
    public IAutoCompleteRepository AutoCompleteRepository { get; set; }
    public AutoCompleteCity(IAutoCompleteRepository autoCompleteRepository) {
        AutoCompleteRepository = autoCompleteRepository;
    }

    public void ProcessRequest(HttpContext context)
    {
    }

}

or try this:

public static void RegisterDependencies()
{
    var builder = new ContainerBuilder();

    builder.RegisterType<AutoCompleteRepository>().As<IAutoCompleteRepository>()
           .SingleInstance();

    builder.RegisterType<AutoCompleteCity>()
      .As<IHttpHandler>()
      .PropertiesAutowired(); //<--!!!

    IContainer container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

EDIT: look here: https://groups.google.com/forum/#!topic/autofac/BkY4s4tusUc, to learn how to register IHttpHandler.

Upvotes: 1

Related Questions