Reputation: 504
I have an MVC4 solution which has a separate project for the controllers. This all works fine.
I have now integrated Castle Windsor IOC as I have done many times before in single project solutions, and it seems to have registered properly as when my web site starts it calls into the HomeController with the repository dependencies just fine.
However all my controllers inherit a BaseController class (which inherits Controller naturally) and in the constructor of the BaseController I am accessing the Session object.
Post Castle Windsor this is no longer working. The Session object is null.
Here is the code from BaseController
protected BaseController(ICustomerRepository customerRepository, ISystemSettingsRepository systemSettingsRepository)
{
_customerRepository = customerRepository;
_systemSettingsRepository = systemSettingsRepository;
_timeoutMinutes = _systemSettingsRepository.GetSystemSettings().TimeoutMinutes;
Session["KYCWarning"] = _customerRepository.GetMessage("KYCWarning");
}
The reason I am storing this message in a Session variable is that it is used in a condition statement in a cshtml file
@{
var kycWarning = Session["KYCWarning"].ToString();
var settingShowKYCReminder = ConfigurationManager.AppSettings["ShowKYCReminder"];
var showKYCReminder = !string.IsNullOrWhiteSpace(settingShowKYCReminder) && (bool.Parse(settingShowKYCReminder));
}
@if (showKYCReminder && Session["CurrentSession"] != null && !string.IsNullOrWhiteSpace(kycWarning) && !((SessionModel)Session["CurrentSession"]).Customer.KycAuthorised)
{
<div class="">
<div class="col-md-12 WarningMessageBox">
<span>@kycWarning</span>
</div>
</div>
}
This is just one example, there are other uses of Session within the controllers and I expect they will fail to if the code were reachable past this exception.
Can anyone suggest why this is happening, and a solution? I understood that a controller that inherits the Controller class would always have access to Session.
Thank you.
Upvotes: 1
Views: 155
Reputation: 504
Ok the problem was with me, not Castle.Windsor!
I was accessing the Session in the constructor, which is of course called by Castle Windsor at a point where the Session object isn't necessarily available.
I moved the code setting the session variables into another part of the application and all is well with the other code which uses Session in the controllers.
Upvotes: 1