GetEntryAssembly returns null…how to avoid it?
-
Friday, April 13, 2012 9:06 AM
I have a WPF application that uses a third-party library to perform a specific task; this library, in one of its methods, executes the statement Assembly.GetEntryAssembly().
If I run the WPF application on debug (by running a .Net exe for testing) the library works properly and Assembly.GetEntryAssembly() returns a reference to the assembly of the executable, but in production I have a different situation that causes me some problems.
In production, the WPF application is integrated with a VB6 application and programs are launched from a VB6 menu using the InteropFormLibrary; in this situation the library raise an exception because Assembly.GetEntryAssembly() returns "null" (it can't find the executable associated with the assembly).
My problem is that I can't change the code of the library because I don't have the sources available and I found the statement that throws the exception by dll decompiler and analyzing the method that was in the exception.
Now my question is: Is there any way to make sure that the method "GetEntryAssembly" no returns "null"? Can I assign or "circumvent" in some way?
- Moved by Sheldon _Xiao Tuesday, April 17, 2012 9:08 AM (From:Windows Presentation Foundation (WPF))
All Replies
-
Friday, April 13, 2012 9:27 AM
You could try doing a Try/Catch as a last resort, and handle what should happen if the exception is raised, and what happens when all is well.
try { //Do you thing. } catch (Exception e) { //Do nothing or whatever you need done if null is returned and exception is thrown. }
Developing is part of being a developer.
-
Friday, April 13, 2012 9:35 AM
You could try doing a Try/Catch as a last resort, and handle what should happen if the exception is raised, and what happens when all is well.
try { //Do you thing. } catch (Exception e) { //Do nothing or whatever you need done if null is returned and exception is thrown. }
Developing is part of being a developer.
I can't because the exception cancel all other statements below Assembly.GetEntryAssembly() (in the same method)...and this break library working. -
Tuesday, April 17, 2012 9:16 AMModerator
According the MSDN library said, it will return null when it loads a unmanaged assembly.
The GetEntryAssembly method can return null when a managed assembly has been loaded from an unmanaged application. For example, if an unmanaged application creates an instance of a COM component written in C#, a call to the GetEntryAssembly method from the C# component returns null, because the entry point for the process was unmanaged code rather than a managed assembly.
The link for your reference:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly.aspx
Hope it helps.
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
- Proposed As Answer by Kris444 Tuesday, April 17, 2012 12:54 PM
- Marked As Answer by Lie YouModerator Monday, April 23, 2012 5:50 AM
-
Tuesday, April 17, 2012 12:58 PM
Hi,
Since you are using thirparty library, you can look for the method that takes Assembly.
If you don't find any,
1> If the library is opensource you can rebuild with this addition,
2> If not opensource you can request the author for this.
If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
- Marked As Answer by Lie YouModerator Monday, April 23, 2012 5:50 AM
-
Thursday, April 19, 2012 8:21 PM
You can easily override what Assembly.GetEntryAssembly() returns.
Actually Assembly.GetEntryAssembly() returns AppDomainManager.EntryAssembly property for the current AppDomain.
So what you can do is create another AppDomain to run your code in. Set it up with your own implementation of AppDomainManager where you override EntryAssembly property and return whatever assembly you want.
Example:
Suppose you have two assemblies, one .exe called MainAssembly (for this experiment it will stand in place of your unmanaged bootstrapper) and one .dll called OtherAssembly. The MainAssembly references the OtherAssembly.
Source for MainAssembly:
using System; using System.Reflection; using OtherAssembly; namespace MainAssembly { internal class Program { private static void Main() { var dmType = typeof (DomainManager); var setup = new AppDomainSetup { AppDomainManagerAssembly = dmType.Assembly.FullName, AppDomainManagerType = dmType.FullName }; var domain = AppDomain.CreateDomain("OtherDomain", null, setup); domain.DoCallBack(ShowEntryAssembly); AppDomain.Unload(domain); } public static void ShowEntryAssembly() { Console.WriteLine("Entry assembly is: " + Assembly.GetEntryAssembly().FullName); } } }
Source for the OtherAssembly:
using System; using System.Reflection; namespace OtherAssembly { public class DomainManager : AppDomainManager { public override Assembly EntryAssembly { get { // or any other assembly you want return Assembly.GetExecutingAssembly(); } } } }
Compile them both and run MainAssembly.exe in command console:
C:\> MainAssembly.exe Entry assembly is: OtherAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
You'll see that inside the new AppDomain Assembly.GetEntryAssembly returns OtherAssembly instead of the actual entry assembly that would be the MainAssembly, or what in your case would normally be null.
Hope this helps,
- Levi- Marked As Answer by Lie YouModerator Monday, April 23, 2012 5:50 AM

