internal static ScriptContext/*!*/ InitWebRequest(ApplicationContext/*!*/ appContext, HttpContext/*!*/ context)
{
Debug.Assert(appContext != null && context != null);
// reloads configuration of the current thread from ASP.NET caches or web.config files;
// cached configuration is reused;
Configuration.Reload(appContext, false);
// takes a writable copy of a global configuration (may throw ConfigurationErrorsException):
LocalConfiguration config = (LocalConfiguration)Configuration.DefaultLocal.DeepCopy();
// following initialization statements shouldn't throw an exception: // can throw on Integrated Pipeline, events must be attached within HttpApplication.Init()
ScriptContext result = new ScriptContext(appContext, config, context.Response.Output, context.Response.OutputStream);
result.IsOutputBuffered = config.OutputControl.OutputBuffering;
result.ThrowExceptionOnError = true;
result.WorkingDirectory = Path.GetDirectoryName(context.Request.PhysicalPath);
if (config.OutputControl.ContentType != null) context.Response.ContentType = config.OutputControl.ContentType;
if (config.OutputControl.CharSet != null) context.Response.Charset = config.OutputControl.CharSet;
result.AutoGlobals.Initialize(config, context);
ScriptContext.CurrentContext = result;
Externals.BeginRequest();
return result;
}