using DependencyInjection in the Configure Method
Asked Answered
L

1

6

In an ASP.NET CORE web application, I have a MyRepository class and an Interface IMyRepository that manages the access to the repository (database).

Each time a user connects to the application(on the site), I need to log an entry in the database.

In Startup.cs, in the ConfigureServices method I do

public void ConfigureServices(IServiceCollection services)
{
    // Adds services required for using options.
    services.AddOptions();
    // ...
    services.AddSingleton<IMyRepository, MyRepository>();

Then in the Configure method I need to update the 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 the info in the DB via IMyRepository
                // 
                myRep.AddUserInfo(userId, firstName, lastName, email, connectedOn);
            }

            return Task.FromResult(0);
        }
    }
});

// ...

}

So my question is how/where do I inject the IMyRepository for use it in that Configure method ?

Lunge answered 22/8, 2017 at 15:39 Comment(0)
D
7

Modify your Configure method to take an additional parameter IMyRepository myRep, it will be injected for you as long as you register it in ConfigureServices.

public void Configure(IApplicationBuilder app, 
                      IHostingEnvironment env, 
                      ILoggerFactory loggerFactory, 
                      IMyRepository myRep) { ... }
Dittography answered 22/8, 2017 at 15:42 Comment(5)
yes because I think ConfigureServices method runs first before ConfigureBoulanger
so simple?! :) what if the OnTicketsRecieved is a function defined outside the Configure method?Lunge
@SergeI I think then you have to use constructor injection and pass an instance of IMyRepository to your constructor . For example in controller.Boulanger
humm... the problem that actually I don't know how to link the "connected event" to a controller's action... I use already constructor injection in controllers, but this time I don't know how to move that code in controller...Lunge
Thanks, your solution helped me.Tolerant

© 2022 - 2024 — McMap. All rights reserved.