Link to home
Start Free TrialLog in
Avatar of foxhardx
foxhardxFlag for Peru

asked on

My WeApp works fine but then 10 or 20 minutes later it fall. Because Microsoft Unity (v3.0) don't have a registered class to match with some interface..

The problem happens when the web is deployed in the server. I don't know maybe the session issue or something like that...

I'm using Unity 3.0 as IOC and Asp.net Mvc5....In my local pc all works fine...when I deploy the solution in a server with IIS7. All works fine, but 10 or 20 minutes later unity don't recognize the matches types... If I stop the server and start again the problems happens again.

--------------------------------------------------------------------------------------------------------------------------------------------
I'm using the unity controller factory:

public class UnityControllerFactory : IControllerFactory
    {
        private IUnityContainer _container;
        private IControllerFactory _innerFactory;

        public UnityControllerFactory(IUnityContainer container)
            : this(container, new DefaultControllerFactory())
        {
        }

        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            try
            {
                return _container.Resolve<IController>(controllerName);
            }
            catch (Exception ex)
            {
                return _innerFactory.CreateController(requestContext, controllerName);
            }
        }

        public void ReleaseController(IController controller)
        {
            _container.Teardown(controller);
        }

        public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }

       
        protected UnityControllerFactory(IUnityContainer container, IControllerFactory innerFactory)
        {
            _container = container;
            _innerFactory = innerFactory;
        }
--------------------------------------------------------------------------------------------------------------------------------------------

This is my UnityConfig

public static void RegisterComponents()
{
    var container = new UnityContainer();
    AppInjections.Register(container);
    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    var factory = new UnityControllerFactory(container);
    ControllerBuilder.Current.SetControllerFactory(factory);
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This is my controller:

public class MyController : Controller
{
        private GeneratorAlpha _generator;
        private FinderBeta _finder;

        public MyController (GeneratorAlpha generator, FinderBeta finder)
        {
            _generate = generator;
            _finder = finder;
        }

        public ActionResult Index()
        {
            return View();
        }
}
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

20 minutes is the default time out for session, so is it 10 minutes or 20 minutes.  

Are you using any global filters to handle session timeout?
Are you getting an exception, with full stack trace?
Avatar of foxhardx

ASKER

Hi Bob,

I think the problem is caused because I have load unity configuration in global.asax. Then, when the web is recycled, that section does not load anymore. I guess I have move this line: "UnityConfig.RegisterComponents();" to another event what load in each request. What do you think?

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            UnityConfig.RegisterComponents(); // <-------------------------------- Unity Registration
        }
    }
If this is happening when the session ends, are you looking to handle that gracefully, like redirect to a "session end" page?
ASKER CERTIFIED SOLUTION
Avatar of foxhardx
foxhardx
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This solution works for me.