0

I want to set CookieExpiration value from DB, however, I'm getting an error:

System.InvalidOperationException: 'Unable to resolve service for type 'EMS.Services.Interfaces.ISettingsService' while attempting to activate 'EMS.Startup'.'

This is the value from DB:

_settingsService.GetTheSettings().CookieExpirationPeriod

Startup:

public ISettingsService _settingsService;
public Startup(IConfiguration configuration, ISettingsService settingsService)
{
    Configuration = configuration;
    _settingsService = settingsService;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Connection to DB
    services.AddDbContext<EMSDbContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DBConnection")));

    // Services for Sign in - Signout - Access Denied
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
    {
        options.LoginPath = "/Auth/SignIn";
        options.LogoutPath = "/Auth/SignOut";
        options.AccessDeniedPath = "/Auth/AccessDenied";
        options.ExpireTimeSpan = TimeSpan.FromHours(_settingsService.GetTheSettings().CookieExpirationPeriod);
    });
Abolfazl
  • 1,592
  • 11
  • 32
Robert
  • 15
  • 6
  • You can't inject a service into startup like that. This is why your seeing and error only ConfigureServices will configure the service and you can access them later on – AliK Jul 14 '21 at 22:54
  • @AliK is right and a simple solution is to register class as implemented services BEFORE using them – benuto Jul 14 '21 at 23:29
  • This question's accepted answer covers your needs: https://stackoverflow.com/a/32461714/2590680 – Andrey Ischencko Jul 15 '21 at 09:57

0 Answers0