3

I'm pretty new to MEF, I always used to use autofac, which automatically registers all the "Web Request"-scoped objects in the life time scope of the Request, so you can always resolve HttpRequestBase and similar objects in dependencies.

Is there a way to modify the MEF container registration to be able to resolve this dependency?

[Export(typeof(ICustomerContext))]
public class WebCustomerContext : ICustomerContext
{
   private readonly HttpRequestBase request;

   [ImportingConstructor]
   public WebCustomerContext(HttpRequestBase request)
   {
            this.request = request;
   }
}
Wiebe Tijsma
  • 10,173
  • 5
  • 52
  • 68

1 Answers1

0

I think you will get the behavior you want if you export your part using the NonShared creation policy. That way, a new instance of the part will be created for each request:

[PartCreationPolicy(ComponentModel.Composition.CreationPolicy.NonShared)]
Matt
  • 548
  • 9
  • 24
  • Really, MEF doesn't create an instance per dependency by default? Let me try and get back to you... – Wiebe Tijsma Nov 12 '13 at 10:39
  • Nope, this doesn't work. I suspect something still needs to be done to register the asp.net context instances with the request life time container, but I'm not sure how – Wiebe Tijsma Nov 12 '13 at 11:09