I came across a problem on a recent WebAPI project where I wanted to use Autofac to inject some tenant information (i.e. derived per request) into the constructor of each controller: public class MyController : ApiController { public MyController(TenantInformation tenantInfo) { } } The problem was that the TenantInformation had to be sourced from an async API call var tenantInfo = await tenantApi.GetTenantInfoAsync(); This means that you cannot implement something like the below to register the component static void Main(string[] args) { var builder = new ContainerBuilder(); builder.Register(context = context.Resolve().GetTenantInfo()); var container = builder.Build(); var example = container.Resolve(); // -- throws 'Autofac.Core.Registration.ComponentNotRegisteredException' } On closer examination of container we can see that TenantInfo has not been registered; instead we have registered an instance of Task. We can await this but not from a constructor. One option that I briefly considered was importing the service directly into each controller and then getting the value within each async action method that required it.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to

Uncategorized