c# - MVC Web API AutoFac Dependency Injection -
In my setup class, I have the following code (using MVC Web API Template in AutoFac and Visual Studio)
builder.RegisterType & lt; CRMUserStore & LT; IdentityUser & gt; & Gt; () In form of. & Lt; IUserLoginStore & LT; IdentityUser & gt; & Gt; () .InstancePerRequest (); Then startup. I have the following in the class.
UserManagerFactory = () = & gt; New UserManager & lt; IdentityUser & gt; (DependencyResolver.Current.GetService & lt; IUserLoginStore & lt; IdentityUser & gt; & gt; ()); This returns the tap when I try to do the above instead
UserManagerFactory = () = & gt; New User Manager & lt; IdentityUser & gt; (_ Container. Resolve & lt; IUserLoginStore & lt; IdentityUser & gt; & gt; ()); // _ Container is IContainer I get an error saying
An exception to the type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll but handled in user The code was not
Additional information: There is no scope with the tag matching 'AutofacWebRequest' in which the request was requested. It generally suggests that a per-HTTP request as a component is being requested by a SingleInstance () component registered (or any such scenario.) Always under the Container DependencyResolver.Current or ILifetimeScopeProvider. RequestLifetime Dependency Request, Never
How can I fix this?
Your IUserLoginStore form of service InstancePerRequest Which means that it can be solved only in the context of a request i.e. A lifetime 'AutofacWebRequest' Scope marked as AutoFac automatically creates a new lifetime capability for each request marked as 'AutofacWebRequest', and therefore can use the services resolved within the request Scope marked in I think that Startup.Auth is running on the scope of class MVC application and outside of any specific request. Therefore there is no access to the scope of the tag and therefore there is no scope for exceptions to the tag matching 'Autofac vabrakvancha' . If this is the case, then IUserLoginStore registration InstancePerLifetimeScope will allow it to be resolved correctly within the Startup.Auth class . However, this also changes the behavior when the application is resolved within the request to get the scotch service as well. Without seeing your code more, I can not tell that it would be an issue.
Here is a related question with a good write up:
Note - You are configured to ensure that the AutoFac document is configured as Asp.Net MVC To use AutoFac for dependency solutions () Protected Zero Application_Start () {var Builder = New ContainerBuilder ().; Builder.RegisterControllers (typeof (MvcApplication) .asembly); Var Container = Builder Build (); DependencyResolver.SetResolver (New AutofacDependencyResolver (Container));
// Other MVC setup ...
Comments
Post a Comment