Thursday, 22 August 2013

Autofac manual injection in ASP .NET Web API application with per request lifetime scope

Autofac manual injection in ASP .NET Web API application with per request
lifetime scope

I am developing ASP .NET Web API application and I need to use autofac
manual injection in custom class (not web api controller) with per request
lifetime scope. I have been searching for solutions for several days, and
have found only the suggestion to use HttpRequestMessage in web api
controller here
In web api controller:
var dependencyScope = actionContext.Request.GetDependencyScope();
var lifetimeScope = dependencyScope.GetRequestLifetimeScope();
var serviceObject = new AutofacWebApiDependencyResolver(lifetimeScope
).GetService(typeof(IMyService)) as IMyService;
But I, as far as I know, I cannot retrieve HttpRequestMessage object
(actionContext.Request) in custom class. I know this is some odd approach
and I should consider using constructor injection, but I am trying to add
autofac to already existing big project and not to break everything for
this step (this will definitely be refactored later). Could you please
help me with this issue.
Thanks for all responses!

No comments:

Post a Comment