Asp.Net Classifieds Starter Kit Does Not Run in IIS7

As many have found that it has become a headache when deploying the classifieds (starter kit) application to IIS7 with all the changes made to the new operating system, Below you will find the old code and the new code to better help you understand and modify your code accordingly. If your site is a fresh down load or you have not modified the required files for this fix then you can simply copy and paste into Visual Studio or Visual Web Developer

The Error that we are thrown is "System.Web.HttpException: Request is not available in this context"

Below is the code to correct this issue/error. More information can be found here on an early blog about this actual error and what it means and again why we got the error in the first place when things work fine in previous versions of IIS (Internet Information Services)


 

App_Code/Web/ClassifiedsHttpApplication.cs

using System;

using System.Web;

using System.Threading;

using AspNet.StarterKits.Classifieds.BusinessLogicLayer;

namespace AspNet.StarterKits.Classifieds.Web

{

public class ClassifiedsHttpApplication : HttpApplication

{

private static object _settingsLock = new object();

private static object _notificationLock = new object();

private static SiteSettings _settings;

private static DateTime _lastNotificationTime;

private static string _siteUrl; private static Timer _hourlyTimer;

public class FirstRequestInitialization // Runs once only on startup and reboot.

{

private static bool s_InitializedAlready = false;

private static Object s_lock = new Object();

// Initialize only on the first request

public static void Initialize(HttpContext context)

{

if (s_InitializedAlready)

{

return;

}

lock (s_lock)

{

if (s_InitializedAlready)

{

return;

}

_siteUrl = GetSiteUrl(context);

// set-up Settings

ClassifiedsHttpApplication.ClassifiedsApplicationSettings = SiteSettings.LoadFromConfiguration();

// set-up maintenance timer

_lastNotificationTime = DateTime.Now;

TimerCallback callback = new TimerCallback(Maintenance.HourlyMaintenanceTimer);

_hourlyTimer = new Timer(callback, null, TimeSpan.Zero, TimeSpan.FromHours(1.0));

// Save //

s_InitializedAlready = true;

}

}

void Application_BeginRequest(Object source, EventArgs e)

{

HttpApplication app = (HttpApplication)source;

HttpContext context = app.Context;

// Attempt to peform first request initialization

FirstRequestInitialization.Initialize(context);

}

}

public static SiteSettings ClassifiedsApplicationSettings

{

get

{

if (_settings == null)

ClassifiedsApplicationSettings = SiteSettings.LoadFromConfiguration();

return _settings;

}

set

{

if (value == null)

throw new ArgumentNullException("ClassifiedsApplicationSettings cannot be set to null");

lock (_settingsLock)

{

_settings = value;

}

}

}

public static DateTime LastNotificationSent

{

get

{

return _lastNotificationTime;

}

set

{

lock (_notificationLock)

{

_lastNotificationTime = value;

}

}

}

public static string SiteUrl

{

get

{

return _siteUrl;

}

}

private static string GetSiteUrl(HttpContext context)

{

string baseUrl = null;

if (context != null)

{

string port = context.Request.ServerVariables["SERVER_PORT"];

if (port == null || port.Equals("80") || port.Equals("443"))

port = String.Empty;

else

port = ":" + port;

string protocol = context.Request.ServerVariables["SERVER_PORT_SECURE"];

if (protocol == null || protocol.Equals("0"))

protocol = "http://";

else

protocol = "https://";

baseUrl = protocol + context.Request.ServerVariables["SERVER_NAME"] + port + context.Request.ApplicationPath;

}

return baseUrl;

}

void Application_End(Object sender, EventArgs e)

{

}

void Application_Error(Object sender, EventArgs e)

{

}

}

} 


Global.ascx

<%@ Application Language="C#" %>

<%@ Import Namespace="AspNet.StarterKits.Classifieds.Web" %>

<script RunAt="server">

void Application_Start(object sender, EventArgs e)

{

// Code that runs on application startup

if (!Roles.RoleExists("Administrators")) Roles.CreateRole("Administrators");if (!Roles.RoleExists("Guests")) Roles.CreateRole("Guests");

}

void Application_BeginRequest(Object source, EventArgs e)

{

// Code that runs on Application startup but only runs once - Will run again on ReBoot.

HttpApplication app = (HttpApplication)source; HttpContext context = app.Context;

// Attempt to peform first request initialization

ClassifiedsHttpApplication.FirstRequestInitialization.Initialize(context);

}

 

</script>

Recieved by X-Post from = http://www.spidermaster.org/blogs/spidermaster

3 Comments

Comments have been disabled for this content.