ASP.NET 5 dependency injection service error -
playing code using new asp.net 5 in vs 2015. had code working , after adding additional code accommodate async identity components getting:
could not resolve service of type 'clientexpress.models.clientexpresscontextseeddata' parameter 'seeder' of method 'configure' on type 'clientexpress.startup'.
have second project used same startup.cs flow including context seeding class, async components , have 0 problems it.
can't see different in workflow other names of classes.
startup.cs
public void configureservices(iservicecollection services) { services.addmvc(); var connectionstring = configuration["data:eautoconnectionstring"]; services.addentityframework() .addsqlserver() .adddbcontext<clientexpresscontext>() .adddbcontext<contactcontext>(options => options.usesqlserver(connectionstring)); services.addscoped<icontactrepository, contactrepository>(); services.addtransient<clientexpresscontextseeddata>(); } // method gets called runtime. use method configure http request pipeline. public async void configure(iapplicationbuilder app, clientexpresscontextseeddata seeder) { app.usestaticfiles(); //app.useidentity(); app.usemvc(config => { config.maproute( name: "default", template: "{controller}/{action}/{id?}", defaults: new { controller = "app", action = "index" } ); }); await seeder.ensureseeddataasync(); }
clientexpresscontextseeddata.cs
public class clientexpresscontextseeddata { private clientexpresscontext _context; private usermanager<clientexpressuser> _usermanager; public clientexpresscontextseeddata(clientexpresscontext context, usermanager<clientexpressuser> usermanager) { _context = context; _usermanager = usermanager; } /// <summary> /// /// </summary> /// <returns></returns> public async task ensureseeddataasync() { if(await _usermanager.findbyemailasync("abc@testcompany.com") == null) { //add initial users var newuser = new clientexpressuser() { username = "newuser", email = "abc@testcompany.com" }; await _usermanager.createasync(newuser, "pa$$w0rd"); } } }
if remove clienexpresscontextseeddata seeder configure method, runs of course no seeding happens.
info have found has problem showing lot when repositories used interchangeably services accidentally isn't case in situation. @ loss different or injection wrong.
clientexpresscontextseeddata
depends on usermanager<clientexpressuser>
usermanager<clientexpressuser>
not setup in configureservices
method , therefor clientexpresscontextseeddata
can't resolved.
add like:
services.addidentity<clientexpressuser, identityrole>()
Comments
Post a Comment