1

I have my webservice running on IIS in Integrated mode. In this mode, HttpContext.Request throws exception, when is called from Application_Start method.

// app is HttpApplication
var serverName = app.Context.Request.ServerVariables["SERVER_NAME"];

The above statement would throw System.Web.HttpException: Request is not available in this context

Is there somewhere else, where I can retrieve ServerVariables from, within Application_Start?

Tschareck
  • 4,071
  • 9
  • 47
  • 74
  • 1
    App start may be called when there isn't a request, that's why you don't have access to a request object. – DavidG Nov 08 '18 at 10:44
  • 1
    Well documented by Microsoft https://learn.microsoft.com/en-us/iis/application-frameworks/building-and-running-aspnet-applications/aspnet-20-breaking-changes-on-iis#16-it-is-not-possible-to-access-the-request-through-the-httpcontextcurrent-property-in-applicationstart-in-globalasax – Lex Li Nov 08 '18 at 13:34
  • Why are you trying to get SERVER_NAME when the application starts? AFAIK SERVER_NAME reflects the name in the request URL without the port number. You could potentially have multiple host names coming into one site, so it would be good to explain what you are looking to achieve. – Brian Clink Nov 08 '18 at 14:44

1 Answers1

0

You can use HostingEnvironment

System.Web.Hosting.HostingEnvironment

It has a lot of properties you can use

VDWWD
  • 35,079
  • 22
  • 62
  • 79