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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:44:32+00:00 2026-05-20T18:44:32+00:00

My code is as fallows below. My issue is the Session is not injected

  • 0

My code is as fallows below. My issue is the Session is not injected and my session remains Null. What is wrong here? I am using ninject 2.2

    public class SessionNinjectModule : NinjectModule
{
    public override void Load()
    {
        this.Bind<ISession>().ToMethod(x => MvcApplication.SessionFactory.GetCurrentSession());
        this.Bind(typeof(IRepository<>)).To(typeof(NHRepository<>)).InRequestScope(); 

    }
}

    public class MvcApplication : NinjectHttpApplication
{

    public static ISessionFactory SessionFactory = CreateSessionFactory();
    public MvcApplication()
    {
        this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
        this.EndRequest += new EventHandler(MvcApplication_EndRequest);
    }

    void MvcApplication_EndRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Unbind(SessionFactory).Dispose();
    }

    void MvcApplication_BeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(SessionFactory.OpenSession());
    }

    private static ISessionFactory CreateSessionFactory()
    {
        string connectionString = "Data Source=VARTAN-PC\\SQLEXPRESS;Initial Catalog=mdlivenh;Integrated Security=SSPI;";
        return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)).ExposeConfiguration(c => c.SetProperty("current_session_context_class", "thread_static"))
            .Mappings(m =>
            {
                // Include both standard NHibernate mapping files and Fluent NHibernate mapping files
                //m.HbmMappings.AddFromAssemblyOf<User>();
                m.FluentMappings.AddFromAssemblyOf<NHMDLive.Models.Mappings.StudyMap>();
            }).BuildSessionFactory();

    }

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected override IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        return kernel;
    }

    /// <summary>
    /// Creates the controllerfactory (I think)
    /// </summary>
    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
    public class NHRepository<T> : IQueryable<T>, IRepository<T> where T : class, IBaseEntity
{
    private ISession _session;

    [Inject]
    public ISession session
    {
        get { return _session; }
        set { _session = value; }
    }

    #region IRepository Members

    virtual public T Get(object id)
    {
        return TryCatch(() => session.Get<T>(id));
    }}

Usage: NHRepository repository = new NHRepository();

  • 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-20T18:44:33+00:00Added an answer on May 20, 2026 at 6:44 pm

    Looks fine. How are you instantiating your NHRepository? Be sure to let Ninject create it for you, otherwise it won’t have a chance to set the session property.

    Example:

    // Tell Ninject to give us an NHRepository.
    var repository = ninjectKernel.Get<NHRepository<Foo>>(); 

    Alternately, instead of using the [Ninject] attribute, the NHRepository itself could grab it’s session from the kernel:

    public ISession Session
    {
        get { return ninjectKernel.Get<ISession>(); }
    }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code doesn't compile. package varargspkg; public class Main { public static void
http://ruby.railstutorial.org/chapters/static-pages#code:pages_controller_spec_title My code is as follows. When uncommented the test wont run. Below is
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
I am attempting to use the MediaStore.ACTION_VIDEO_CAPTURE in my app, see code below. However,
Ruby noob here. Any help with a little issue I'm having would be appreciated.
I have a strange issue with my code I can't seem to figure out.
I'm trying to insert multiple rows using transaction-bind through PHP PDO. Below is my
I use the code below on a thread (showing a progress dialog in ui)
Below I have the code on my site, yet when I click on a
I've this strange issue. I've an xml string which looks like below - <key><int>5</int></key><value><int>10</int>

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.