title: Why GetEntryAssembly returns null?
body: <p>What is the underlying implementation of <a href="https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly(v=vs.110).aspx" rel="noreferrer">GetEntryAssembly</a> in C#? And why it does return null when dll was loaded from an unmanaged application?</p>
  
  <p>The MSDN documentation says:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>I have the following setup:</p>
  
  <blockquote>
    <p>Native executable (C++) -> Mixed mode assembly (C++/cli) -> Managed
    assembly (C#)</p>
  </blockquote>
  
  <p>Managed assembly is optional, in mixed mode assembly one can call GetEntryAssembly and get null.</p>
  
  <p>While debugging we've tracked to this call in C# <a href="https://github.com/dotnet/coreclr/blob/3cdb42e9b5a8ca871b53a27194106707a2db8962/src/mscorlib/src/System/AppDomainManager.cs" rel="noreferrer">source code</a>:</p>
  
  <pre><code>[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  private static extern void GetEntryAssembly(ObjectHandleOnStack retAssembly);
  </code></pre>
  
  <p>We can see that the entry assembly should be the native executable. However it is not retrieved. I wonder what are the reasons for this? Shouldn't native to managed transition take care of that?</p>
  
  <p><strong>Edit</strong></p>
  
  <p><code>GetEntryAssembly</code> is used internally by .NET. We are just experiencing the side effect when it returns <code>null</code>. 
  Reproducible with one line in Managed assembly:</p>
  
  <pre><code>System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForDomain ();
  </code></pre>
  
  <p>This will throw the following exception: <code>System.IO.IsolatedStorage.IsolatedStorageException: Unable to determine the identity of domain.</code> The root cause for this exception seems to be the entry assembly being null. </p>