We help IT Professionals succeed at work.

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..

411 Views
Last Modified: 2014-05-20
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();
        }
}
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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?

Author

Commented:
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
        }
    }
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If this is happening when the session ends, are you looking to handle that gracefully, like redirect to a "session end" page?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
This solution works for me.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.