Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Víctor Pma 5 posts 75 karma points
    Nov 20, 2019 @ 09:25
    VĂ­ctor Pma
    0

    issue using Umbraco v7 + LightInject

    Hi!

    I'm trying to use LightInject on Umbraco 7 but I'm having an issue that I'm not able to resolve. I'm not an expert on DI so probably I'm doing something wrong when trying to setup the project.

    The class where I configure everything looks like:

    public class UmbracoStartup : IApplicationEventHandler
    {
        void IApplicationEventHandler.OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var container = new ServiceContainer();
            RegisterCoreUmbracoServices(container);
            RegisterServices(container);
            RegisterAssembliesServices(container);
    
            container.RegisterControllers(typeof(UmbracoStartup).Assembly);
            container.RegisterApiControllers(typeof(UmbracoStartup).Assembly);
    
            container.EnableMvc();
            container.EnablePerWebRequestScope();
            container.EnableWebApi(GlobalConfiguration.Configuration);
    
            var resolver = new LightInjectWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
            DependencyResolver.SetResolver(new LightInjectMvcDependencyResolver(container));
        }
    
        private static void RegisterCoreUmbracoServices(IServiceRegistry container)
        {
            container.Register(factory => UmbracoContext.Current).RegisterInstance(new PerScopeLifetime());
            container.Register(factory => new UmbracoHelper(UmbracoContext.Current)).RegisterInstance(new PerScopeLifetime());
            container.Register(factory => ApplicationContext.Current.Services.ContentService).RegisterInstance(new PerScopeLifetime());
            container.Register(factory => ApplicationContext.Current.Services.UserService).RegisterInstance(new PerScopeLifetime());
        }
    
        private static void RegisterAssembliesServices(IServiceRegistry container)
        {
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                var controllerTypes = assembly.GetTypes().Where(t => !t.IsAbstract && typeof(IHttpController).IsAssignableFrom(t));
                foreach (var controllerType in controllerTypes)
                {
                    container.Register(controllerType, new PerScopeLifetime());
                }
            }
        }
    
        private static void RegisterServices(IServiceRegistry container)
        {
            container.Register<IMyService1, MyService1>().RegisterInstance(new PerScopeLifetime());
            container.Register<IMyService2, MyService2>().RegisterInstance(new PerScopeLifetime());
        }
    }
    

    Then I have a Controller with some defined Get methods that get the data from the Service:

    public class MyApiController : UmbracoAuthorizedJsonController
    {
        private readonly IMyService1 _myService;
    
        public MyApiController(IMyService1 myService)
        {
            _myService = myService;
        }
    
        [HttpGet]
        public IEnumerable<MyData> GetLogData()
        {
            return _myService.GetLogData();
        }
    }
    

    And finally on the Service:

    public class MyService1 : IMyService
    {
        private readonly IContentService _contentService;
        private readonly IMyService2 _myService2;
    
        public MyService1(IMyService2 myService2, IContentService contentService)
        {
            _contentService = contentService;
            _myService2 = myService2;
        }
    
        public IEnumerable<MyData> GetLogData()
        {
        ...
        //here I'm using the services to get the data
        ...
        }
    }
    

    My angular controller is calling the API every 5 seconds (controller calls the service to get the data and then return it) and everything is working properly. I'm able to display this data on a dashboard but after some time I get an error that says:

    Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.
    

    Seems like the connections are not being close after every HttpRequest and that's why I'm getting the error. I've been trying to do fix this for a couple of days with no success.

    Can anyone shine some light on this? Any help would be much appreciated.

Please Sign in or register to post replies

Write your reply to:

Draft