The request is not available in this context.

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 on 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>
+3
source share
3 answers

Requestunavailable at Application_Start.

Application_Start It is executed at the launch of your web application, and this start is not associated with any page request, since the page request has not yet occurred at the moment.

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

0
source

What version of IIS are we dealing with? If it is 7 or more recent changes, this can lead to this behavior.

See this article.

+1
source

Application_Start ( Global.asax.cs) - asmx IIS 6.0

It might be better to check this in the BeginRequest method instead of Application_Start because the first request may be, but later you could call applications in a different domain and it will no longer be local.

0
source

All Articles