Error : HttpContext.Current.Request.ServerVariables
<p>my both serverName and serverPort lines are giving me error...
When I take out the servername assignment line, it gives me error in serverport assignment line.. the only common thing is the HttpContext.Current.Request...
what am I doing wrong? What can I do to work around the situation?</p>

<p>Code :</p>

<pre><code>serverName = "localhost";
            if (serverName == null || serverName.Equals("localhost"))
                serverName = HttpContext.Current.Server.MachineName.ToLower();
            serverPort = Convert.ToInt32(HttpContext.Current.Request.ServerVariables["SERVER_PORT"]);
            baseUrl = "http://" + serverName;
            if (serverPort != 80)
                baseUrl += ":" + serverPort.ToString();
</code></pre>

<p>Here is the error it gives me when i try to debug it :</p>

<pre><code>Request is not available in this context
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 321:            // Old code left in for if this fails...
Line 322:
Line 323:            serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
Line 324:            if (serverName == null || serverName.Equals("localhost"))
Line 325:                serverName = HttpContext.Current.Server.MachineName.ToLower();

//************* second source error when i remove serverName line ********/

Source Error:

Line 325:            if (serverName == null || serverName.Equals("localhost"))
Line 326:                serverName = HttpContext.Current.Server.MachineName.ToLower();
Line 327:            serverPort = Convert.ToInt32(HttpContext.Current.Request.ServerVariables["SERVER_PORT"]);
Line 328:            baseUrl = "http://" + serverName;
Line 329:            if (serverPort != 80)

//******************************************///

Source File: C:\CFG.cs    Line: 323

Stack Trace:

[HttpException (0x80004005): Request is not available in this context]
   System.Web.HttpContext.get_Request() +8806688
   labs.shared.config.CFGConstants.InitializeFromHttpContext() in C:\Users\...
   labs.shared.config.CFGConstants..ctor() in C:\Users\...
   labs.shared.config.CFGConstants.Get() in C:\Users\...
   labs.site.framework.FWStateHelper.OnApplicationStart(HttpApplication app) in C:\Users...
   labs.site.Global.Application_Start(Object sender, EventArgs e) in C:\Users\...

[HttpException (0x80004005): Request is not available in this context]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +2724290
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +128
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +188
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +295
   System.Web.HttpApplicationFactory.GetPipelineApplicationInstance(IntPtr appContext, HttpContext context) +56
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +231

[HttpException (0x80004005): Request is not available in this context]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8909915
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +85
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +333
</code></pre>
