internal void Application_Start() { Debug.WriteLine($"Application_Start({_unique}, {GetType().AssemblyQualifiedName})"); HtmlHelper.ClientValidationEnabled = true; GlobalFilters.Filters.Add(new HandleErrorAttribute()); GlobalFilters.Filters.Add(new External.ActionParameterAlias.ParameterAliasAttributeGlobal()); GlobalFilters.Filters.Add(new WebUtils.CompressBehaviourFilter()); ModelBinders.Binders.Add(typeof(JsonDictionary), new JsonDictionaryModelBinder()); ModelBinders.Binders.DefaultBinder = new TraceModelBinder(); lock (SyncRootStart) { if (_applicationCore == null) { try { OnBeforeApplicationStart(); } catch (Exception ex) { Debug.WriteLine("OnApplicationStart: {0}", ex.Message); throw; } var physicalApplicationPath = Server.MapPath("~"); _applicationCore = new WebApplicationAspNetMvc(physicalApplicationPath, ConnectionString); _applicationCoreStarted = false; } } }
internal void Application_End(Object sender, EventArgs e) { try { OnUtils.Tasks.TasksManager.DeleteAllTasks(); } catch { } try { OnApplicationStopping(); } catch { } try { var appCore = _applicationCore; _applicationCore = null; if (appCore?.GetState() == CoreComponentState.Started) { appCore.Stop(); } } catch (Exception ex) { Debug.WriteLine($"Error stopping application core: {ex.ToString()}"); } try { OnApplicationStopped(); } catch { } try { OnUtils.Tasks.TasksManager.DeleteAllTasks(); } catch { } }
internal void Application_BeginRequest(Object sender, EventArgs e) { var isFirstRequest = (bool?)Context.GetType().GetProperty("FirstRequest", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic)?.GetValue(Context); if (isFirstRequest.HasValue && isFirstRequest.Value) { _urlFirst = Request.Url; } lock (SyncRootStart) { if (_applicationCore == null) { try { OnBeforeApplicationStart(); } catch (Exception ex) { Debug.WriteLine("OnApplicationStart: {0}", ex.Message); throw; } var physicalApplicationPath = Server.MapPath("~"); _applicationCore = new WebApplicationAspNetMvc(physicalApplicationPath, ConnectionString); _applicationCoreStarted = false; } if (!_applicationCore.IsServerUrlHasBeenSet && _urlFirst != null) { _applicationCore.ServerUrl = new UriBuilder(_urlFirst.Scheme, _urlFirst.Host, _urlFirst.Port).Uri; } if (!_applicationCoreStarted) { _applicationCore.Start(); try { OnAfterApplicationStart(); // todo _applicationCore.OnApplicationAfterStartAfterUserEvent(); _applicationCoreStarted = true; } catch (Exception ex) { _applicationCore = null; Debug.WriteLine("OnAfterApplicationStart: {0}", ex.Message); throw; } } } WebUtils.QueryLogHelper.QueryLogEnabled = true; Context.Items["TimeRequestStart"] = DateTime.Now; HttpContext.Current.SetAppCore(_applicationCore); _applicationCore.GetUserContextManager().ClearCurrentUserContext(); _requestSpecificDisposables = new Queue <IDisposable>(); try { /* * Попытка распарсить json из запроса в <see cref="Request.Form"/>. * */ if (Request.ContentType.IndexOf("application/json", StringComparison.InvariantCultureIgnoreCase) >= 0) { if (Request.InputStream.CanRead) { try { if (Request.InputStream.CanSeek) { Request.InputStream.Seek(0, SeekOrigin.Begin); } var body = Request.InputStream; var encoding = Request.ContentEncoding; var reader = new System.IO.StreamReader(body, encoding); string s = reader.ReadToEnd(); var jsonRequestObject = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(s, new Newtonsoft.Json.JsonSerializerSettings() { Error = null, }); if (jsonRequestObject != null && jsonRequestObject.Count > 0) { var oQuery = Request.Form; oQuery = (NameValueCollection)Request.GetType().GetField("_form", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request); if (oQuery != null) { var oReadable = oQuery.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance); oReadable.SetValue(oQuery, false, null); foreach (var p in jsonRequestObject) { Request.Form[p.Key] = p.Value?.ToString(); } oReadable.SetValue(oQuery, true, null); } } } finally { if (Request.InputStream.CanSeek) { Request.InputStream.Seek(0, SeekOrigin.Begin); } } } } } catch (ThreadAbortException) { throw; } catch { } try { this.OnBeginRequest(); } catch (ThreadAbortException) { throw; } catch (Exception ex) { Debug.WriteLine("OnBeginRequest: " + ex.Message); } }