Contains methods that are useful to web applications.
Beispiel #1
0
        // Derived from http://blogs.msdn.com/b/carlosag/archive/2011/01/21/get-iis-bindings-at-runtime-without-being-an-administrator.aspx.
        // Since we want this method to work from within web applications, it's important to use WebConfigurationManager instead of ServerManager. See Dominick
        // Baier's post: http://leastprivilege.com/2007/01/09/iis7-configuration-api/.
        internal static string GetFirstBaseUrlForCurrentSite(bool iisExpress)
        {
            var assembly = getAssembly(iisExpress);
            var configurationManagerClass = assembly.GetType("Microsoft.Web.Administration.WebConfigurationManager");

            var     getSectionMethod = configurationManagerClass.GetMethod("GetSection", new[] { typeof(string), typeof(string), typeof(string) });
            dynamic sitesSection     = getSectionMethod.Invoke(null, new object[] { null, null, "system.applicationHost/sites" });

            var site             = ((IEnumerable <dynamic>)sitesSection.GetCollection()).Single(i => (string)i["name"] == HostingEnvironment.SiteName);
            var firstHttpBinding = ((IEnumerable <dynamic>)site.GetCollection("bindings")).First(i => ((string)i["protocol"]).StartsWith("http"));

            var bindingInfo = ((string)firstHttpBinding["bindingInformation"]).Separate(":", false);
            var ipAddress   = bindingInfo[0];        // Should never be empty; * means All Unassigned.
            var port        = bindingInfo[1];        // never empty
            var host        = bindingInfo[2];

            return
                (NetTools.CombineUrls(
                     "{0}://{1}:{2}".FormatWith((string)firstHttpBinding["protocol"], host.Any() ? host : ipAddress != "*" ? ipAddress : "localhost", port),
                     HttpRuntime.AppDomainAppVirtualPath));
        }
Beispiel #2
0
        /// <summary>
        /// Reports an error to the developers. The report includes the specified exception and additional information about the running program. The prefix
        /// provides additional information before the standard exception and page information.
        /// </summary>
        public static void ReportError(string prefix, Exception exception)
        {
            using (var sw = new StringWriter()) {
                if (prefix.Length > 0)
                {
                    sw.WriteLine(prefix);
                    sw.WriteLine();
                }
                if (exception != null)
                {
                    sw.WriteLine(exception.ToString());
                    sw.WriteLine();
                }

                sw.WriteLine("Application: {0}".FormatWith(ConfigurationStatics.AppName));
                sw.WriteLine("Version: {0}".FormatWith(ConfigurationStatics.AppAssembly.GetName().Version));

                if (!ConfigurationStatics.IsDevelopmentInstallation)
                {
                    sw.WriteLine();
                    sw.WriteLine("Installation: {0}".FormatWith(ConfigurationStatics.InstallationConfiguration.InstallationName));
                    sw.WriteLine("Machine: {0}".FormatWith(Tewl.Tools.NetTools.GetLocalHostName()));
                }

                if (NetTools.IsWebApp())
                {
                    // This check ensures that there is an actual request, which is not the case during application initialization.
                    if (EwfApp.Instance != null && EwfApp.Instance.RequestState != null)
                    {
                        sw.WriteLine();
                        sw.WriteLine("URL: " + AppRequestState.Instance.Url);

                        sw.WriteLine();
                        foreach (string fieldName in HttpContext.Current.Request.Form)
                        {
                            sw.WriteLine("Form field " + fieldName + ": " + HttpContext.Current.Request.Form[fieldName]);
                        }

                        sw.WriteLine();
                        foreach (string cookieName in HttpContext.Current.Request.Cookies)
                        {
                            sw.WriteLine("Cookie " + cookieName + ": " + HttpContext.Current.Request.Cookies[cookieName].Value);
                        }

                        sw.WriteLine();
                        sw.WriteLine("User agent: " + HttpContext.Current.Request.GetUserAgent());
                        sw.WriteLine("Referrer: " + NetTools.ReferringUrl);

                        User user         = null;
                        User impersonator = null;

                        // exception-prone code
                        try {
                            user         = AppTools.User;
                            impersonator = AppRequestState.Instance.ImpersonatorExists ? AppRequestState.Instance.ImpersonatorUser : null;
                        }
                        catch {}

                        if (user != null)
                        {
                            sw.WriteLine("User: {0}{1}".FormatWith(user.Email, impersonator != null ? " (impersonated by {0})".FormatWith(impersonator.Email) : ""));
                        }
                    }
                }

                EwlStatics.CallEveryMethod(
                    () => {
                    lock ( errorEmailLimiter ) {
                        errorEmailLimiter.RequestAction(
                            () => EmailStatics.SendDeveloperNotificationEmail(getErrorEmailMessage(sw.ToString())),
                            () => SendDeveloperNotification(
                                "An error occurred and the email rate-limit was reached! See the log file for this and any other errors that may occur in the near future."),
                            () => {});
                    }
                },
                    () => logError(sw.ToString()));
            }
        }
        /// <summary>
        /// Reports an error to the developers. The report includes the specified exception and additional information about the running program. The prefix
        /// provides additional information before the standard exception and page information.
        /// </summary>
        public static void ReportError(string prefix, Exception exception)
        {
            using (var sw = new StringWriter()) {
                if (prefix.Length > 0)
                {
                    sw.WriteLine(prefix);
                    sw.WriteLine();
                }
                if (exception != null)
                {
                    sw.WriteLine(exception.ToString());
                    sw.WriteLine();
                }

                if (NetTools.IsWebApp())
                {
                    // This check ensures that there is an actual request, which is not the case during application initialization.
                    if (EwfApp.Instance != null && EwfApp.Instance.RequestState != null)
                    {
                        sw.WriteLine("URL: " + AppRequestState.Instance.Url);

                        sw.WriteLine();
                        foreach (string fieldName in HttpContext.Current.Request.Form)
                        {
                            sw.WriteLine("Form field " + fieldName + ": " + HttpContext.Current.Request.Form[fieldName]);
                        }

                        sw.WriteLine();
                        foreach (string cookieName in HttpContext.Current.Request.Cookies)
                        {
                            sw.WriteLine("Cookie " + cookieName + ": " + HttpContext.Current.Request.Cookies[cookieName].Value);
                        }

                        sw.WriteLine();
                        sw.WriteLine("User agent: " + HttpContext.Current.Request.GetUserAgent());
                        sw.WriteLine("Referrer: " + NetTools.ReferringUrl);

                        User user         = null;
                        User impersonator = null;

                        // exception-prone code
                        try {
                            user         = AppTools.User;
                            impersonator = AppRequestState.Instance.ImpersonatorExists ? AppRequestState.Instance.ImpersonatorUser : null;
                        }
                        catch {}

                        if (user != null)
                        {
                            sw.WriteLine("User: {0}{1}".FormatWith(user.Email, impersonator != null ? " (impersonated by {0})".FormatWith(impersonator.Email) : ""));
                        }
                    }
                }
                else
                {
                    sw.WriteLine("Program: " + ConfigurationStatics.AppName);
                    sw.WriteLine("Version: " + ConfigurationStatics.AppAssembly.GetName().Version);
                    sw.WriteLine("Machine: " + EwlStatics.GetLocalHostName());
                }

                EwlStatics.CallEveryMethod(
                    delegate { EmailStatics.SendDeveloperNotificationEmail(getErrorEmailMessage(sw.ToString())); },
                    delegate { logError(sw.ToString()); });
            }
        }