【问题标题】:PreApplicationStartMethod Attribute causes exceptionPreApplicationStartMethod 属性导致异常
【发布时间】:2010-05-21 08:21:19
【问题描述】:

PreApplicationStartMethod 属性发生在我身上的奇怪事情。我确实在我的最新项目中实现了它。在 AssemblyInfo.cs 我有以下行:

[assembly: PreApplicationStartMethod(typeof(MyAssembly.Initializer), "Initialize")]

类型和方法如下所示:

namespace MyAssembly
{
    public static class Initializer
    {
       public static void Initialize()
       {
           TranslationKeys.Initialize();
       }
    }
}

当我重建我的应用程序并将其加载到浏览器中时,我收到以下错误:

无法解析程序集“MyWebApp,Version=0.0.1.0,Culture=neutral,PublicKeyToken=null”上的 PreApplicationStartMethodAttribute 指定的方法。类型:“MyAssembly.Initializer”,方法名:“初始化”。验证类型是公共的,方法是公共的和静态的(在 Visual Basic 中是共享的)。

我真的不知道问题出在哪里。

【问题讨论】:

    标签: c# asp.net .net-4.0


    【解决方案1】:

    奇怪,我们在 ASP.NET 团队中经常使用此功能,但没有遇到过。为了帮助调试,您可以尝试运行以下代码,它的作用类似于 ASP.NET 用于定位方法的操作吗?

    运行它的最佳方法是创建一个控制台应用程序并将该代码放入其中。然后只需调用它,将您看到问题的程序集传递给它。然后,您需要对其进行调试并仔细跟踪以查看发生了什么。

    顺便说一句,在执行此操作之前,请仔细检查您是否将属性放在包含该类的同一程序集中。即它不能指向不同程序集中的类型。

    这是要尝试的代码:

    using System;
    using System.Web;
    using System.Reflection;
    
    public class TestClass {
        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 static MethodInfo FindPreStartInitMethod(Type type, string methodName) {
            MethodInfo method = null;
            if (type.IsPublic) {
                method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase,
                                binder: null,
                                types: Type.EmptyTypes,
                                modifiers: null);
            }
            return method;
        }
    }
    

    【讨论】:

    • 顺便说一句,在执行此操作之前,请仔细检查您是否将属性放在包含该类的同一程序集中。即它不能指向不同程序集中的类型。 - - - - - - - - - - - - - - - - - - - - 谢谢!那成功了。在我的解决方案中,我有不同的项目。 Initialize 类的解决方案与我的 Web 应用程序不同。
    • 但是。考虑一下并查看堆栈跟踪,我认为该类需要在同一个项目/程序集中是一件奇怪的事情。它说:System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies()。并且引用了包含我的 Initialize 类的程序集。
    • 不要过多地使用内部方法的名称 :) 在这里,它的意思是它为每个引用的程序集查找 Init 方法。但是对于给定的程序集,它只查看该程序集本身。这是我们添加的一项故意检查,以防止程序集导致调用不同程序集中的不相关代码。我们遵循的逻辑基本上就是我上面写的,所以同汇编检查完全是经过深思熟虑的。
    【解决方案2】:

    我遇到了类似的问题。我在不同的程序集中引用了一个类。

    因此,在我的 Web 应用程序中,我创建了一个包装器,用于调用单独程序集中的方法。然后我在 PreApplicationStartMethod 属性中引用这个包装器。

    【讨论】:

      【解决方案3】:

      第 1 步: 确保您在 AssemblyInfo.cs 中使用了 System.Web

      第 2 步:

      您还需要右键单击您的项目“add-->reference”并选择Assemblies--> Framework --> 单击“System.Web”。

      我在创建类库时总是这样,因为它在创建项目时没有默认这个命名空间..

      【讨论】:

      • 感谢添加参考帮助。
      【解决方案4】:

      我复制了您的代码,它运行良好。因此,请尝试清理并重建整个应用程序。

      你确定你不在嵌套命名空间之类的吗?

      【讨论】:

      • 我的 Web 应用程序的命名空间是 WebApplication。 Refresher 的命名空间是 WebApplication.Logic。这应该没什么区别吧?
      • 我确实清理并重建了我的整个解决方案。我在我的开发机器上运行它。带有 IIS 5.1 的 Windows XP。但由于它的目标是 .NET 4.0,所以也不应该有所作为。
      • 如果你尝试使用内置的网络服务器(Casini)
      • 发生同样的异常。奇怪。
      猜你喜欢
      • 2013-01-07
      • 2011-08-10
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      相关资源
      最近更新 更多