Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How may I inject something in the Global.asax ? #16

Closed
vtortola opened this issue Apr 1, 2011 · 1 comment
Closed

How may I inject something in the Global.asax ? #16

vtortola opened this issue Apr 1, 2011 · 1 comment

Comments

@vtortola
Copy link

vtortola commented Apr 1, 2011

I'm starting a web application with MVC3 and Ninject. There is one dependency that I also need in the Global.asax file that needs to be a singleton.

I thought it should be like this:

public class MvcApplication : NinjectHttpApplication
{
IUserAuthentication _auth;

public MvcApplication()
{
    base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
}

protected override IKernel CreateKernel()
{
    var _kernel = new StandardKernel(new SecurityModule());
    _auth = _kernel.Get<IUserAuthentication>();

    return _kernel;
}

void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
{
    _auth.ToString();
}

But then I saw that _auth is null when MvcApplication_AuthenticateRequest is called.

Then I tried like this:

public class MvcApplication : NinjectHttpApplication
{
ItUserAuthentication _auth;
IKernel _kernel;

public MvcApplication()
{
    _kernel = new StandardKernel(new SecurityModule());
    _auth = _kernel.Get<IUserAuthentication>();
    base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
}

protected override IKernel CreateKernel()
{
    return _kernel;
}

void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
{
    _auth.ToString();
}

But now I can see that the constructor is being called several times, therefore I will have several IKernel, and I guess that singleton instances won't be so singleton in my app scope.

How should I do it? Using a static variable?

I put this question also in http://stackoverflow.com/questions/5512040/ninject-ing-a-dependency-in-global-asax

@remogloor
Copy link
Member

implemented in 4284ad2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants