c# - using DependencyInjection in the Configure Method -
in asp.net core web application, have myrepository
class , interface imyrepository
manages access repository (database).
each time user connects application(on site), need log entry in database.
in startup.cs, in configureservices
method do
public void configureservices(iservicecollection services) { // adds services required using options. services.addoptions(); // ... services.addsingleton<imyrepository, myrepository>();
then in configure
method need update database
public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory) { // ... imyrepository myrep = ??? // should inject somehow // ... app.useopenidconnectauthentication(new openidconnectoptions { clientid = configuration["...clientid"], authority = configuration["...aadinstance"] + configuration["...tenantid"], callbackpath = configuration["...callbackpath"], events = new openidconnectevents { onticketreceived = context => { var user = (claimsidentity)context.ticket.principal.identity; if (user.isauthenticated) { var firstname = user.findfirst(claimtypes.givenname).value; var lastname = user.findfirst(claimtypes.surname).value; var email = user.findfirst(claimtypes.email).value; var connectedon = datetime.utcnow; var userid = user.name; // // here add info in db via imyrepository // myrep.adduserinfo(userid, firstname, lastname, email, connectedon); } return task.fromresult(0); } } }); // ... }
so question how/where inject imyrepository
use in configure
method ?
modify configure
method take additional parameter imyrepository myrep
, injected long register in configureservices
.
public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory, imyrepository myrep) { ... }
wiki
Comments
Post a Comment