当入口程序集为 ASP.NET 时,GetEntryAssembly 似乎不起作用 [英] GetEntryAssembly doesn't appear to work when the entry assembly is ASP.NET

查看:17
本文介绍了当入口程序集为 ASP.NET 时,GetEntryAssembly 似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不认为这是对这些的欺骗:

First, I do not believe this is a dupe of these:

Web 应用程序的 GetEntryAssembly

从 ASP.NET 应用程序获取入口程序集

我有一个引用类库的 ASP.NET WebForms 应用程序.当我从 ASP.NET 应用程序引用的类库中调用 Assembly.GetEntryAssembly() 时,它返回 null.但是,当我在控制台应用程序引用类库时调用相同的代码时,它会返回控制台应用程序程序集.

I have an ASP.NET WebForms app that references a class library. When I call Assembly.GetEntryAssembly() from within the class library while it's referenced by the ASP.NET app, it returns null. However, when I call that same code while the class library is referenced by a console app, it returns the console app assembly.

这个答案或许可以说明为什么 Assembly.GetEntryAssembly() 与 ASP.NET 的行为不同:

This answer perhaps sheds light on why Assembly.GetEntryAssembly() behaves differently with ASP.NET:

为什么 ASP.NET 以不同的方式解析程序集引用?

不过,我希望能够像获取 Windows 应用程序(控制台、WPF、WinForms 等)一样获取 ASP.NET 程序集.有什么方法可以让我的类库在不添加任何 ASP.NET 引用的情况下了解 ASP.NET 程序集?

Still, I'd like to be able to get the ASP.NET assembly as if it were a Windows application (console, WPF, WinForms, etc). Is there any way to allow my class library to know about the ASP.NET assembly without adding any ASP.NET references?

虽然不是我正在寻找的解决方案,但暂时有效.基本上,我使用上面问题之一的回答者提供的以下代码从 Web 应用程序获取 ASP.NET 程序集,然后将其作为参数传递给我的类库中的方法.

While not the solution I am looking for, this works for the time being. Basically, I use the code below provided by the answerer from one of the questions above to get the ASP.NET assembly from within the web app and then pass it as an argument to a method in my class library.

还有 AppDomain.CurrentDomain.GetAssemblies() 它在类库中调用时会公开 ASP.NET 程序集,但在应用程序域中加载了 50 个程序集,这意味着您需要一个魔法字符串来获取程序集名称.绝对不是一个选项,但它可以工作.

There's also AppDomain.CurrentDomain.GetAssemblies() which does expose the ASP.NET assembly when called within the class library, but there are like 50 assemblies loaded in the app domain which means you'd need a magic string to get the assembly name. Definitely not an option, but it could work.

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;
}

推荐答案

ASP.NET 应用程序是由 IIS 工作进程启动的,在大多数情况下,它是一个本机模块.因此,该 API 应返回 null 以反映与控制台应用程序的差异.

An ASP.NET application is started by IIS worker process, which is a native module, in most cases. Thus, that API should return null to reflect the differences from a console application.

无论您想使用该 API 实现什么目标,都可以找到替代方法.

Whatever you want to achieve with that API, find an alternative way instead.

这篇关于当入口程序集为 ASP.NET 时,GetEntryAssembly 似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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