nopSolutions/nopCommerce · critical · NullReferenceException

Can't get {nameof(IStoreContext)} implementation from the sc

Error message

Can't get {nameof(IStoreContext)} implementation from the scope

What it means

Thrown by TaskScheduler.InitializeAsync when scope.ServiceProvider.GetService<IStoreContext>() returns null, as a NullReferenceException with a descriptive message. It is resolved right after IScheduleTaskService, so if you reach this line the service scope works but IStoreContext specifically is not registered/resolvable.

Source

Thrown at src/Libraries/Nop.Services/ScheduleTasks/TaskScheduler.cs:63

    /// </summary>
    /// <returns>A task that represents the asynchronous operation</returns>
    public virtual async Task InitializeAsync()
    {
        if (!DataSettingsManager.IsDatabaseInstalled())
            return;

        if (_taskThreads.Any())
            return;

        using var scope = _serviceScopeFactory.CreateScope();
        var scheduleTaskService = scope.ServiceProvider.GetService<IScheduleTaskService>() ?? throw new NullReferenceException($"Can't get {nameof(IScheduleTaskService)} implementation from the scope");

        //initialize and start schedule tasks
        var scheduleTasks = (await scheduleTaskService.GetAllTasksAsync())
            .OrderBy(x => x.Seconds)
            .ToList();

        var storeContext = scope.ServiceProvider.GetService<IStoreContext>() ?? throw new NullReferenceException($"Can't get {nameof(IStoreContext)} implementation from the scope");

        var store = await storeContext.GetCurrentStoreAsync();

        var scheduleTaskUrl = $"{store.Url.TrimEnd('/')}/{NopTaskDefaults.ScheduleTaskPath}";
        var timeout = _appSettings.Get<CommonConfig>().ScheduleTaskRunTimeout;

        foreach (var scheduleTask in scheduleTasks)
        {
            var taskThread = new TaskThread(scheduleTask, scheduleTaskUrl, timeout)
            {
                Seconds = scheduleTask.Seconds
            };

            //sometimes a task period could be set to several hours (or even days)
            //in this case a probability that it'll be run is quite small (an application could be restarted)
            //calculate time before start an interrupted task
            if (scheduleTask.LastStartUtc.HasValue)
            {

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Check the application log for constructor exceptions from your IStoreContext implementation (GetService returns null on construction failure).
  2. Confirm IStoreContext is registered exactly once with a resolvable implementation and all its constructor dependencies are registered.
  3. Remove or fix any custom IDependencyRegistrar that re-registers IStoreContext incorrectly.
  4. Restore the default store context registration if it was inadvertently removed.

Example fix

// before: a custom registrar overwrites with an unresolvable impl
services.AddTransient<IStoreContext, MyBrokenStoreContext>();
// after: ensure MyBrokenStoreContext ctor deps are registered, or revert:
services.AddScoped<IStoreContext, WebStoreContext>();
Defensive patterns

Strategy: try-catch

Validate before calling

using var scope = _serviceScopeFactory.CreateScope();
var ctx = scope.ServiceProvider.GetService<IStoreContext>();
if (ctx is null) { logger.Critical("IStoreContext unresolved; check registrations."); return; }

Try / catch

try { await _taskScheduler.InitializeAsync(); }
catch (NullReferenceException ex) when (ex.Message.Contains(nameof(IStoreContext)))
{ logger.Critical("IStoreContext could not be constructed; inspect ctor deps.", ex); }

Prevention

When it happens

Trigger: During scheduler init, after tasks load successfully, when the DI container cannot produce IStoreContext. Often caused by a registration extension for the store context failing or a custom IStoreContext implementation whose constructor dependencies cannot be satisfied.

Common situations: Multi-store customization replaces IStoreContext with a custom implementation whose dependencies throw during construction (GetService swallows constructor exceptions and returns null); a plugin dependency registrar overwrote IStoreContext with an unresolvable type; partial deployment.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/8caf9146533c7520. Report an issue: GitHub.