When you register an instance of an object, not a type, even if you specify PropertiesAutowired , which does not take effect, since Autofac assumes that you have completed all the work you want when creating the instance. If you want properties to be connected, you need to do this in the OnActivated handler.
There are many things in this code example that will not work.
- The values ββin
SimpleAuthorizationServerProvider are not property fields , so PropertiesAutowired will not work on them. - Fields are marked as readonly, and they are never set.
- Your
UserManager<IdentityUser> registered as a lambda, but there is also a PropertiesAutowired that will not work - you can only use PropertiesAutowired for a reflection-based component (e.g. RegisterType<T> ).
Consider registering a lambda for your provider and install everything in a lambda:
builder.Register(c => { var p = new SimpleAuthorizationServerProvider(); p.repository = c.Resolve<UserManager<IdentityUser>>();
Also, keep in mind that if you register an instance (or register something as SingleInstance , then the properties will be resolved once and that is so. Therefore, if you have some dependencies InstancePerDependency or InstancePerRequest , this is not going to work like that, what do you think - they will be solved once and effectively become singles after that.
Update 1
Based on the source and updated code, it occurs to me that it would be nice if you could check out some of the Autofac docs to better understand how this works. For example, using a field in SimpleAuthorizationServerProvider shows that you cannot fully understand how injection works in Autofac or how to register things correctly so that Autofac can do your job.
For example, viewing updates ...
- Now you have the lambda registered for
SimpleAuthorizationServerProvider , but I do not see where you set the repository field there. - You do not need
PropertiesAutowired in registering SimpleAuthorizationServerProvider , because you are registering lambda and the properties will not be auto-updated (as noted earlier). - The only component that I see as registered
InstancePerRequest is AuthRepository , but, as I said, I donβt see where it is allowed or set, and that the only thing that would throw an exception that you noted. There is a FAQ regarding this exact exception that you should study.
In addition, you are showing two different versions of OAuthServerOptions that are being initialized, and it is difficult to determine which one is βrealβ.
I would recommend fairly large refactoring to allow actions to use DI correctly.
Modify SimpleAuthorizationServerProvider to stop using public fields and add them as constructor parameters so that Autofac can connect them to you.
public class SimpleAuthorizationServerProvider { public IAuthRepository Repository { get; private set; } public UserManager<IdentityUser> UserManager {get; private set; } public AuthContext Context { get; private set; } public SimpleAuthorizationServerProvider( IAuthRepository repository, UserManager<IdentityUser> userManager, AuthContext context) { this.Repository = repository; this.UserManager = userManager; this.AuthContext = context; } }
At startup, correct your login information to remove foreign objects and take advantage of Autofac auto-wiring.
public class StartUp { public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); WebApiConfig.Register(config); var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
Assuming all your code should be in order. If everything is not set - as one of the SimpleAuthorizationServerProvider properties is null, or if you get an exception because it is missing a dependency, or if you get an exception because there is no request scope ... then something else happens that you are not asked your question.
Again, please take the time to check the documents and familiarize yourself with Autofac. I think that many of the problems you face are the result of some misunderstanding as to how things connect.