PreApplicationStartMethod Attribute Raises Exception

The strange thing that happens to me with the PreApplicationStartMethod attribute. I implemented it in my last project. In AssemblyInfo.cs, I have the following line:

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

The type and method are as follows:

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

When I rebuild the application and load it in the browser, I get the following error:

The method specified by PreApplicationStartMethodAttribute in the assembly 'MyWebApp, Version = 0.0.1.0, Culture = neutral, PublicKeyToken = null' cannot be resolved. Type: "MyAssembly.Initializer", MethodName: "Initialize". Verify that the type is public and the method is public and static (Shared in Visual Basic).

I really don't know what the problem is.

+4
source share
4 answers

Strange, we often use this function in the ASP.NET team and do not encounter this. To help debug this, can you try running the following code that does something similar to what ASP.NET does to define a method?

The best way to run this is to create a console application and put this code there. Then just call him, passing him the assembly where you see the problem. Then you will want to debug it and track it carefully to find out what is going on.

By the way, before that double check that you are putting the attribute in the same assembly that contains the class. that is, it cannot point to a type in another 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; } } 
+10
source

I had a similar problem. I was referring to a class in another assembly.

So, in my web application, I created a wrapper that calls the method in a separate assembly. Then I refer to this shell in the PreApplicationStartMethod attribute.

+3
source

Step 1: Verify that you are using System.Web in AssemblyInfo.cs

Step 2:

You also need to right-click on your project โ€œadd โ†’ referenceโ€ and select Assemblies โ†’ Framework โ†’ Click โ€œSystem.Webโ€.

This has always been the case for me when creating the class library, because when creating the project this namespace is not used.

+2
source

I copied your code and it works great. Therefore, try to clean and rebuild the entire application.

Are you sure you are not in a nested namespace or something like that?

0
source

Source: https://habr.com/ru/post/1310459/


All Articles