GetEntryAssembly Web应用程序 [英] GetEntryAssembly for web applications

查看:419
本文介绍了GetEntryAssembly Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Assembly.GetEntryAssembly()不为web应用程序的工作。

Assembly.GetEntryAssembly() does not work for web applications.

但是......我真的很需要类似的东西。
我与一些深层嵌套code,它在Web和非Web应用程序使用的工作。

But... I really need something like that. I work with some deeply-nested code that is used in both web and non-web applications.

我目前的解决办法是浏览堆栈跟踪找到第一个叫组装。

My current solution is to browse the StackTrace to find the first called assembly.

/// <summary>
/// Version of 'GetEntryAssembly' that works with web applications
/// </summary>
/// <returns>The entry assembly, or the first called assembly in a web application</returns>
public static Assembly GetEntyAssembly()
{
    // get the entry assembly
    var result = Assembly.GetEntryAssembly();

    // if none (ex: web application)
    if (result == null)
    {
        // current method
        MethodBase methodCurrent = null;
        // number of frames to skip
        int framestoSkip = 1;


        // loop until we cannot got further in the stacktrace
        do
        {
            // get the stack frame, skipping the given number of frames
            StackFrame stackFrame = new StackFrame(framestoSkip);
            // get the method
            methodCurrent = stackFrame.GetMethod();
            // if found
            if ((methodCurrent != null)
                // and if that method is not excluded from the stack trace
                && (methodCurrent.GetAttribute<ExcludeFromStackTraceAttribute>(false) == null))
            {
                // get its type
                var typeCurrent = methodCurrent.DeclaringType;
                // if valid
                if (typeCurrent != typeof (RuntimeMethodHandle))
                {
                    // get its assembly
                    var assembly = typeCurrent.Assembly;

                    // if valid
                    if (!assembly.GlobalAssemblyCache
                        && !assembly.IsDynamic
                        && (assembly.GetAttribute<System.CodeDom.Compiler.GeneratedCodeAttribute>() == null))
                    {
                        // then we found a valid assembly, get it as a candidate
                        result = assembly;
                    }
                }
            }

            // increase number of frames to skip
            framestoSkip++;
        } // while we have a working method
        while (methodCurrent != null);
    }
    return result;
}

要保证装配是我们想要的,我们有3个条件:

To ensure the assembly is what we want, we have 3 conditions :


  • 装配不在GAC

  • 大会没有动态

  • 不产生装配(为了避免临时asp.net文件

当基页在一个单独的组件定义的最后一个问题,我遇到的。
(我用ASP.Net MVC,但它会与ASP.Net相同)。
在特定的情况下,它返回的是单独的程序,不包含网页的人。

The last problem I meet is when the base page is defined in a separate assembly. (I use ASP.Net MVC, but it'll be the same with ASP.Net). In that particular case, it's that separate assembly that is returned, not the one containing the page.

我要寻找现在是:

1)是我组装的验证条件还不够吗? (我可能已经忘记了的情况下)

1) Are my assembly validation conditions enough ? (I may have forgotten cases)

2)有没有一种方法,从给定code-生成的程序集在ASP.Net临时文件夹,以获取有关包含页/查看项目信息?
(我想不会,但谁知道...)

2) Is there a way, from a given code-generated assembly in the ASP.Net temporary folder, to get information about the project that contains that Page / View ? (I think not, but who knows...)

推荐答案

这似乎是一个可靠的,简单的方式来获得进入或主组装一个web应用程序。

This seems to be a reliable, simple way to get the "entry" or main assembly for a web app.

如果你把控制器在一个单独的项目,你可能会发现基类ApplicationInstance是不是在同一个组件,你的MVC项目包含意见 - 但是,这种设置似乎pretty罕见的(我说出来,因为我已经试过这种设置在一个点上,和前阵子的几个博客支持这样的想法)。

If you put controllers in a separate project, you may find that the base class of ApplicationInstance is not in the same assembly as your MVC project that contains the Views - but, this setup seems pretty rare (I mention it because I've tried this setup at one point, and a while back a few blogs supported the idea).

    static private Assembly GetWebEntryAssembly()
    {
        if (System.Web.HttpContext.Current == null ||
            System.Web.HttpContext.Current.ApplicationInstance == null) 
        {
            return null;
        }

        var type = System.Web.HttpContext.Current.ApplicationInstance.GetType();
        while (type != null && type.Namespace == "ASP") {
            type = type.BaseType;
        }

        return type == null ? null : type.Assembly;
    }

这篇关于GetEntryAssembly Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆