title: InvalidCastException for Object of the same type - Custom Control Load
body: <p>I have a very wired error, one of my custom controls seems that is create two compiled files, and when I try to load it dynamically with <code>LoadControl()</code> is just fail because can not cast the one to the other - even if they are exactly the same. I write the message to see that all is the same, is only change the compiled dll.</p>
  
  <pre><code>System.Web.HttpUnhandledException (0x80004005):     
   Exception of type 'System.Web.HttpUnhandledException' was thrown. ---&gt;             
      System.InvalidCastException:
  [A]ASP.Modules_OneProduct_MedioumImage cannot be cast to
  [B]ASP.Modules_OneProduct_MedioumImage.         
  
     Type A originates from 'App_Web_kg4bazz1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
  in the context 'Default'
      at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\80ed7513\10eb08d9\App_Web_kg4bazz1.dll'.          
  
     Type B originates from 'App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' 
  in the context 'Default'    
      at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\80ed7513\10eb08d9\App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b.dll'.
  </code></pre>
  
  <h2>The code</h2>
  
  <p>This is the code as it is right now after I have follow <a href="http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx">exactly what is written on MSDN</a>:  </p>
  
  <pre><code>foreach (int OneProductID in TheProductIdArrays)
  {
      // here is the throw.
      ASP.Modules_OneProduct_MedioumImage OneProduct = 
          (ASP.Modules_OneProduct_MedioumImage)LoadControl(@"~/mod/OneProduct_MediumImage.ascx");
  
      // do some work with 
      //OneProduct
  }
  </code></pre>
  
  <p>Previously I have Load the control without the <code>ASP.</code> but after this bug appears and looking for solution, I strictly follow what is on MSDN. The bug is still here no matter what I do.</p>
  
  <p>I have also try both of this methods, each one alone, and together (again fail)</p>
  
  <pre><code>&lt;%@ Register src="~/mod/OneProduct_MediumImage.ascx" tagname="OneProduct_MediumImage" tagprefix="uc1" %&gt;
  &lt;%@ Reference Control="~/mod/OneProduct_MediumImage.ascx" %&gt;
  </code></pre>
  
  <h2>Config</h2>
  
  <p>My web.config, I have try with <code>maxBatchSize</code> 20, 100, 1000, also with <code>optimizeCompilations</code> true or false, but the bug is appears again.</p>
  
  <pre><code>&lt;compilation debug="false" defaultLanguage="C#" batch="true"
  maxBatchSize="800" batchTimeout="10800" optimizeCompilations="false"
  targetFramework="4.0"&gt;
  </code></pre>
  
  <h2>Now some details about</h2>
  
  <ul>
  <li>The error is random, in some compile appears, in some other not.</li>
  <li>The project is a big one, the pages are live with a lot of people in every minute that ask to see something, but also appears when there is no one inside.</li>
  <li>Is run on 64bit dot.net 4,  Intergrated</li>
  <li>Run as web garden but also tested and one pool alone (and get the same issue)</li>
  <li>The session is off on the full project.</li>
  <li>The pages are run from 2007 but this issue is appears the last month, unfortunately I can not find where and how is started, or what is trigger it because I late some days to see it.</li>
  <li>Appears only one one custom control loads, the one that have heavy call.</li>
  <li>I have change 4 times the code making small changes, or big changes and still there.</li>
  <li>I have try with <code>optimizeCompilations</code> true and false and the same issue.</li>
  <li>I have try also by stopping the web, delete all temporary files, reopening, and there was again.</li>
  <li>I have try to place a mutex on global.asax when the application starts to lock only one compile at the time, but this fails also.</li>
  <li>From the moment that works, then all is good, but if not works is not auto corrected.</li>
  <li>The code that I load this custom control is exist and called in more than one places on the code, on different pages.</li>
  <li>Other custom controls, with similar load did not have any problems.</li>
  <li>ViewState is disabled for this custom control.</li>
  <li>I have also try relocate some code, change the full function call with micro optimizes, no again fail.</li>
  <li><strike>Is work fine on development computer</strike>. I place <code>batch="true"</code> on web.config and the bug appears right away.</li>
  <li>There are no other issues like that, like a bug that we can not fix no matter what. The system is run for days, the pool is NOT recycle at all, the memory is stable, and there is more free to use. The program is run for years now, but we change is almost every day with updates.</li>
  <li>Under the same core code runs more than one sites (something like stackexchange) and all have the same random problem.</li>
  <li>The AutoEventWireup is false</li>
  <li>Its appears and on other custom control that I load the same way.</li>
  </ul>
  
  <p>What I do now as workaround when this bug appears: I just force the project to recompile with a small change, and the error go away, until the next update.</p>
  
  <p>I have a bug that try to solve the last tree weeks with out find the reason. I have try almost anything I can thing of, but all fails, and the bug appears again. So I post here maybe some can help me out and find a way out of this.</p>
  
  <p>Last word: This bug is crazy, the custom control is the same, I do anything on it I only load it dynamically and boom, the compiler is have it two different times for some reason that only he knows - randomly.</p>
  
  <h2>Update 1</h2>
  
  <p>I been able to reproduce the bug on the developer machine. There I discover that the two dll modules that contains this custom control have a different.</p>
  
  <p>The one was a bundle of 4 custom controls together. The other module was the custom control alone.</p>
  
  <h2>Workaround</h2>
  
  <p>After tree weeks trying to fix this bug I end up that this bug is appears when the compiler make batch compile of a directory, and bundle many different custom controls, in the same dll. So when I try to load it alone is throw this exception.</p>
  
  <p>So I move the problematic custom control in a different directory alone and seems that I avoid it for now.</p>
  
  <h2>Update 2</h2>
  
  <p>Appears again, even after I move some files to a different directory. Is random and can not find a clear connection with what is triggers its.</p>
  
  <h2>Update 3</h2>
  
  <p>Because we have spot that the main issue here is the batch compile (<code>batch="true"</code>) that compiles on the same dll many custom controls, one way to say to the compiler to NOT do that, is the <a href="http://msdn.microsoft.com/en-us/library/system.web.configuration.compilationsection.maxbatchgeneratedfilesize.aspx"><code>maxBatchGeneratedFileSize</code></a> parameter. I use it with a value of 100, and the issue appears again, now I have lower it to 40 and test it.</p>
  
  <pre><code>maxBatchGeneratedFileSize="40"
  </code></pre>