Inject method in IHttpModule in WebAPI

227 views
Skip to first unread message

manna...@gmail.com

unread,
Mar 13, 2015, 10:38:09 AM3/13/15
to aut...@googlegroups.com
Hi All,

I am new in AutoFac and I am stuck with an urgent issue. It would be great if any person can help me out. I would like to inject a method in IHttpModule in a WebApi application. I have tried with various option without luck.Herewith the code snippet,

public class BasicAuthHttpModule : IHttpModule
{

public static Func<IUserBusinessLogic> _userBusinessLogic ;

public BasicAuthHttpModule(Func<IUserBusinessLogic> repository)
{
_userBusinessLogic = repository;
}
private static bool AuthenticateUser(string username,string password)
{

var userObj = _userBusinessLogic().AuthenticateUser(username, password);
//more
}
//more
}

I tried to register the method using
builder.RegisterType<UserBusinessLogic>().As<IUserBusinessLogic>();
I have also gone through the following link http://www.sapiensworks.com/blog/post/2013/03/18/Http-Module-Dependecy-Injection-with-Autofac-Gotcha.aspx and http://www.paraesthesia.com/archive/2011/02/08/dynamic-httpmodule-registration-in-asp-net-4-0.aspx/ but not able to succeed.
please help.
Regards,
Ujjwal

Travis Illig

unread,
Mar 13, 2015, 11:13:06 AM3/13/15
to aut...@googlegroups.com
IHttpModule implementations sit in the ASP.NET pipeline and don't have a "hook" into dependency injection. If you need services resolved in them, you need to use service location (e.g., CommonServiceLocator with Autofac.Extras.CommonServiceLocator).

The HttpModuleMagic package, mentioned in that first article and which I'm not very familiar with, appears to try adding DI into modules, but you'd need to contact those folks with questions since that's not our package.

The second article (which I wrote) just tells you how to dynamically add IHttpModule implementations into the pipeline without adding them to web.config. It doesn't have anything to do with adding DI support.

Hope that helps,
-T
Reply all
Reply to author
Forward
0 new messages