Re: Resolve dependencies in IHttpHandler, pattern recommendations?

1,644 views
Skip to first unread message

Alex Meyer-Gleaves

unread,
Sep 1, 2012, 12:12:55 AM9/1/12
to aut...@googlegroups.com
Hi Andy,

What you have looks like it would work just fine, but you could refactor things a little and get support for per-request lifetime scopes. 

I would do this by registering the HTTP handler with the container and using the route handler to resolve an instance per request.

builder.Register(c => new MyServiceImpl())
.As<IMyService>()
.InstancePerHttpRequest();

builder.Register(c => new MyHandler(c.Resolve<IMyService>()))
.AsSelf()
.InstancePerHttpRequest();

Your route handler would grab the current request lifetime scope from the dependency resolver and resolve the HTTP handler from that.

public class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var lifetimeScope = AutofacDependencyResolver.Current.RequestLifetimeScope;
return lifetimeScope.Resolve<MyHandler>();
}
}

The actual HTTP handler can then have the dependent service injected into its constructor.

public class MyHandler : IHttpHandler
{
readonly IMyService _myService;

public MyHandler(IMyService myService)
{
_myService = myService;
}

public void ProcessRequest(HttpContext context)
{
_myService.DoStuff();

context.Response.Write("Hello, world!");
}

public bool IsReusable { get; private set; }
}

Now the handler and its dependencies will be cleaned up at the end of each request.

Cheers,

Alex.

On 31 August 2012 23:19, <andy.b...@gmail.com> wrote:
Hi

I'm currently using Autofac for MVC and WebApi, but would like to use the same container within a single IHttpHandler. What is the simplest and/or best pattern to use? I am currently using the following code outline.

public class MyHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        using (ILifetimeScope container = AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope())
        {
            var myService = container.Resolve<IMyService>();
                
            // do work                
        }
    }
}

public class MyHandlerRoute : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MyHandler();
    }
}

RouteTable.Routes.Add(new Route(url, new MyHandlerRoute()));

var containerBuilder = new ContainerBuilder();
var assembly = typeof(AutofacConfig).Assembly;

containerBuilder.RegisterApiControllers(assembly);
containerBuilder.RegisterControllers(assembly);

containerBuilder.RegisterType<MyServiceImpl>().As<IMyService>();

var container = containerBuilder.Build();

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

Cheers.

Andy

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/EkjcIVnAY8EJ.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.

Reply all
Reply to author
Forward
0 new messages