using System.Linq; using System.Web.Mvc; using System.Configuration; using System.Web.Routing; using Neo.LegitimateLicences.Web.FrameworkExtensions; using System; using Neo.Legitimate.Web.DBUp; using Sentry; using Sentry.AspNet; using System.Net; namespace Neo.LegitimateLicences.Web { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { private IDisposable _sentry; public static void RegisterGlobalFilters(GlobalFilterCollection filters) { //filters.Add(new HandleErrorAttribute()); // Use the fixed HandleErrorAttributeWithHealthMonitoring ////filters.Add(new HandleErrorAttributeWithHealthMonitoring()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" }); routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); var ignorePaths = ConfigurationManager.AppSettings["ExternalRouteIgnores"]; if (ignorePaths != null) { foreach (var s in ignorePaths.Split(',').Where(s => !string.IsNullOrEmpty(s))) { routes.IgnoreRoute(s); } } routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Default", id = UrlParameter.Optional } // Parameter defaults ); } protected void Application_Start() { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder()); //ViewEngines.Engines.Add(new CustomViewEngine()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); // Initialize Sentry to capture AppDomain unhandled exceptions and more. _sentry = SentrySdk.Init(o => { o.AddAspNet(); o.Dsn = ConfigurationManager.AppSettings["SentryDsn"]; // When configuring for the first time, to see what the SDK is doing: o.Debug = true; // Set TracesSampleRate to 1.0 to capture 100% // of transactions for performance monitoring. // We recommend adjusting this value in production o.TracesSampleRate = 1.0; // If you are using EF (and installed the NuGet package): o.AddEntityFramework(); }); // Attempt database update, but don't crash if database is unavailable // This allows the application to start for testing purposes (e.g., eNatis API testing) try { DatabaseUpdator.UpdateDB(); } catch (Exception ex) { // Log the error but don't crash the application // This allows the application to start even if database is unavailable System.Diagnostics.Debug.WriteLine($"Database update failed during startup: {ex.Message}"); // Optionally log to a file or event log if needed } } protected void Application_Error() => Server.CaptureLastError(); protected void Application_End() { // Flushes out events before shutting down. _sentry?.Dispose(); } protected void Application_BeginRequest() { Context.StartSentryTransaction(); } protected void Application_EndRequest() { Context.FinishSentryTransaction(); } } }