title: GetEntryAssembly for web applications
body: <p><strong>Assembly.GetEntryAssembly()</strong> does not work for web applications.</p>
  
  <p>But... I really need something like that.
  I work with some deeply-nested code that is used in both web and non-web applications.</p>
  
  <p>My current solution is to browse the StackTrace to find the first called assembly.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Version of 'GetEntryAssembly' that works with web applications
  /// &lt;/summary&gt;
  /// &lt;returns&gt;The entry assembly, or the first called assembly in a web application&lt;/returns&gt;
  public static Assembly GetEntyAssembly()
  {
      // get the entry assembly
      var result = Assembly.GetEntryAssembly();
  
      // if none (ex: web application)
      if (result == null)
      {
          // current method
          MethodBase methodCurrent = null;
          // number of frames to skip
          int framestoSkip = 1;
  
  
          // loop until we cannot got further in the stacktrace
          do
          {
              // get the stack frame, skipping the given number of frames
              StackFrame stackFrame = new StackFrame(framestoSkip);
              // get the method
              methodCurrent = stackFrame.GetMethod();
              // if found
              if ((methodCurrent != null)
                  // and if that method is not excluded from the stack trace
                  &amp;&amp; (methodCurrent.GetAttribute&lt;ExcludeFromStackTraceAttribute&gt;(false) == null))
              {
                  // get its type
                  var typeCurrent = methodCurrent.DeclaringType;
                  // if valid
                  if (typeCurrent != typeof (RuntimeMethodHandle))
                  {
                      // get its assembly
                      var assembly = typeCurrent.Assembly;
  
                      // if valid
                      if (!assembly.GlobalAssemblyCache
                          &amp;&amp; !assembly.IsDynamic
                          &amp;&amp; (assembly.GetAttribute&lt;System.CodeDom.Compiler.GeneratedCodeAttribute&gt;() == null))
                      {
                          // then we found a valid assembly, get it as a candidate
                          result = assembly;
                      }
                  }
              }
  
              // increase number of frames to skip
              framestoSkip++;
          } // while we have a working method
          while (methodCurrent != null);
      }
      return result;
  }
  </code></pre>
  
  <p>To ensure the assembly is what we want, we have 3 conditions :</p>
  
  <ul>
  <li>the assembly is not in the GAC</li>
  <li>the assembly is no dynamic</li>
  <li>the assembly is not generated (to avoid temporary asp.net files</li>
  </ul>
  
  <p>The last problem I meet is when the base page is defined in a separate assembly.
  (I use ASP.Net MVC, but it'll be the same with ASP.Net).
  In that particular case, it's that separate assembly that is returned, not the one containing the page.</p>
  
  <p>What I am looking for now is :</p>
  
  <p>1) Are my assembly validation conditions enough ? (I may have forgotten cases)</p>
  
  <p>2) Is there a way, from a given code-generated assembly in the ASP.Net temporary folder, to get information about the project that contains that Page / View ?
  (I think not, but who knows...)</p>