Skip to content

Instantly share code, notes, and snippets.

@gists-app-test
Created September 12, 2012 14:32
Show Gist options
  • Save gists-app-test/3707013 to your computer and use it in GitHub Desktop.
Save gists-app-test/3707013 to your computer and use it in GitHub Desktop.
Ninject MVC bind constructor arguments to HttpContext.Current.Session
public class Foo : IFoo
{
string _applicationName;
string _currentUserName;
public Foo(string applicationName, string currentUserName)
{
_applicationName = applicationName;
_currentUserName = currentUserName;
}
public void Bar()
{
}
}
public interface IFoo
{
void Bar();
}
public static class NinjectMVC3
{
//Other bootstrapper methods removed to be more concise
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IFoo>().To<Foo>()
.WithConstructorArgument("applicationName", "value1")
.WithConstructorArgument("currentUserName", x => GetSessionValue("UserName"));
}
static string GetSessionValue(string key)
{
if (HttpContext.Current == null) return "";
var httpSessionStateBase = new HttpContextWrapper(HttpContext.Current).Session;
if (httpSessionStateBase != null && httpSessionStateBase[key] != null)
return httpSessionStateBase[key].ToString();
return "";
}
}
@shefaliA
Copy link

I tried using this .It works fine for the first call during that request However in subsequent call the session values are returned Null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment