Provides probing functions for HttpApplication entry/exit points in a bin path. Assumes the bin path is in the current app domain's probing path.
コード例 #1
0
        /// <summary>
        /// Sets up and invokes an entry point on an <see cref="HttpApplication"/> implementer found
        /// by probing the given bin path.
        /// </summary>
        /// <param name="logger">The logger to use when probing.</param>
        /// <param name="binPath">The bin path to probe.</param>
        private void SetupandInvokeEntryPoint(ILogger logger, string binPath)
        {
            if (Directory.Exists(binPath))
            {
                HttpApplicationProbe probe = new HttpApplicationProbe(logger, binPath);
                Type type = HttpApplicationProbe.FindApplicationTypes(probe.FindApplicationAssemblies()).FirstOrDefault();

                if (type != null)
                {
                    logger.Info("Found an HTTP application requiring startup: {0}.", type.FullName);
                    HttpApplication httpApplication = null;

                    try
                    {
                        httpApplication = (HttpApplication)Activator.CreateInstance(type);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "Failed to create an instance of the HTTP application {0}.", type.FullName);
                    }

                    if (httpApplication != null)
                    {
                        try
                        {
                            MethodInfo entryPoint = HttpApplicationProbe.FindEntryPoint(type);

                            if (entryPoint != null)
                            {
                                logger.Info("Invoking entry point for HTTP application {0}.", type.FullName);
                                this.InvokeEventHandler(httpApplication, entryPoint);
                            }
                            else
                            {
                                logger.Info("No entry point found for HTTP application {0}.", type.FullName);
                            }

                            this.httpApplication = httpApplication;
                            httpApplication      = null;
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex, "Failed to invoke the entry point for HTTP application {0}.", type.FullName);

                            if (ex.InnerException != null)
                            {
                                logger.Error(ex.InnerException);
                            }
                        }
                        finally
                        {
                            if (httpApplication != null)
                            {
                                httpApplication.Dispose();
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Disposes of the HTTP applications that was created as an entry point.
        /// </summary>
        private void DisposeHttpApplication()
        {
            HttpApplication httpApplication = this.httpApplication;

            this.httpApplication = null;

            if (httpApplication != null)
            {
                try
                {
                    MethodInfo method = HttpApplicationProbe.FindExitPoint(httpApplication.GetType());

                    if (method != null)
                    {
                        this.logger.Info("Invoking exit point for HTTP application {0}.", httpApplication.GetType().FullName);
                        this.InvokeEventHandler(httpApplication, method);
                    }
                    else
                    {
                        this.logger.Info("No exit point found for HTTP application {0}.", httpApplication.GetType().FullName);
                    }
                }
                finally
                {
                    httpApplication.Dispose();
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Finds a method acting as an <see cref="HttpApplication"/> entry point
        /// for the given type.
        /// </summary>
        /// <param name="type">The type to find the entry point for.</param>
        /// <returns>An entry point method, or null if none was found.</returns>
        public static MethodInfo FindEntryPoint(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type", "type cannot be null.");
            }

            return(HttpApplicationProbe.FindEventHandler(type, HttpApplicationProbe.EntryPoints));
        }
コード例 #4
0
        /// <summary>
        /// Finds all types in the given assembly collection that implement <see cref="HttpApplication"/>
        /// and contain an entry or exit point (or both).
        /// </summary>
        /// <param name="assemblies">The assemblies to find application types for.</param>
        /// <returns>A collection of <see cref="HttpApplication"/> types.</returns>
        public static IEnumerable <Type> FindApplicationTypes(IEnumerable <Assembly> assemblies)
        {
            List <Type> result = new List <Type>();

            foreach (Assembly assembly in assemblies ?? new Assembly[0])
            {
                foreach (Type type in assembly.GetTypes().Where(t => HttpApplicationProbe.IsHttpApplication(t)))
                {
                    if (HttpApplicationProbe.FindEntryPoint(type) != null ||
                        HttpApplicationProbe.FindExitPoint(type) != null)
                    {
                        result.Add(type);
                    }
                }
            }

            return(result);
        }
コード例 #5
0
        /// <summary>
        /// Sets up and invokes an entry point on an <see cref="HttpApplication"/> implementer found
        /// by probing the given bin path.
        /// </summary>
        /// <param name="logger">The logger to use when probing.</param>
        /// <param name="binPath">The bin path to probe.</param>
        private void SetupandInvokeEntryPoint(ILogger logger, string binPath)
        {
            if (Directory.Exists(binPath))
            {
                HttpApplicationProbe probe = new HttpApplicationProbe(logger, binPath);
                Type type = HttpApplicationProbe.FindApplicationTypes(probe.FindApplicationAssemblies()).FirstOrDefault();

                if (type != null)
                {
                    logger.Info("Found an HTTP application requiring startup: {0}.", type.FullName);
                    HttpApplication httpApplication = null;

                    try
                    {
                        httpApplication = (HttpApplication)Activator.CreateInstance(type);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "Failed to create an instance of the HTTP application {0}.", type.FullName);
                    }

                    if (httpApplication != null)
                    {
                        try
                        {
                            MethodInfo entryPoint = HttpApplicationProbe.FindEntryPoint(type);

                            if (entryPoint != null)
                            {
                                logger.Info("Invoking entry point for HTTP application {0}.", type.FullName);
                                this.InvokeEventHandler(httpApplication, entryPoint);
                            }
                            else
                            {
                                logger.Info("No entry point found for HTTP application {0}.", type.FullName);
                            }

                            this.httpApplication = httpApplication;
                            httpApplication = null;
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex, "Failed to invoke the entry point for HTTP application {0}.", type.FullName);

                            if (ex.InnerException != null)
                            {
                                logger.Error(ex.InnerException);
                            }
                        }
                        finally
                        {
                            if (httpApplication != null)
                            {
                                httpApplication.Dispose();
                            }
                        }
                    }
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Finds <see cref="Assembly"/> instances in the current bin path
        /// that contain types that implement <see cref="HttpApplication"/>.
        /// </summary>
        /// <returns>A collection of <see cref="Assembly"/> instances.</returns>
        public IEnumerable <Assembly> FindApplicationAssemblies()
        {
            List <Assembly> result = new List <Assembly>();

            string[] assemblyPaths = null;

            try
            {
                assemblyPaths = Directory.GetFiles(this.BinPath, "*.dll");
            }
            catch (IOException ex)
            {
                this.Logger.Error(ex);
            }
            catch (UnauthorizedAccessException ex)
            {
                this.Logger.Error(ex);
            }

            if (assemblyPaths != null)
            {
                foreach (string assemblyPath in assemblyPaths)
                {
                    AssemblyName assemblyName = null;
                    Assembly     assembly     = null;

                    try
                    {
                        assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
                    }
                    catch (Exception ex)
                    {
                        this.Logger.Error(ex);
                    }

                    if (assemblyName != null &&
                        !assemblyName.FullName.StartsWith("System.", StringComparison.OrdinalIgnoreCase) &&
                        !assemblyName.FullName.StartsWith("Microsoft.", StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            assembly = Assembly.Load(assemblyName);
                        }
                        catch (ArgumentException ex)
                        {
                            this.Logger.Error(ex);
                        }
                        catch (BadImageFormatException ex)
                        {
                            this.Logger.Error(ex);
                        }
                        catch (IOException ex)
                        {
                            this.Logger.Error(ex);
                        }
                        catch (ReflectionTypeLoadException ex)
                        {
                            this.Logger.Error(ex);
                        }
                        catch (SecurityException ex)
                        {
                            this.Logger.Error(ex);
                        }
                    }

                    try
                    {
                        if (assembly != null && assembly.GetTypes().Any(t => HttpApplicationProbe.IsHttpApplication(t)))
                        {
                            result.Add(assembly);
                        }
                    }
                    catch (ReflectionTypeLoadException ex)
                    {
                        try
                        {
                            this.Logger.Error(ex);
                        }
                        catch
                        {
                            // This can fail for various reasons...
                        }
                    }
                }
            }

            return(result);
        }