PreApplicationStartMethod Attribute causes exception
Strange thing happening to me with the PreApplicationStartMethod Attribute. I did implement it in my latest project. In the AssemblyInfo.cs I have the following line:
[assembly: PreApplicationStartMethod(typeof(MyAssembly.Initializer), "Initialize")]
The Type and method look like this:
namespace MyAssembly
{
public static class Initializer
{
public static void Initialize()
{
TranslationKeys.Initialize();
}
}
}
When I rebuild my application and load it in the browser I get the following error:
The method specified by the PreApplicationStartMethodAttribute on assembly 'MyWebApp, Version=0.0.1.0, Culture=neutral, PublicKeyToken=null' cannot be resolved. Type: 'MyAssembly.Initializer', MethodName: 'Ini开发者_开发技巧tialize'. Verify that the type is public and the method is public and static (Shared in Visual Basic).
I really have no idea what the problem is.
Strange, we use this feature a lot in the ASP.NET team, and have not run into this. To help debug this, can you try running the following code, which does something similar to what ASP.NET does to locate the method?
The best way to run it is to create a Console app and put that code in there. Then just call it, passing it the assembly where you are seeing the issue. You'll then want to debug it and trace through it carefully to see what goes on.
BTW, before doing this, double check that you are putting the attribute on the same assembly that contains the class. i.e. it can't point to a type in a different assembly.
Here is the code to try:
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;
}
}
I had a similar issue. I was referencing a class in a different assembly.
So in my web application, I created a wrapper that calls the method in the separate assembly. I then reference this wrapper in the PreApplicationStartMethod attribute.
Step 1: Make sure you have using System.Web in AssemblyInfo.cs
Step 2:
You also need to right click your project "add-->reference" and choose Assemblies--> Framework --> Click "System.Web".
This was always the case for me when creating Class Library as it does not default this namespace when creating a project..
I copied your code and it works perfectly. So try to clean and rebuild the whole application.
Are you sure you are not in a nested namespace or something?
精彩评论