3
Exception Details: System.Web.HttpException: Request is not available in this context

Source Error:

Line 7:     Private Sub Application_Start(sender As Object, e As EventArgs)
Line 8:     ' Caching the tracker image in memory
Line 9:     Dim trackerImg As Byte() = File.ReadAllBytes(Context.Request.MapPath(ConfigurationManager.AppSettings("SD_Tut_ImageFileLocation")))
Line 10:    Application(ConfigurationManager.AppSettings("SD_Tut_ImageFileKeyName")) = trackerImg

Error is in line 9

This is the Global.asax Code

<%@ Application Language="vb" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Stardeveloper.Tutorial" %>

<script runat="server">
   Private Sub Application_Start(sender As Object, e As EventArgs)
    ' Caching the tracker image in memory
    Dim trackerImg As Byte() = File.ReadAllBytes(Context.Request.MapPath(ConfigurationManager.AppSettings("SD_Tut_ImageFileLocation")))
    Application(ConfigurationManager.AppSettings("SD_Tut_ImageFileKeyName")) = trackerImg

    ' Creating a new request queue collection
    Dim queueCapacity As Integer = Convert.ToInt32(ConfigurationManager.AppSettings("SD_Tut_TrackerRequestsToCache"))
    Dim trackerReqQueue As New Queue(Of TrackerRequest)(queueCapacity)
    Application(ConfigurationManager.AppSettings("SD_Tut_TrackerCachedRequestsKeyName")) = trackerReqQueue
End Sub

Private Sub Application_End(sender As Object, e As EventArgs)
    ' Storing the queued tracker reqs to database
    Dim trackerReqQueue As Queue(Of TrackerRequest) = DirectCast(Application(ConfigurationManager.AppSettings("SD_Tut_TrackerCachedRequestsKeyName")), Queue(Of TrackerRequest))
    Tracker.FlushRequestQueueCache(trackerReqQueue)
End Sub

</script>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Monodeep
  • 1,392
  • 1
  • 17
  • 39

3 Answers3

1

What version of IIS are we dealing with? If it's 7 or later there were changes made that can result in this behavior.

See this article.

Khepri
  • 9,547
  • 5
  • 45
  • 61
0

Request is not available on Application_Start.

Application_Start is executed when your web application starts and this start is not associated with any page request, as page request is not happened yet at this point.

For your need, you can use Server.MapPath() instead.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • when i run the website in my local machine i get no errors...but after hosting it ...i am getting this error. – Monodeep May 28 '11 at 05:11
  • Why don't you just replace `Context.Request.MapPath` with `Server.MapPath`? As far as I can see you don't really need Request. – Alex Aza May 28 '11 at 05:14
  • now i get this error....`System.Data.SqlClient.SqlException: Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. Please make sure the user has a local user profile on the computer. The connection will be closed.` – Monodeep May 28 '11 at 05:20
  • From the code that you provided I can't see how you access database. What line throws exception? – Alex Aza May 28 '11 at 05:25
  • I assume that your application settings have 'User' scope instead of 'Application'. In config file change settings scope. – Alex Aza May 28 '11 at 05:52
0

Find Here how to get Application_Start (in Global.asax.cs) to be called before requests in asmx web service on IIS 6.0

It might be more appropriate to check this in the BeginRequest method instead of the Application_Start because the first request might be local but later you could call the application on some other domain and it will no longer be local.

Community
  • 1
  • 1
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
  • when i run the website in my local machine i get no errors...but after hosting it ...i am getting this error. – Monodeep May 28 '11 at 05:11