Value cannot be null. Parameter name: provider using DI in .net framework
03:21 10 Dec 2025

I have added Microsoft DependencyInjection nuget to a .NET framework 4.8 project and used this post to help set it up: https://stackoverflow.com/a/73414927/2739613

Part of my code looks like this:

Startup.cs

[assembly: OwinStartupAttribute(typeof(Account.Startup))]
namespace Account {
    public partial class Startup {
        public void Configuration(IAppBuilder app) {
            ServiceCollection services = new ServiceCollection();
                        //Add multiple dependencies...
                        //Create the service provider
                        IServiceProvider serviceProvider = services.BuildServiceProvider();
                        //Save it in a static field
                        DiClassStore.ServiceProvider = serviceProvider;
                        DependencyResolver.SetResolver(mvcResolver);
            }
}

Global.asax:

protected void Application_BeginRequest(object sender, EventArgs e) {           
    HttpContext.Current.Items[typeof(IServiceScope)] = DiClassStore.ServiceProvider.CreateScope();
}

protected void Application_EndRequest(object sender, EventArgs e) {
    IServiceScope scope = HttpContext.Current.Items[typeof(IServiceScope)] as IServiceScope;
    scope?.Dispose();
}

The code has been working in production for a couple of days without issue but suddenly Elmah log is filled with "Value cannot be null. Parameter name: provider" errors in the "Application_BeginRequest" method.

asp.net-mvc asp.net-web-api .net-framework-version