I'm running into a bit of a problem with the WebApi dependency resolver that the MVC one doesn't seem to be experiencing. I don't know how to summarize it, so here's the slightly longer version.
// registration code
builder.RegisterType<SessionFactory>().As<ISessionFactory>();
builder.RegisterType(c => c.Resolve<ISessionFactory>().OpenSession())
.As<ISession>().InstancePerApiRequest();
DependencyResolver.SetResolver(new AufacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// within a controller
DependencyResolver.Current.GetService<ISession>(); // resolves properly
// but
GlobalConfiguration.Configuration.DependencyResolver
.GetService(typeof(ISession));
throws a DependecyResolutionException with the message "No scope with a Tag matching 'AutofacWebRequest' is visible from the scope which the instance was requested."
The exception is thrown from within MatchingScopeLifetime.FindScope where mostNestedVisibleScope.Tag == "root" but the _matcher's lifetimeScopeTagToMatch is "AutofacWebRequest".
One thing I noticed is that normal resolution paths (i.e. via controller constructors) that find themselves into FindScope would have a mostNestedVisibleScope.Tag = "AutofacWebRequest", but GlobalConfig.DependencyResolver uses the lifetime scope from the original container, which has a Tag of "root" (no ParentLifetimeScopes).
I don't know if this is a bug or if I'm misusing the GlobalConfig DependencyResolver. If the former, let me know if you want me to file a bug report. If the later, I'd appreciate some advice on how to work around this problem.
Thank you!
--
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/-/oMmwFE5uD80J.
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.