Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7436579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:15:42+00:00 2026-05-29T10:15:42+00:00

In a simple word I try to create Lifetime manager for Unity framework by

  • 0

In a simple word I try to create Lifetime manager for Unity framework by using Http Session in my MVC3 project. My sample implementation of lifetime manager is:

    public class UnityPerSessionLifetimeManager : LifetimeManager
    {
        private string sessionKey;
        private HttpContext ctx;

        public UnityPerSessionLifetimeManager(string sessionKey)
        {
            this.sessionKey = sessionKey;
            this.ctx = HttpContext.Current;
        }

        public override object GetValue()
        {
            return this.ctx.Session[this.sessionKey];
        }

        public override void RemoveValue()
        {
            this.ctx.Items.Remove(this.sessionKey);
        }

        public override void SetValue(object newValue)
        {
            this.ctx.Session[this.sessionKey] = newValue;
        }
    }

In my global.asax.cs I replaced default controller factory with my own UnityControllerFactory

    public class UnityControllerFactory : DefaultControllerFactory
    {
        private IUnityContainer container;

        public UnityControllerFactory(IUnityContainer container)
        {
            this.container = container;
            this.RegisterServices();
        }

        protected override IController GetControllerInstance(RequestContext context, Type controllerType)
        {
            if (controllerType != null)
            {
                return this.container.Resolve(controllerType) as IController;
            }

            return null;
        }

        private void RegisterServices()
        {
            this.container.RegisterType<IMyType, MyImpl>(new UnityPerSessionLifetimeManager("SomeKey"));
        }
    }
}

I set breakpoints on each function of UnityPerSessionLifetimeManager class, I noticed that when controller factory tries to solve dependencies of my controller, the HttpContext.Session is actually null, so the code fails retrieve from session or save to session.

Any idea why session is null at this stage?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:15:43+00:00Added an answer on May 29, 2026 at 10:15 am

    My mistake, I should change code of UnityPerSessionLifetimeManager class to be

    public class UnityPerSessionLifetimeManager : LifetimeManager
    {
        private string sessionKey;
    
        public UnityPerSessionLifetimeManager(string sessionKey)
        {
            this.sessionKey = sessionKey;
        }
    
        public override object GetValue()
        {
            return HttpContext.Current.Session[this.sessionKey];
        }
    
        public override void RemoveValue()
        {
            HttpContext.Current.Session.Remove(this.sessionKey);
        }
    
        public override void SetValue(object newValue)
        {
            HttpContext.Current.Session[this.sessionKey] = newValue;
        }
    }
    

    because when the constructor was called to register type, session state is not ready yet and I already assigned http context of that time to a variable. But in later Get/Set functions session state is ready.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write a simple MFC - Word Automation to save for every
Direct Question: How do I create a simple hello world CUDA project within visual
I have a simple Word to Pdf converter as an MSBuild Task. The task
I want to show some bold and some simple word within the same sentence.If
I have another csv file where I am trying to do a simple word
Ive written a Word addin in VS 2008 thats pretty simple, just adds a
Is there a way to export a simple HTML page to Word (.doc format,
I have a very simple Office utility question. Open Microsoft word (2003 or 2007),
I'm trying to create a simple username/password login screen. I have the layout done,
I am trying to make a a simple script of finding the largest word

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.