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 9148791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:22:01+00:00 2026-06-17T11:22:01+00:00

I have a repository that needs to be created per request. Now there is

  • 0

I have a repository that needs to be created per request. Now there is a singleton caching object that needs to use that repository to fill itself with data, but this object is initialized in the Application_Start event, therefore there is not request context.

Which would be the best way of accomplish this with Ninject?

Thanks.

  • 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-06-17T11:22:02+00:00Added an answer on June 17, 2026 at 11:22 am

    As current HttpContext is missing on app startup (in IIS7 integration mode) Ninject will definitely treat object binded InRequestScope as it would be in InTransientScope when you try to inject dependency on app startup. As we can see in Ninject’s (2.2) source code:

    /// <summary>
    /// Scope callbacks for standard scopes.
    /// </summary>
    public class StandardScopeCallbacks
    {
        /// <summary>
        /// Gets the callback for transient scope.
        /// </summary>
        public static readonly Func<IContext, object> Transient = ctx => null;
    
        #if !NO_WEB
        /// <summary>
        /// Gets the callback for request scope.
        /// </summary>
        public static readonly Func<IContext, object> Request = ctx => HttpContext.Current;
        #endif
    }
    

    HttpContext.Current will be null at app startup, so the InRequestScope binded object will be treated on app startup the same way as it would be binded InTransientScope.

    So you can have in your global.asax:

    protected override Ninject.IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<RequestScopeObject>().ToSelf().InRequestScope();
        kernel.Bind<SingletonScopeObject>().ToSelf().InSingletonScope();
        return kernel;
    }
    
    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
        // init cache
        this.Kernel.Get<SingletonScopeObject>().Init();
    }
    

    But you will have to do cleanup after use of RequestScopeObject (e.g. Dispose() it, if it implements IDisposable).

    public class SingletonScopeObject
    {
        private string cache;
        private RequestScopeObject requestScopeObject;
        public SingletonScopeObject(RequestScopeObject requestScopeObject)
        {
            this.requestScopeObject = requestScopeObject;
        }
    
        public void Init()
        {
            cache = this.requestScopeObject.GetData();
            // do cleanup
            this.requestScopeObject.Dispose();
            this.requestScopeObject = null;
        }
    
        public string GetCache()
        {
            return cache;
        }
    }
    

    Another approach

    Bind your RequestScopeObject both InRequestScope and InSingletonScope utilizing the conditional binding like this:

    kernel.Bind<SingletonScopeObject>()
          .ToSelf()
          .InSingletonScope()
          .Named("singletonCache");
    // as singleton for initialization
    kernel.Bind<RequestScopeObject>()
          .ToSelf()
          .WhenParentNamed("singletonCache")
          .InSingletonScope();
    // per request scope binding
    kernel.Bind<RequestScopeObject>()
          .ToSelf()
          .InRequestScope();
    

    The cleanup after initialization remains the same.

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

Sidebar

Related Questions

I have created Generic Repository and I have two entities that i need to
I have created a repository that is returning data from my database using Entity
I have a repository that looks something like this: foo/ subrepo/ a_file_in_subrepo another.ext unrelated_file
I have a mercurial repository that is not accessible by Http. I can't figure
I have a git repository that I'm trying to delete all files and folders
I have a Mercurial repository that I can see just fine if I navigate
I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
I have a remote SVN repository that I connect to via HTTPS. The repository
I have a central git repository that everyone pushes to for testing and integration,
I have some old branches in my git repository that are no longer under

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.