Working, coding and debugging within Visual Studio environment is very practical and mostly error free. But sooner or later you will need to push your application to a production environment (third-party hosting usually). Some development environments are harder than others. Some will have more instructions than others. My environment is ASP.NET MVC S#arp Framework using Nhibernate in IIS 7.0.
I setup a virtual directory that maps to my Publish directory. The setup I have currently mimics the one in my hosting environment. These are the steps I took along with the error messages I got.
Once my virtual directory was created in IIS7 I decided to publish and test my application. I encountered the first error:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace
below.Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.PipelineStepManager.ResumeSteps(Exception error) +929
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context,
AsyncCallback cb) +91
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,
HttpContext context) +508
The problem here is the NHibernate initialization happening in Application_Start(). Not sure if a fix has been checked-in yet but the solution for me was to use IIS7 in Classic Mode.
From IIS Manager. Select the virtual directory. Select “Advanced Settings”, change Application Pool from “DefaultAppPool” (Integrated Mode) to “Classic .NET AppPool”.
Now that changes makes the next one a requirement. If you run your application now, the message will be gone and you will most likely be able to see your home page but once you try to navigate from page to page you will get a 404 error message. The next step is to add a “Wildcard Script Map”.
Again, from IIS Manager, open “Handler Mappings”. Then “Add Wildcard Script Map…”, browse to the executable “aspnet_isapi.dll”, located in the Windows directory, NetFramework 2.0.50727. You can name it “ASP.NET MVC”.
After that your site should be up and running in your local machine and migration to your hosting environment should be a bit easier
jbland
Re. The Application_Start issue, here is code from one of my projects (i forgot where i got it, but it must have been from a SharpArch demo)
Sorry for the formatting…
///
/// Due to issues on IIS7, the NHibernate initialization cannot reside in Init() but
/// must only be called once. Consequently, we invoke a thread-safe singleton class to
/// ensure it’s only initialized once.
///
protected void Application_BeginRequest(object sender, EventArgs e)
{
NHibernateInitializer.Instance().InitializeNHibernateOnce(
() => InitializeNHibernateSession());
}
///
/// If you need to communicate to multiple databases, you’d add a line to this method to
/// initialize the other database as well.
///
private void InitializeNHibernateSession()
{
NHibernateSession.Init(
webSessionStorage, new String[]{},
Server.MapPath(“~/Configuration/NHibernate.config.xml”)
);
}
DaCoder
Thanks for the time to write your comment. I will try to test this code as soon as possible.
M Saltmarsh
Thanks for the the help.
Because I’m running 2008 64 bit make sure you go to the right version of the Framework.
Took me 2 hours beating my head on the desk to figure out.
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll
Hope this helps someone.
M
DaCoder
Excellent observation. I am sure this will help someone. Thanks for your comment
JMnet
Thanks this really helped me out!!!
DaCoder
I am glad this “old” article still helping many people, including myself, thanks for your comment.
Samant
Hi,
I’m building an ASP.NET MVC 3 app in .NET 4.0. Classic Application Pool still gave 404, perhaps because it was only meant for .NET 2.0. So, as per your solution, I should use ‘ASP.NET V4.0 classic’ in application pool?
I tried but it didn’t work either.
If this was an APP_Start issue, I tried using the following to delay NHibernate initialization:
public class NHibernateHelper
{
public static NHibernateHelper _instance = null;
public static NHibernateHelper Instance
{
get
{
if (_instance == null)
_instance = new NHibernateHelper();
return _instance;
}
set { _instance = value; }
}
public NHibernateHelper()
{
Config = GetNewConfiguration();
SessionFactory = GetNewSessionFactory();
}
}
I tried debugging in my local server and verified that the instance is only created when a call to database is made. Still, I’m facing problems and the same error.