internal static ICollection <MethodInfo> GetPreStartInitMethodsFromAssemblyCollection( IEnumerable <Assembly> assemblies ) { List <MethodInfo> methods = new List <MethodInfo>(); foreach (Assembly assembly in assemblies) { PreApplicationStartMethodAttribute[] attributes = null; try { attributes = (PreApplicationStartMethodAttribute[])assembly.GetCustomAttributes( typeof(PreApplicationStartMethodAttribute), inherit: true ); } catch { // GetCustomAttributes invokes the constructors of the attributes, so it is possible that they might throw unexpected exceptions. // (Dev10 bug 831981) } if (attributes != null && attributes.Length != 0) { Debug.Assert(attributes.Length == 1); PreApplicationStartMethodAttribute attribute = attributes[0]; Debug.Assert(attribute != null); MethodInfo method = null; // Ensure the Type on the attribute is in the same assembly as the attribute itself if ( attribute.Type != null && !String.IsNullOrEmpty(attribute.MethodName) && attribute.Type.Assembly == assembly ) { method = FindPreStartInitMethod(attribute.Type, attribute.MethodName); } if (method != null) { methods.Add(method); } // No-op if the attribute is invalid /* * else { * throw new HttpException(SR.GetString(SR.Invalid_PreApplicationStartMethodAttribute_value, * assembly.FullName, * (attribute.Type != null ? attribute.Type.FullName : String.Empty), * attribute.MethodName)); * } */ } } return(methods); }
public void PreApplicationStartMethodAttributeTest() { Assembly assembly = typeof(Controller).Assembly; object[] attributes = assembly.GetCustomAttributes(typeof(PreApplicationStartMethodAttribute), true); Assert.AreEqual(1, attributes.Length, "{0} does not have a PreApplicationStartMethodAttribute. ", assembly.FullName); PreApplicationStartMethodAttribute preAppStartMethodAttribute = (PreApplicationStartMethodAttribute)attributes[0]; Type preAppStartMethodType = preAppStartMethodAttribute.Type; Assert.AreEqual(typeof(PreApplicationStartCode), preAppStartMethodType, "The PreApplicationStartMethod type should be PreApplicationStartCode"); }
public static void TestPreStartInitMethodLocation(Assembly assembly) { var attributes = (PreApplicationStartMethodAttribute[])assembly.GetCustomAttributes(typeof(PreApplicationStartMethodAttribute), inherit: true); if (attributes != null && attributes.Length != 0) { PreApplicationStartMethodAttribute attribute = attributes[0]; MethodInfo method = null; // They must be in the same assembly! if (attribute.Type != null && !String.IsNullOrEmpty(attribute.MethodName) && attribute.Type.Assembly == assembly) { method = FindPreStartInitMethod(attribute.Type, attribute.MethodName); } if (method == null) { throw new HttpException("Couldn't find attribute"); } } }
public void ProcessRequest(HttpWorkerRequest wr = null) { string error = null; inUnhandledException = false; var ctx = HttpContext.Current?.Application; HttpApplication app = null; // StateRuntime state = null; string[] args = Environment.GetCommandLineArgs(); if (Debugger.IsAttached && args.Length >= 5) { PreApplicationStartMethodAttribute[] attributes = null; string dll = null; Assembly assembly = null; try { dll = args[4]; assembly = Assembly.LoadFrom(this.HostPath + "bin/" + dll); attributes = (PreApplicationStartMethodAttribute[])assembly .GetCustomAttributes(typeof(PreApplicationStartMethodAttribute), inherit: true); } catch {} PreApplicationStartMethodAttribute att = attributes.FirstOrDefault(); if (att != null) { var type = att.Type; var m = att.MethodName; var method = type.GetMethod(m, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase, binder: null, types: Type.EmptyTypes, modifiers: null); if (method != null) { method.Invoke(null, BindingFlags.Static, null, null, CultureInfo.CurrentCulture); } // Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) } } /* if (ctx == null) * { * state = new StateRuntime(); * if (HttpContext.Current != null) * app = HttpContext.Current.ApplicationInstance as HttpApplication; * // HttpApplicationFactory.GetApplicationInstance(HttpContext.Current) * } */ HttpResponse response = null; var request = HttpContext.Current?.Request; try { AssertFileAccessible(); response = HttpContext.Current?.Response; HttpRuntime.ProcessRequest(this); IHttpHandler hand = AppDomain.CurrentDomain.GetData("web:app") as IHttpHandler; app = hand as HttpApplication; /* if (hand != null) { * response = HttpContext.Current?.Response; * hand.ProcessRequest(new HttpContext(request, response)); * } */ } catch (HttpException ex) { inUnhandledException = true; error = ex.GetHtmlErrorMessage(); } catch (Exception ex) { inUnhandledException = true; var hex = new HttpException(400, "Bad request", ex); error = hex.GetHtmlErrorMessage(); } if (!inUnhandledException) { return; } if (error.Length == 0) { error = String.Format(DEFAULT_EXCEPTION_HTML, "Unknown error"); } try { SendStatus(400, "Bad request"); SendUnknownResponseHeader("Connection", "close"); SendUnknownResponseHeader("Date", DateTime.Now.ToUniversalTime().ToString("r")); Encoding enc = Encoding.UTF8; byte[] bytes = enc.GetBytes(error); SendUnknownResponseHeader("Content-Type", "text/html; charset=" + enc.WebName); SendUnknownResponseHeader("Content-Length", bytes.Length.ToString()); SendResponseFromMemory(bytes, bytes.Length); FlushResponse(true); } catch (Exception ex) { // should "never" happen Logger.Write(LogLevel.Error, "Error while processing a request: "); Logger.Write(ex); throw; } }