Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Last active May 11, 2024 02:04
Show Gist options
  • Save davidfowl/563a602936426a18f67cd77088574e61 to your computer and use it in GitHub Desktop.
Save davidfowl/563a602936426a18f67cd77088574e61 to your computer and use it in GitHub Desktop.
ASP.NET MVC and ServiceCollection sample
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Microsoft.Extensions.DependencyInjection;
using WebApplication16;
using WebApplication16.Controllers;
[assembly: PreApplicationStartMethod(typeof(MvcApplication), "InitModule")]
namespace WebApplication16
{
public class MvcApplication : System.Web.HttpApplication
{
public static void InitModule()
{
RegisterModule(typeof(ServiceScopeModule));
}
protected void Application_Start()
{
var services = new ServiceCollection();
ConfigureServices(services);
ServiceScopeModule.SetServiceProvider(services.BuildServiceProvider());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
DependencyResolver.SetResolver(new ServiceProviderDependencyResolver());
}
private void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ScopedThing>();
services.AddTransient<HomeController>();
}
}
public class ScopedThing : IDisposable
{
public ScopedThing()
{
}
public void Dispose()
{
}
}
internal class ServiceScopeModule : IHttpModule
{
private static ServiceProvider _serviceProvider;
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
context.EndRequest += Context_EndRequest;
}
private void Context_EndRequest(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
if (context.Items[typeof(IServiceScope)] is IServiceScope scope)
{
scope.Dispose();
}
}
private void Context_BeginRequest(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
context.Items[typeof(IServiceScope)] = _serviceProvider.CreateScope();
}
public static void SetServiceProvider(ServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
}
internal class ServiceProviderDependencyResolver : IDependencyResolver
{
public object GetService(Type serviceType)
{
if (HttpContext.Current?.Items[typeof(IServiceScope)] is IServiceScope scope)
{
return scope.ServiceProvider.GetService(serviceType);
}
throw new InvalidOperationException("IServiceScope not provided");
}
public IEnumerable<object> GetServices(Type serviceType)
{
if (HttpContext.Current?.Items[typeof(IServiceScope)] is IServiceScope scope)
{
return scope.ServiceProvider.GetServices(serviceType);
}
throw new InvalidOperationException("IServiceScope not provided");
}
}
}
@davidfowl
Copy link
Author

Is session involved? I'm not sure how inactivity could affect it. Maybe add some logging and see if Begin/End request is firing on the requests that end up failing forever. Do you have a full stack trace? Are you ever trying to resolve things outside of the request? (background task etc).

@laurent-jeancler-realist
Copy link

laurent-jeancler-realist commented Jun 17, 2021

Here is the stacktrace I get :

[InvalidOperationException: IServiceScope not provided]
   GANet.IoC.ServiceProviderDependencyResolver.GetService(Type serviceType) in C:\AzureDevOpsAgents\artifacts_agent_cluster_2_1\_work\2\s\GA.Net\GANet\IoC\ServiceProviderDependencyResolver.cs:18
   System.Web.Mvc.DependencyResolverExtensions.GetService(IDependencyResolver resolver) +59
   System.Web.Mvc.SingleServiceResolver`1.GetValueFromResolver() +64
   System.Lazy`1.CreateValue() +243
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +32
   System.Lazy`1.get_Value() +14194428
   System.Web.Mvc.SingleServiceResolver`1.get_Current() +19
   System.Web.Mvc.MvcRouteHandler.GetSessionStateBehavior(RequestContext requestContext) +202
   System.Web.Mvc.MvcRouteHandler.GetHttpHandler(RequestContext requestContext) +45
   System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +219
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +223
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +220
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +94

ligne 18 is where the exception is thrown :

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using Microsoft.Extensions.DependencyInjection;

namespace GANet.IoC
{
    internal class ServiceProviderDependencyResolver : IDependencyResolver
    {
        public object GetService(Type serviceType)
        {
            if (HttpContext.Current?.Items[typeof(IServiceScope)] is IServiceScope scope)
            {
                return scope.ServiceProvider.GetService(serviceType);
            }

            throw new InvalidOperationException("IServiceScope not provided"); <----------------------------------------
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (HttpContext.Current?.Items[typeof(IServiceScope)] is IServiceScope scope)
            {
                return scope.ServiceProvider.GetServices(serviceType);
            }

            throw new InvalidOperationException("IServiceScope not provided");
        }
    }
}

So no service scope has been added to current HttpContext Items property I guess (or HttpContext.Current is null)

I will add some log to investigate the issue, thank you for your time.

I've found someone that is having a similar problem here : dnnsoftware/Dnn.Platform#4363

@davidfowl
Copy link
Author

My assumption would be that HttpContext.Curent is null? It would be good to figure that out. If there's no service scope it would mean Begin request didn't run, which doesn't seem likely. Definitely do some more debugging to see if HttpContext.Current is null (which is also strange because it look like it's running within the pipeline).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment