开发者

Getting first executing assembly returns null

开发者 https://www.devze.com 2023-03-27 17:21 出处:网络
I\'m trying to get the first executing assembly using the following code: AssemblyName entryAssembly = Assembly.GetEntryAssembly().GetName();

I'm trying to get the first executing assembly using the following code:

AssemblyName entryAssembly = Assembly.GetEntryAssembly().GetName(); 

I'm getting a null exception though. The GetExecutingAssembly() and GetCallingAssembly() functions do return values though. In the documentation, it mentions that it can retur开发者_运维百科n null when called from unmanaged code. I don't think my code falls into this category.

I'm running a ASP.NET MVC 2 application in Visual Studio using the Visual Studio Development Server.

Any ideas why I'm getting a null?


This is by design. Quote from Gendarme (note: I'm quoting myself):

This rule warns when an assembly without an entry point (i.e. a dll or library) calls Assembly.GetEntryAssembly (). This call is problematic since it will always return null when called from outside the root (main) application domain. This may become a problem inside libraries that can be used, for example, inside ASP.NET applications.

from https://github.com/spouliot/gendarme/wiki/Gendarme.Rules.BadPractice.GetEntryAssemblyMayReturnNullRule%282.10%29

Edit: the above answer the 'why' (your stated question). A possible workaround it to create a StackTrace and iterate each StackFrame until the top to see from which assembly it comes from. However this will only work, like you wish, when done from the main (web) application thread (i.e. take great care on where this is called from).

0

精彩评论

暂无评论...
验证码 换一张
取 消